From 7a093b26ee6e0715a917cee0d7cc2165fcad1ff7 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Tue, 6 Aug 2013 16:40:51 -0700 Subject: [PATCH] Use better practices in my own code. --- builds/appchan-x.user.js | 131 ++++++++++++++++++------------------ builds/crx/script.js | 131 ++++++++++++++++++------------------ src/General/Main.coffee | 1 + src/General/Settings.coffee | 6 +- src/Theming/Mascots.coffee | 92 ++++++++++++++----------- src/Theming/Style.coffee | 2 - 6 files changed, 189 insertions(+), 174 deletions(-) diff --git a/builds/appchan-x.user.js b/builds/appchan-x.user.js index 3a8309f84..7fd1217f7 100644 --- a/builds/appchan-x.user.js +++ b/builds/appchan-x.user.js @@ -11063,38 +11063,33 @@ }; MascotTools = { - init: function(mascot) { - var el, image, name, - _this = this; + init: function() { + var _this = this; - if (!(mascot && mascot.image)) { - if (!Conf[g.MASCOTSTRING].length) { - return; - } - name = Conf[g.MASCOTSTRING][Math.floor(Math.random() * Conf[g.MASCOTSTRING].length)]; - mascot = Mascots[name]; - Conf['mascot'] = name; + if (!Conf['Mascots'] || (g.VIEW === 'catalog' && Conf['Hide Mascots on Catalog'])) { + return; } - if (!this.el) { - this.el = $.el('div', { - id: "mascot", - innerHTML: "" - }); - if (Conf['Click to Toggle']) { - $.on(this.el, 'mousedown', MascotTools.click); - } - $.on(doc, 'QRDialogCreation', MascotTools.reposition); - $.asap((function() { - return d.body; - }), function() { - return $.add(d.body, _this.el); - }); + if (Conf['Click to Toggle']) { + $.on(this.el, 'mousedown', MascotTools.click); } + $.on(doc, 'QRDialogCreation', MascotTools.reposition); + $.asap((function() { + return d.body; + }), function() { + return $.add(d.body, _this.el); + }); + return MascotTools.toggle(); + }, + el: $.el('div', { + id: "mascot", + innerHTML: "" + }), + change: function(mascot) { + var el, image; + el = this.el.firstElementChild; - if (!Conf['Mascots'] || (Conf['Hide Mascots on Catalog'] && g.VIEW === 'catalog')) { - if (el) { - el.src = ""; - } + if (!Conf['Mascots']) { + el.src = ""; return; } if (Conf['Mascot Position'] === 'default') { @@ -11112,21 +11107,29 @@ } else if (!Conf['Silhouettize Mascots']) { $.rmClass(doc, 'silhouettize-mascots'); } - if (!mascot) { - if (name && !(mascot = Mascots[name])) { - if (el) { - el.src = ""; - } else { - null; - } - Conf[g.MASCOTSTRING].remove(name); - return MascotTools.init(); - } - } image = Array.isArray(mascot.image) ? Style.lightTheme ? mascot.image[1] : mascot.image[0] : mascot.image; el.src = image; return Style.mascot.textContent = "#mascot img {\nheight: " + (mascot.height && isNaN(parseFloat(mascot.height)) ? mascot.height : mascot.height ? parseInt(mascot.height, 10) + 'px' : 'auto') + ";\nwidth: " + (mascot.width && isNaN(parseFloat(mascot.width)) ? mascot.width : mascot.width ? parseInt(mascot.width, 10) + 'px' : 'auto') + ";\n}\n#mascot {\nmargin: " + (mascot.vOffset || 0) + "px " + (mascot.hOffset || 0) + "px;\n}\n.sidebar-large #mascot {\nleft: " + (mascot.center ? 25 : 0) + "px;\nright: " + (mascot.center ? 25 : 0) + "px;\n}\n.mascot-position-above-post-form.post-form-style-fixed #mascot {\ntransform: translateY(-" + (QR.nodes ? QR.nodes.el.getBoundingClientRect().height : 0) + "px);\n}"; }, + toggle: function() { + var enabled, len, mascot, name, string; + + string = g.MASCOTSTRING; + enabled = Conf[string]; + if (!(len = enabled.length)) { + return; + } + name = enabled[Math.floor(Math.random() * len)]; + if (!(mascot = Mascots[name])) { + enabled.remove(name); + if (el) { + el.src = ""; + } + $.set(string, Conf[string] = enabled); + return MascotTools.toggle(); + } + return MascotTools.change(mascot); + }, categories: ['Anime', 'Ponies', 'Questionable', 'Silhouette', 'Western'], dialog: function(key) { var container, dialog, div, fileInput, input, item, layout, name, option, optionHTML, setting, value, _i, _len, _ref; @@ -11165,7 +11168,7 @@ if (name === 'image') { $.on(input, 'blur', function() { editMascot[this.name] = this.value; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); fileInput = $.el('input', { type: "file", @@ -11190,12 +11193,12 @@ return alert("Mascot names must start with a letter."); } editMascot[this.name] = this.value; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); } else { $.on(input, 'blur', function() { editMascot[this.name] = this.value; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); } break; @@ -11203,7 +11206,7 @@ div = this.input(item, name); $.on($('input', div), 'blur', function() { editMascot[this.name] = parseInt(this.value); - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); break; case "select": @@ -11222,7 +11225,7 @@ setting.value = value; $.on($('select', div), 'change', function() { editMascot[this.name] = this.value; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); break; case "checkbox": @@ -11232,12 +11235,12 @@ }); $.on($('input', div), 'click', function() { editMascot[this.name] = this.checked ? true : false; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); } $.add(container, div); } - MascotTools.init(editMascot); + MascotTools.change(editMascot); $.on($('#save > a', dialog), 'click', function() { return MascotTools.save(editMascot); }); @@ -11279,7 +11282,7 @@ return reader.readAsDataURL(file); }, save: function(mascot) { - var image, name, type, _i, _len, _ref; + var image, name; name = mascot.name, image = mascot.image; if ((name == null) || name === "") { @@ -11305,23 +11308,23 @@ } } } - _ref = ["Enabled Mascots", "Enabled Mascots sfw", "Enabled Mascots nsfw"]; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - type = _ref[_i]; - if (!Conf[type].contains(name)) { - Conf[type].push(name); - $.set(type, Conf[type]); - } - } Mascots[name] = JSON.parse(JSON.stringify(mascot)); - Conf["mascot"] = name; delete Mascots[name].name; - return $.get("userMascots", {}, function(item) { - var userMascots; + return $.get("userMascots", {}, function(_arg) { + var type, userMascots, _i, _len, _ref; - userMascots = item['userMascots']; + userMascots = _arg.userMascots; userMascots[name] = Mascots[name]; $.set('userMascots', userMascots); + Conf["mascot"] = name; + _ref = ["Enabled Mascots", "Enabled Mascots sfw", "Enabled Mascots nsfw"]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + type = _ref[_i]; + if (!Conf[type].contains(name)) { + Conf[type].push(name); + $.set(type, Conf[type]); + } + } return alert("Mascot \"" + name + "\" saved."); }); }, @@ -11330,7 +11333,7 @@ return; } e.preventDefault(); - return MascotTools.init(); + return MascotTools.toggle(); }, close: function() { Conf['editMode'] = false; @@ -11537,7 +11540,6 @@ $.addClass(doc, hyphenated); } } - MascotTools.init(); if (g.VIEW === 'index') { return $.asap((function() { return $('.mPagelist'); @@ -14447,7 +14449,7 @@ name = this.parentElement.parentElement.id; if (confirm("Are you sure you want to delete \"" + name + "\"?")) { if (Conf['mascot'] === name) { - MascotTools.init(); + MascotTools.toggle(); } _ref = ["Enabled Mascots", "Enabled Mascots sfw", "Enabled Mascots nsfw"]; for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -14489,12 +14491,12 @@ select: function() { if (Conf[g.MASCOTSTRING].remove(this.id)) { if (Conf['mascot'] === this.id) { - MascotTools.init(); + MascotTools.toggle(); } } else { Conf['mascot'] = this.id; Conf[g.MASCOTSTRING].push(this.id); - MascotTools.init(Mascots[this.id]); + MascotTools.change(Mascots[this.id]); } $.toggleClass(this, 'enabled'); return $.set(g.MASCOTSTRING, Conf[g.MASCOTSTRING]); @@ -14714,6 +14716,7 @@ 'Polyfill': Polyfill, 'Emoji': Emoji, 'Style': Style, + 'Mascots': MascotTools, 'Rice': Rice, 'Banner': Banner, 'Announcements': GlobalMessage, diff --git a/builds/crx/script.js b/builds/crx/script.js index 80ba0c336..064d6fa1a 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -11050,38 +11050,33 @@ }; MascotTools = { - init: function(mascot) { - var el, image, name, - _this = this; + init: function() { + var _this = this; - if (!(mascot && mascot.image)) { - if (!Conf[g.MASCOTSTRING].length) { - return; - } - name = Conf[g.MASCOTSTRING][Math.floor(Math.random() * Conf[g.MASCOTSTRING].length)]; - mascot = Mascots[name]; - Conf['mascot'] = name; + if (!Conf['Mascots'] || (g.VIEW === 'catalog' && Conf['Hide Mascots on Catalog'])) { + return; } - if (!this.el) { - this.el = $.el('div', { - id: "mascot", - innerHTML: "" - }); - if (Conf['Click to Toggle']) { - $.on(this.el, 'mousedown', MascotTools.click); - } - $.on(doc, 'QRDialogCreation', MascotTools.reposition); - $.asap((function() { - return d.body; - }), function() { - return $.add(d.body, _this.el); - }); + if (Conf['Click to Toggle']) { + $.on(this.el, 'mousedown', MascotTools.click); } + $.on(doc, 'QRDialogCreation', MascotTools.reposition); + $.asap((function() { + return d.body; + }), function() { + return $.add(d.body, _this.el); + }); + return MascotTools.toggle(); + }, + el: $.el('div', { + id: "mascot", + innerHTML: "" + }), + change: function(mascot) { + var el, image; + el = this.el.firstElementChild; - if (!Conf['Mascots'] || (Conf['Hide Mascots on Catalog'] && g.VIEW === 'catalog')) { - if (el) { - el.src = ""; - } + if (!Conf['Mascots']) { + el.src = ""; return; } if (Conf['Mascot Position'] === 'default') { @@ -11099,21 +11094,29 @@ } else if (!Conf['Silhouettize Mascots']) { $.rmClass(doc, 'silhouettize-mascots'); } - if (!mascot) { - if (name && !(mascot = Mascots[name])) { - if (el) { - el.src = ""; - } else { - null; - } - Conf[g.MASCOTSTRING].remove(name); - return MascotTools.init(); - } - } image = Array.isArray(mascot.image) ? Style.lightTheme ? mascot.image[1] : mascot.image[0] : mascot.image; el.src = image; return Style.mascot.textContent = "#mascot img {\nheight: " + (mascot.height && isNaN(parseFloat(mascot.height)) ? mascot.height : mascot.height ? parseInt(mascot.height, 10) + 'px' : 'auto') + ";\nwidth: " + (mascot.width && isNaN(parseFloat(mascot.width)) ? mascot.width : mascot.width ? parseInt(mascot.width, 10) + 'px' : 'auto') + ";\n}\n#mascot {\nmargin: " + (mascot.vOffset || 0) + "px " + (mascot.hOffset || 0) + "px;\n}\n.sidebar-large #mascot {\nleft: " + (mascot.center ? 25 : 0) + "px;\nright: " + (mascot.center ? 25 : 0) + "px;\n}\n.mascot-position-above-post-form.post-form-style-fixed #mascot {\n-webkit-transform: translateY(-" + (QR.nodes ? QR.nodes.el.getBoundingClientRect().height : 0) + "px);\n}"; }, + toggle: function() { + var enabled, len, mascot, name, string; + + string = g.MASCOTSTRING; + enabled = Conf[string]; + if (!(len = enabled.length)) { + return; + } + name = enabled[Math.floor(Math.random() * len)]; + if (!(mascot = Mascots[name])) { + enabled.remove(name); + if (el) { + el.src = ""; + } + $.set(string, Conf[string] = enabled); + return MascotTools.toggle(); + } + return MascotTools.change(mascot); + }, categories: ['Anime', 'Ponies', 'Questionable', 'Silhouette', 'Western'], dialog: function(key) { var container, dialog, div, fileInput, input, item, layout, name, option, optionHTML, setting, value, _i, _len, _ref; @@ -11152,7 +11155,7 @@ if (name === 'image') { $.on(input, 'blur', function() { editMascot[this.name] = this.value; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); fileInput = $.el('input', { type: "file", @@ -11177,12 +11180,12 @@ return alert("Mascot names must start with a letter."); } editMascot[this.name] = this.value; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); } else { $.on(input, 'blur', function() { editMascot[this.name] = this.value; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); } break; @@ -11190,7 +11193,7 @@ div = this.input(item, name); $.on($('input', div), 'blur', function() { editMascot[this.name] = parseInt(this.value); - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); break; case "select": @@ -11209,7 +11212,7 @@ setting.value = value; $.on($('select', div), 'change', function() { editMascot[this.name] = this.value; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); break; case "checkbox": @@ -11219,12 +11222,12 @@ }); $.on($('input', div), 'click', function() { editMascot[this.name] = this.checked ? true : false; - return MascotTools.init(editMascot); + return MascotTools.change(editMascot); }); } $.add(container, div); } - MascotTools.init(editMascot); + MascotTools.change(editMascot); $.on($('#save > a', dialog), 'click', function() { return MascotTools.save(editMascot); }); @@ -11266,7 +11269,7 @@ return reader.readAsDataURL(file); }, save: function(mascot) { - var image, name, type, _i, _len, _ref; + var image, name; name = mascot.name, image = mascot.image; if ((name == null) || name === "") { @@ -11292,23 +11295,23 @@ } } } - _ref = ["Enabled Mascots", "Enabled Mascots sfw", "Enabled Mascots nsfw"]; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - type = _ref[_i]; - if (!Conf[type].contains(name)) { - Conf[type].push(name); - $.set(type, Conf[type]); - } - } Mascots[name] = JSON.parse(JSON.stringify(mascot)); - Conf["mascot"] = name; delete Mascots[name].name; - return $.get("userMascots", {}, function(item) { - var userMascots; + return $.get("userMascots", {}, function(_arg) { + var type, userMascots, _i, _len, _ref; - userMascots = item['userMascots']; + userMascots = _arg.userMascots; userMascots[name] = Mascots[name]; $.set('userMascots', userMascots); + Conf["mascot"] = name; + _ref = ["Enabled Mascots", "Enabled Mascots sfw", "Enabled Mascots nsfw"]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + type = _ref[_i]; + if (!Conf[type].contains(name)) { + Conf[type].push(name); + $.set(type, Conf[type]); + } + } return alert("Mascot \"" + name + "\" saved."); }); }, @@ -11317,7 +11320,7 @@ return; } e.preventDefault(); - return MascotTools.init(); + return MascotTools.toggle(); }, close: function() { Conf['editMode'] = false; @@ -11524,7 +11527,6 @@ $.addClass(doc, hyphenated); } } - MascotTools.init(); if (g.VIEW === 'index') { return $.asap((function() { return $('.mPagelist'); @@ -14432,7 +14434,7 @@ name = this.parentElement.parentElement.id; if (confirm("Are you sure you want to delete \"" + name + "\"?")) { if (Conf['mascot'] === name) { - MascotTools.init(); + MascotTools.toggle(); } _ref = ["Enabled Mascots", "Enabled Mascots sfw", "Enabled Mascots nsfw"]; for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -14471,12 +14473,12 @@ select: function() { if (Conf[g.MASCOTSTRING].remove(this.id)) { if (Conf['mascot'] === this.id) { - MascotTools.init(); + MascotTools.toggle(); } } else { Conf['mascot'] = this.id; Conf[g.MASCOTSTRING].push(this.id); - MascotTools.init(Mascots[this.id]); + MascotTools.change(Mascots[this.id]); } $.toggleClass(this, 'enabled'); return $.set(g.MASCOTSTRING, Conf[g.MASCOTSTRING]); @@ -14693,6 +14695,7 @@ 'Polyfill': Polyfill, 'Emoji': Emoji, 'Style': Style, + 'Mascots': MascotTools, 'Rice': Rice, 'Banner': Banner, 'Announcements': GlobalMessage, diff --git a/src/General/Main.coffee b/src/General/Main.coffee index a2699cc2e..b69bbce5c 100644 --- a/src/General/Main.coffee +++ b/src/General/Main.coffee @@ -108,6 +108,7 @@ Main = 'Polyfill': Polyfill 'Emoji': Emoji 'Style': Style + 'Mascots': MascotTools 'Rice': Rice 'Banner': Banner 'Announcements': GlobalMessage diff --git a/src/General/Settings.coffee b/src/General/Settings.coffee index d43d3438e..59029cb58 100644 --- a/src/General/Settings.coffee +++ b/src/General/Settings.coffee @@ -830,7 +830,7 @@ Settings = name = @parentElement.parentElement.id if confirm "Are you sure you want to delete \"#{name}\"?" if Conf['mascot'] is name - MascotTools.init() + MascotTools.toggle() for type in ["Enabled Mascots", "Enabled Mascots sfw", "Enabled Mascots nsfw"] Conf[type].remove name $.set type, Conf[type] @@ -868,11 +868,11 @@ Settings = select: -> if Conf[g.MASCOTSTRING].remove @id if Conf['mascot'] is @id - MascotTools.init() + MascotTools.toggle() else Conf['mascot'] = @id Conf[g.MASCOTSTRING].push @id - MascotTools.init Mascots[@id] + MascotTools.change Mascots[@id] $.toggleClass @, 'enabled' $.set g.MASCOTSTRING, Conf[g.MASCOTSTRING] diff --git a/src/Theming/Mascots.coffee b/src/Theming/Mascots.coffee index c76c8bff3..3c999c78b 100644 --- a/src/Theming/Mascots.coffee +++ b/src/Theming/Mascots.coffee @@ -1,28 +1,26 @@ MascotTools = - init: (mascot) -> - unless mascot and mascot.image - return unless Conf[g.MASCOTSTRING].length - name = Conf[g.MASCOTSTRING][Math.floor(Math.random() * Conf[g.MASCOTSTRING].length)] - mascot = Mascots[name] - Conf['mascot'] = name + init: -> + return if !Conf['Mascots'] or (g.VIEW is 'catalog' and Conf['Hide Mascots on Catalog']) - unless @el - @el = $.el 'div', - id: "mascot" - innerHTML: "" + if Conf['Click to Toggle'] + $.on @el, 'mousedown', MascotTools.click - if Conf['Click to Toggle'] - $.on @el, 'mousedown', MascotTools.click + $.on doc, 'QRDialogCreation', MascotTools.reposition - $.on doc, 'QRDialogCreation', MascotTools.reposition + $.asap (-> d.body), => + $.add d.body, @el - $.asap (-> d.body), => - $.add d.body, @el + MascotTools.toggle() + el: $.el 'div', + id: "mascot" + innerHTML: "" + + change: (mascot) -> el = @el.firstElementChild - if !Conf['Mascots'] or (Conf['Hide Mascots on Catalog'] and g.VIEW is 'catalog') - if el then el.src = "" + if !Conf['Mascots'] + el.src = "" return if Conf['Mascot Position'] is 'default' @@ -39,24 +37,34 @@ MascotTools = else unless Conf['Silhouettize Mascots'] $.rmClass doc, 'silhouettize-mascots' - unless mascot - if name and not mascot = Mascots[name] - if el then el.src = "" else null - Conf[g.MASCOTSTRING].remove name - return MascotTools.init() - image = if Array.isArray mascot.image if Style.lightTheme mascot.image[1] else mascot.image[0] - else mascot.image + else + mascot.image el.src = image Style.mascot.textContent = """<%= grunt.file.read('src/General/css/mascot.css') %>""" + toggle: -> + string = g.MASCOTSTRING + enabled = Conf[string] + return unless len = enabled.length + + name = enabled[Math.floor(Math.random() * len)] + + unless mascot = Mascots[name] + enabled.remove name + el.src = "" if el + $.set string, Conf[string] = enabled + return MascotTools.toggle() + + MascotTools.change mascot + categories: [ 'Anime' 'Ponies' @@ -125,7 +133,7 @@ MascotTools = id: "mascotConf" className: "reply dialog" innerHTML: """<%= grunt.file.read('src/General/html/Features/MascotDialog.html').replace(/>\s+<').trim() %>""" - + container = $ "#mascotcontent", dialog for name, item of layout @@ -140,7 +148,7 @@ MascotTools = if name is 'image' $.on input, 'blur', -> editMascot[@name] = @value - MascotTools.init editMascot + MascotTools.change editMascot fileInput = $.el 'input', type: "file" @@ -164,19 +172,19 @@ MascotTools = if (@value isnt "") and !/^[a-z]/i.test @value return alert "Mascot names must start with a letter." editMascot[@name] = @value - MascotTools.init editMascot + MascotTools.change editMascot else $.on input, 'blur', -> editMascot[@name] = @value - MascotTools.init editMascot + MascotTools.change editMascot when "number" div = @input item, name $.on $('input', div), 'blur', -> editMascot[@name] = parseInt @value - MascotTools.init editMascot + MascotTools.change editMascot when "select" optionHTML = "
#{item[0]}
#{item[0]}" $.on $('input', div), 'click', -> editMascot[@name] = if @checked then true else false - MascotTools.init editMascot + MascotTools.change editMascot $.add container, div - MascotTools.init editMascot + MascotTools.change editMascot $.on $('#save > a', dialog), 'click', -> MascotTools.save editMascot @@ -266,23 +274,25 @@ MascotTools = else return alert "Creation of \"#{name}\" aborted." - for type in ["Enabled Mascots", "Enabled Mascots sfw", "Enabled Mascots nsfw"] - unless Conf[type].contains name - Conf[type].push name - $.set type, Conf[type] - Mascots[name] = JSON.parse(JSON.stringify(mascot)) - Conf["mascot"] = name + Mascots[name] = JSON.parse JSON.stringify mascot delete Mascots[name].name - $.get "userMascots", {}, (item) -> - userMascots = item['userMascots'] + + $.get "userMascots", {}, ({userMascots}) -> userMascots[name] = Mascots[name] $.set 'userMascots', userMascots + + Conf["mascot"] = name + + for type in ["Enabled Mascots", "Enabled Mascots sfw", "Enabled Mascots nsfw"] + unless Conf[type].contains name + Conf[type].push name + $.set type, Conf[type] alert "Mascot \"#{name}\" saved." click: (e) -> return if e.button isnt 0 # not LMB e.preventDefault() - MascotTools.init() + MascotTools.toggle() close: -> Conf['editMode'] = false diff --git a/src/Theming/Style.coffee b/src/Theming/Style.coffee index 5af79d721..10a2dbf20 100644 --- a/src/Theming/Style.coffee +++ b/src/Theming/Style.coffee @@ -23,8 +23,6 @@ Style = hyphenated = "#{name}#{if setting[2] then " #{Conf[name]}" else ""}".toLowerCase().replace(/^4/, 'four').replace /\s+/g, '-' $.addClass doc, hyphenated - MascotTools.init() - if g.VIEW is 'index' $.asap (-> $ '.mPagelist'), ->