aboutsummaryrefslogtreecommitdiffstats
path: root/notifier/observer_growl.js
blob: 166e1ab223dcbd9844b0bf33c28d7cd07e2c3ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * notifier.js plugin observer
 * @name            observer_growl.js
 * @description     growl when notified.
 * @description-ja  Growl風通知。
 * @author          suVene suvene@zeromemory.info
 * @version         0.1.0
 * Last Change:     07-Dec-2008.
 *
 * use JQuery
 *   http://jquery.com/
 * use JGrowl
 *   http://stanlemon.net/projects/jgrowl.html
 */
(function() {

var notifier = liberator.plugins.notifier;
if (!notifier) return;

var libly = notifier.libly;
var $U = libly.$U;
var logger = $U.getLogger('observer_growl');

var Growl = function() {//{{{
    this.initialize.apply(this, arguments);
}
Growl.prototype = {
    defaults: {
        life: 5000
    },
    initialize: function(dom, container) {
        this.dom = dom;
        this.container = container;
        this.created = new Date();
        this.life = this.defaults.life;
        dom.childNodes[0].addEventListener("click", $U.bind(this, this.remove), false);
    },
    remove: function() {
        this.container.removeChild(this.dom);
    },

}//}}}

notifier.observer.register({
    initialize: function () {
        logger.log('initialize');
        this.count = 1;

        io.getRuntimeDirectories('').forEach(function(dir) {
            let path = io.expandPath(dir.path + '/plugin/notifier');
            $U.readDirectory(path, '^growl' , function(f) {
                try {
                    io.source(f.path, true)
                    logger.log('load success: ' + f.leafName);
                } catch (e) {
                    logger.log('load failed: ' + f.leafName);
                }
            });
        });
    },
    update: function(message) {
        logger.log('update:' + this.count);

        var doc = window.content.document;
        var container = doc.getElementById("observer_growl");
        if (!container) {
            doc.body.appendChild(util.xmlToDom(<div id="observer_growl" class="observer_growl top-right" />, doc));
            container = doc.getElementById("observer_growl");
        }

        this.createPopup(doc, message, container);
        container.appendChild(this.createPopup(doc, message, container));

        if (container.childNodes.length == 1) {
            var interval = setInterval($U.bind(this, this.checkStatus), 1000);
            container.__interval__ = interval;
        }

        this.count++;
    },
    createPopup: function(doc, message, nodes) {
        var dom;
        var html =
        <div class="observer_growl_notification" style="display: block;">
            <div class="close">&#215;</div>
            <div class="header">{util.escapeHTML(this.count + ': ' + message.title)}</div>
            <div class="message">{util.escapeHTML(message.message)}</div>
        </div>;
        dom = util.xmlToDom(html, doc, nodes);
        dom.__data__ = new Growl(dom, nodes);

        let count = this.count;
        return dom;
    },
    checkStatus: function() {

        var doc = window.content.document;
        var container = doc.getElementById("observer_growl");
        if (!container) return;

        var removeNodes = [];
        for (let i = 0, len = container.childNodes.length; i < len; i++) {
            let item = container.childNodes[i];
            let growl = item.__data__;
            if (growl && growl.created &&
                growl.created.getTime() + growl.life < (new Date()).getTime()) {
                removeNodes.push(item);
            }
        }
        removeNodes.forEach(function(n) container.removeChild(n));

        if (container.childNodes.length == 0)
            clearInterval(container.__interval__);

    }
});

})();
// vim: set fdm=marker sw=4 ts=4 sts=0 et: