File size: 6,380 Bytes
cf0f72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
 * Notificator Options
 * @namespace
 */

var NotificatorOptions = {
    /**
     * Init options
     */
    init: function(_notificator) {
        this.notificator = _notificator;
        
        this.elems = {
            updateInterval: document.getElementById('interval'),
            openListe: document.getElementById('openListe'),
            openInNewTab: document.getElementById("newTab"),
            showAllNotifButton: document.getElementById("allNotifs"),
            showDesktopNotif: document.getElementById("notifNative"),
            useDetailedNotifs: document.getElementById("detailedNotifs"),
            notifPriority: document.getElementById("priorityNotif"),
            mpPriority: document.getElementById("priorityMP"),
            playSon: document.getElementById("playSon"),
            tweet: document.getElementById("tweet"),
            SdZLink: document.getElementById("SdZLink"),
            autoclosePopup: document.getElementById("autoclosePopup"),
            archiveAllLink: document.getElementById("archiveAllLink")
        };
        
        document.getElementById("enregistrer").addEventListener("click", this.save.bind(this));
        document.getElementById("reset").addEventListener("click", this.reset.bind(this));
        this.elems["updateInterval"].addEventListener("keyup", this.checkInput.bind(this));
        this.elems["openListe"].addEventListener("click", this.toggle.bind(this));
        this.elems["showDesktopNotif"].addEventListener("click", this.toggle.bind(this));
        
        this.load();
    },
    
    oldValue: 5,
    
    /**
     * Check inputs
     * @param {Object} [evt] JS Event
     */
    checkInput: function(evt) {
        var val = this.elems["updateInterval"].value;
        if(val == '')
            this.elems["updateInterval"].value = this.oldValue;
        else {
            if(parseInt(val) > 60) {
                this.elems["updateInterval"].value = 60;
                this.oldValue = 60;
            } else {
                this.oldValue = val;
            }
        }
    },

    /**
     * Reset options
     */
    reset: function() {
        var c = confirm("Voulez vous r\u00E9ellement remettre \u00E0 z\u00E9ro les options ?");
        if(c) {
            this.notificator.resetOptions();
            window.location.reload();
        }
    },
    
    /**
     * Save options
     */
    save: function() {
        this.notificator.setOptions(this.getValues(), function() {
            //console.log("Options Saved");
            window.close();
        });
    },
    
    /**
     * Load options
     */
    load: function() {
        this.options = this.notificator.getOptions();
        
        for(var key in this.elems) {
            if(this.options[key] !== undefined) {
                if(this.elems[key].type == "checkbox") {
                    this.elems[key].checked = this.options[key];
                }
                else {
                    this.elems[key].value = this.options[key];
                }
            }
        }
        this.toggle();
        this.oldValue = this.elems['updateInterval'].value;
        this.checkAvatar();
    },
    
    /**
     * Get inputs value
     * @returns {Object} The input values
     */
    getValues: function() {
        var obj = {};
        for(var key in this.elems) {
            var val = this.elems[key].type == "checkbox" ? this.elems[key].checked : parseInt(this.elems[key].value);
            obj[key] = val;
        }
        return obj;
    },
    
    /**
     * Toggle inputs
     */
    toggle: function() {
        if(this.elems['showDesktopNotif'].checked) {
            $(".subNotifFields").removeClass("disabled");
            $(".subNotifFields input").attr("disabled", false);
        }
        else {
            $(".subNotifFields").addClass("disabled");
            $(".subNotifFields input").attr("disabled", true);
        }
        
        if(this.elems['openListe'].checked) {
            $(".subPopupFields").removeClass("disabled");
            $(".subPopupFields input").attr("disabled", false);
        }
        else {
            $(".subPopupFields").addClass("disabled");
            $(".subPopupFields input").attr("disabled", true);
        }
        
        $(".priority input").on("change", function(e) {
            var value = "Erreur";
            switch(parseInt(e.target.value)) {
                case -2:
                    value = "Pas important";
                    break;
                case -1:
                    value = "Peu important";
                    break;
                case 0:
                    value = "Normale";
                    break;
                case 1:
                    value = "Important";
                    break;
                case 2:
                    value = "Très important";
                    break;
            }
            $(e.currentTarget).parent().find(".value").text(value);
        }).trigger("change");
    },
    
    /**
     * Check avatar
     */
    checkAvatar: function() {
        $.get("http://www.fr.openclassrooms.com", this.loadCallback.bind(this), "text");
    },
    
    /**
     * Callback when page loaded
     */
    loadCallback: function(data) {
        var self = this,
            xmlDoc = new DOMParser().parseFromString(data, "text/xml"), 
            $data = $(xmlDoc);
            
        var leDiv = $("div#connecteComme");
        
        //on est pas connecté !
        if(!this.notificator.logged) {
            leDiv.find("a").attr("href","http://www.fr.openclassrooms.com/login");
            leDiv.find("strong").text("Non connecté !");          
        } else {
            var avatarImgSrc = donnees.find("img").attr('src'),
            profil = $data.find("div#memberLinks a.nickname"),
            profilName = profil.text(),
            profilLink = profil.attr('href');
            
            leDiv.find("a").attr("href","http://www.fr.openclassrooms.com" + profilLink);
            leDiv.find("strong").text(profilName);
            leDiv.find("img").attr("src",avatarImgSrc);
        }
    }
};

document.addEventListener('DOMContentLoaded', function () {
    chrome.runtime.getBackgroundPage(function(bgWindow) {
        var notificator = bgWindow.theNotificator;
        NotificatorOptions.init.call(NotificatorOptions, notificator);
    });
});