From 22bedd63e0dcc3d0db45fc93932379708072be13 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Sat, 4 Jan 2014 14:48:54 -0700 Subject: [PATCH] Fix and optimize element rice code --- builds/appchan-x.user.js | 53 ++++++++++++++++++---------------------- builds/crx/script.js | 53 ++++++++++++++++++---------------------- src/Theming/Rice.coffee | 43 ++++++++++++-------------------- 3 files changed, 64 insertions(+), 85 deletions(-) diff --git a/builds/appchan-x.user.js b/builds/appchan-x.user.js index 56bb8966e..ee6083dab 100644 --- a/builds/appchan-x.user.js +++ b/builds/appchan-x.user.js @@ -13014,12 +13014,12 @@ select = Rice.input; container = select.nextElementSibling; container.firstChild.textContent = this.textContent; - select.value = this.getAttribute('data-value'); + select.value = this.dataset.value; $.event('change', null, select); return Rice.cleanup(); }, select: function(e) { - var clientHeight, li, nodes, option, rect, select, style, ul, _i, _len, _ref; + var bottom, clientHeight, left, li, nodes, option, select, style, top, ul, width, _i, _len, _ref, _ref1; e.stopPropagation(); e.preventDefault(); ul = Rice.ul; @@ -13032,19 +13032,19 @@ if (ul.children.length > 0) { return Rice.cleanup(); } - rect = this.getBoundingClientRect(); + _ref = this.getBoundingClientRect(), width = _ref.width, left = _ref.left, bottom = _ref.bottom, top = _ref.top; clientHeight = d.documentElement.clientHeight; style = ul.style; - style.cssText = ("width: " + rect.width + "px; left: " + rect.left + "px;") + (clientHeight - rect.bottom < 200 ? "bottom: " + (clientHeight - rect.top) + "px" : "top: " + rect.bottom + "px"); + style.cssText = ("width: " + width + "px; left: " + left + "px;") + (clientHeight - bottom < 200 ? "bottom: " + (clientHeight - top) + "px" : "top: " + bottom + "px"); Rice.input = select = this.previousSibling; nodes = []; - _ref = select.options; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - option = _ref[_i]; + _ref1 = select.options; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + option = _ref1[_i]; li = $.el('li', { textContent: option.textContent }); - li.setAttribute('data-value', option.value); + li.dataset.value = option.value; $.on(li, 'click', Rice.cb.option); nodes.push(li); } @@ -13056,29 +13056,24 @@ } }, cleanup: function() { - var child, _i, _len, _ref; $.off(d, 'click scroll blur resize', Rice.cleanup); - _ref = __slice.call(Rice.ul.children); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - child = _ref[_i]; - $.rm(child); - } + $.rmAll(Rice.ul); }, nodes: function(root) { - var checkboxes, checkrice, input, select, selectrice, selects, _i, _j, _len, _len1; + var fn; root || (root = d.body); - checkboxes = $$('[type=checkbox]:not(.riced)', root); - checkrice = Rice.checkbox; - for (_i = 0, _len = checkboxes.length; _i < _len; _i++) { - input = checkboxes[_i]; - checkrice(input); - } - selects = $$('select:not(.riced)', root); - selectrice = Rice.select; - for (_j = 0, _len1 = selects.length; _j < _len1; _j++) { - select = selects[_j]; - selectrice(select); - } + fn = function(items, type) { + var func, item, _i, _len, _results; + func = Rice[type]; + _results = []; + for (_i = 0, _len = items.length; _i < _len; _i++) { + item = items[_i]; + _results.push(func(item)); + } + return _results; + }; + fn($$('[type=checkbox]:not(.riced)', root), 'checkbox'); + return fn($$('select:not(.riced)', root), 'select'); }, node: function() { return Rice.checkbox($('.postInfo input', this.nodes.post)); @@ -13097,11 +13092,11 @@ return $.on(div, 'click', Rice.cb.check); }, select: function(select) { - var div, _ref; + var div; $.addClass(select, 'riced'); div = $.el('div', { className: 'selectrice', - innerHTML: "
" + (((_ref = select.options[select.selectedIndex || 0]) != null ? _ref.textContent : void 0) || null) + "
" + innerHTML: "
" + (select.options[select.selectedIndex || '0'].textContent || '') + "
" }); $.on(div, "click", Rice.cb.select); return $.after(select, div); diff --git a/builds/crx/script.js b/builds/crx/script.js index 745c3ffb0..6d80bdae6 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -13019,12 +13019,12 @@ select = Rice.input; container = select.nextElementSibling; container.firstChild.textContent = this.textContent; - select.value = this.getAttribute('data-value'); + select.value = this.dataset.value; $.event('change', null, select); return Rice.cleanup(); }, select: function(e) { - var clientHeight, li, nodes, option, rect, select, style, ul, _i, _len, _ref; + var bottom, clientHeight, left, li, nodes, option, select, style, top, ul, width, _i, _len, _ref, _ref1; e.stopPropagation(); e.preventDefault(); ul = Rice.ul; @@ -13037,19 +13037,19 @@ if (ul.children.length > 0) { return Rice.cleanup(); } - rect = this.getBoundingClientRect(); + _ref = this.getBoundingClientRect(), width = _ref.width, left = _ref.left, bottom = _ref.bottom, top = _ref.top; clientHeight = d.documentElement.clientHeight; style = ul.style; - style.cssText = ("width: " + rect.width + "px; left: " + rect.left + "px;") + (clientHeight - rect.bottom < 200 ? "bottom: " + (clientHeight - rect.top) + "px" : "top: " + rect.bottom + "px"); + style.cssText = ("width: " + width + "px; left: " + left + "px;") + (clientHeight - bottom < 200 ? "bottom: " + (clientHeight - top) + "px" : "top: " + bottom + "px"); Rice.input = select = this.previousSibling; nodes = []; - _ref = select.options; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - option = _ref[_i]; + _ref1 = select.options; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + option = _ref1[_i]; li = $.el('li', { textContent: option.textContent }); - li.setAttribute('data-value', option.value); + li.dataset.value = option.value; $.on(li, 'click', Rice.cb.option); nodes.push(li); } @@ -13061,29 +13061,24 @@ } }, cleanup: function() { - var child, _i, _len, _ref; $.off(d, 'click scroll blur resize', Rice.cleanup); - _ref = __slice.call(Rice.ul.children); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - child = _ref[_i]; - $.rm(child); - } + $.rmAll(Rice.ul); }, nodes: function(root) { - var checkboxes, checkrice, input, select, selectrice, selects, _i, _j, _len, _len1; + var fn; root || (root = d.body); - checkboxes = $$('[type=checkbox]:not(.riced)', root); - checkrice = Rice.checkbox; - for (_i = 0, _len = checkboxes.length; _i < _len; _i++) { - input = checkboxes[_i]; - checkrice(input); - } - selects = $$('select:not(.riced)', root); - selectrice = Rice.select; - for (_j = 0, _len1 = selects.length; _j < _len1; _j++) { - select = selects[_j]; - selectrice(select); - } + fn = function(items, type) { + var func, item, _i, _len, _results; + func = Rice[type]; + _results = []; + for (_i = 0, _len = items.length; _i < _len; _i++) { + item = items[_i]; + _results.push(func(item)); + } + return _results; + }; + fn($$('[type=checkbox]:not(.riced)', root), 'checkbox'); + return fn($$('select:not(.riced)', root), 'select'); }, node: function() { return Rice.checkbox($('.postInfo input', this.nodes.post)); @@ -13102,11 +13097,11 @@ return $.on(div, 'click', Rice.cb.check); }, select: function(select) { - var div, _ref; + var div; $.addClass(select, 'riced'); div = $.el('div', { className: 'selectrice', - innerHTML: "
" + (((_ref = select.options[select.selectedIndex || 0]) != null ? _ref.textContent : void 0) || null) + "
" + innerHTML: "
" + (select.options[select.selectedIndex || '0'].textContent || '') + "
" }); $.on(div, "click", Rice.cb.select); return $.after(select, div); diff --git a/src/Theming/Rice.coffee b/src/Theming/Rice.coffee index f7a62b6db..07a105137 100644 --- a/src/Theming/Rice.coffee +++ b/src/Theming/Rice.coffee @@ -19,7 +19,7 @@ Rice = select = Rice.input container = select.nextElementSibling container.firstChild.textContent = @textContent - select.value = @getAttribute 'data-value' + select.value = @dataset.value $.event 'change', null, select Rice.cleanup() @@ -30,28 +30,26 @@ Rice = {ul} = Rice unless ul - Rice.ul = ul = $.el 'ul', - id: "selectrice" + Rice.ul = ul = $.el 'ul', id: "selectrice" $.add d.body, ul if ul.children.length > 0 return Rice.cleanup() - rect = @getBoundingClientRect() + {width, left, bottom, top} = @getBoundingClientRect() {clientHeight} = d.documentElement {style} = ul - style.cssText = "width: #{rect.width}px; left: #{rect.left}px;" + (if clientHeight - rect.bottom < 200 then "bottom: #{clientHeight - rect.top}px" else "top: #{rect.bottom}px") + style.cssText = "width: #{width}px; left: #{left}px;" + (if clientHeight - bottom < 200 then "bottom: #{clientHeight - top}px" else "top: #{bottom}px") Rice.input = select = @previousSibling + nodes = [] - for option in select.options - li = $.el 'li', - textContent: option.textContent - li.setAttribute 'data-value', option.value - + li = $.el 'li', textContent: option.textContent + li.dataset.value = option.value $.on li, 'click', Rice.cb.option nodes.push li + $.add ul, nodes $.on ul, 'click scroll blur', (e) -> @@ -61,26 +59,18 @@ Rice = cleanup: -> $.off d, 'click scroll blur resize', Rice.cleanup - for child in [Rice.ul.children...] - $.rm child + $.rmAll Rice.ul return nodes: (root) -> root or= d.body - checkboxes = $$('[type=checkbox]:not(.riced)', root) - checkrice = Rice.checkbox + fn = (items, type) -> + func = Rice[type] + func item for item in items - for input in checkboxes - checkrice input - - selects = $$('select:not(.riced)', root) - selectrice = Rice.select - - for select in selects - selectrice select - - return + fn $$('[type=checkbox]:not(.riced)', root), 'checkbox' + fn $$('select:not(.riced)', root), 'select' node: -> Rice.checkbox $ '.postInfo input', @nodes.post @@ -88,8 +78,7 @@ Rice = checkbox: (input) -> return if $.hasClass input, 'riced' $.addClass input, 'riced' - div = $.el 'div', - className: 'rice' + div = $.el 'div', className: 'rice' div.check = input $.after input, div $.on div, 'click', Rice.cb.check @@ -98,7 +87,7 @@ Rice = $.addClass select, 'riced' div = $.el 'div', className: 'selectrice' - innerHTML: "
#{select.options[select.selectedIndex or 0]?.textContent or null}
" + innerHTML: "
#{select.options[select.selectedIndex or '0'].textContent or ''}
" $.on div, "click", Rice.cb.select $.after select, div \ No newline at end of file