From 6fb69e51ec34a306f58b5024ad8c49642c978fa8 Mon Sep 17 00:00:00 2001 From: surunzi Date: Mon, 16 May 2016 01:07:02 +0800 Subject: [PATCH] Dev: EntryBtn refactoring --- eustia/pxToNum.js | 6 ++ src/Elements/Highlight.es6 | 10 +--- src/EntryBtn/EntryBtn.es6 | 29 ++++------ src/EntryBtn/EntryBtn.scss | 4 -- src/lib/util.js | 116 ++++++++++++++++++++++++++----------- 5 files changed, 99 insertions(+), 66 deletions(-) create mode 100644 eustia/pxToNum.js diff --git a/eustia/pxToNum.js b/eustia/pxToNum.js new file mode 100644 index 0000000..158cc12 --- /dev/null +++ b/eustia/pxToNum.js @@ -0,0 +1,6 @@ +_('toNum'); + +function exports(str) +{ + return toNum(str.replace('px', '')); +} \ No newline at end of file diff --git a/src/Elements/Highlight.es6 b/src/Elements/Highlight.es6 index 03c9885..d4b810c 100644 --- a/src/Elements/Highlight.es6 +++ b/src/Elements/Highlight.es6 @@ -50,10 +50,7 @@ export default class Highlight var computedStyle = getComputedStyle(this._target, ''); - function getNumStyle(name) - { - return pxToNum(computedStyle.getPropertyValue(name)); - } + var getNumStyle = name => util.pxToNum(computedStyle.getPropertyValue(name)); var ml = getNumStyle('margin-left'), mr = getNumStyle('margin-right'), @@ -112,8 +109,3 @@ export default class Highlight this._$content = this._$el.find('.eruda-content'); } } - -function pxToNum(str) -{ - return util.toNum(str.replace('px', '')); -} \ No newline at end of file diff --git a/src/EntryBtn/EntryBtn.es6 b/src/EntryBtn/EntryBtn.es6 index 77dc017..64615df 100644 --- a/src/EntryBtn/EntryBtn.es6 +++ b/src/EntryBtn/EntryBtn.es6 @@ -4,13 +4,13 @@ import config from '../lib/config.es6' require('./EntryBtn.scss'); -export default class HomeBtn extends util.Emitter +export default class EntryBtn extends util.Emitter { constructor($parent) { super(); - this._$parent = $parent; + this._$parent = $parent; this._appendTpl(); this._makeDraggable(); this._initConfig(); @@ -22,7 +22,6 @@ export default class HomeBtn extends util.Emitter var $parent = this._$parent; $parent.append(require('./EntryBtn.hbs')()); - this._$el = $parent.find('.eruda-home-btn'); } _setPos(orientationChanged) @@ -35,10 +34,7 @@ export default class HomeBtn extends util.Emitter if (outOfRange || !cfg.get('rememberPos') || - orientationChanged) - { - pos = defPost; - } + orientationChanged) pos = defPost; this._$el.css({ left: pos.x, @@ -62,8 +58,8 @@ export default class HomeBtn extends util.Emitter if (cfg.get('rememberPos')) { cfg.set('pos', { - x: util.toNum(this._$el.css('left').replace('px', '')), - y: util.toNum(this._$el.css('top').replace('px', '')) + x: util.pxToNum(this._$el.css('left')), + y: util.pxToNum(this._$el.css('top')) }); } @@ -74,9 +70,7 @@ export default class HomeBtn extends util.Emitter } _makeDraggable() { - this._draggabilly = new Draggabilly(this._$el.get(0), { - containment: true - }); + this._draggabilly = new Draggabilly(this._$el.get(0), {containment: true}); } _initConfig() { @@ -89,13 +83,10 @@ export default class HomeBtn extends util.Emitter } }; -function getDefPos() +var getDefPos = () => { - var wh = window.innerHeight, - ww = window.innerWidth; - return { - x: ww - 50, - y: wh - 50 + x: window.innerWidth - 50, + y: window.innerHeight - 50 }; -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/EntryBtn/EntryBtn.scss b/src/EntryBtn/EntryBtn.scss index edbc9d6..7e5af79 100644 --- a/src/EntryBtn/EntryBtn.scss +++ b/src/EntryBtn/EntryBtn.scss @@ -11,10 +11,6 @@ font-size: 25px; text-align: center; line-height: 40px; - span { - position: relative; - top: 1px; - } &.active, &:active { opacity: .8; } diff --git a/src/lib/util.js b/src/lib/util.js index a85c1cf..b9039c3 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -1367,6 +1367,42 @@ module.exports = (function () * |methods|object |Public methods | * |statics|object |Static methods | * |return |function|Function used to create instances| + * + * ```javascript + * var People = Class({ + * initialize: function (name, age) + * { + * this.name = name; + * this.age = age; + * }, + * introduce: function () + * { + * return 'I am ' + this.name + ', ' + this.age + ' years old.'. + * } + * }); + * + * var Student = People.extend({ + * initialize: function (name, age, school) + * { + * this.callSuper('initialize', name, age); + * + * this.school = school. + * }, + * introduce: function () + * { + * return this.callSuper('introduce') + '\n I study at ' + this.school + '.'. + * } + * }, { + * is: function (obj) + * { + * return obj instanceof Student; + * } + * }); + * + * var a = new Student('allen', 17, 'Hogwarts'); + * a.introduce(); // -> 'I am allen, 17 years old. \n I study at Hogwarts.' + * Student.is(a); // -> true + * ``` */ var regCallSuper = /callSuper/; @@ -2345,6 +2381,52 @@ module.exports = (function () return exports; })({}); + /* ------------------------------ toNum ------------------------------ */ + + var toNum = _.toNum = (function (exports) + { + /* Convert value to a number. + * + * |Name |Type |Desc | + * |------------------------------| + * |val |* |Value to process| + * |return|number|Resulted number | + * + * ```javascript + * toNum('5'); // -> 5 + * ``` + */ + + exports = function (val) + { + if (isNum(val)) return val; + + if (isObj(val)) + { + var temp = isFn(val.valueOf) ? val.valueOf() : val; + val = isObj(temp) ? (temp + '') : temp; + } + + if (!isStr(val)) return val === 0 ? val : +val; + + return +val; + }; + + return exports; + })({}); + + /* ------------------------------ pxToNum ------------------------------ */ + + var pxToNum = _.pxToNum = (function (exports) + { + function exports(str) + { + return toNum(str.replace('px', '')); + } + + return exports; + })({}); + /* ------------------------------ rtrim ------------------------------ */ var rtrim = _.rtrim = (function (exports) @@ -2423,40 +2505,6 @@ module.exports = (function () return exports; })({}); - /* ------------------------------ toNum ------------------------------ */ - - var toNum = _.toNum = (function (exports) - { - /* Convert value to a number. - * - * |Name |Type |Desc | - * |------------------------------| - * |val |* |Value to process| - * |return|number|Resulted number | - * - * ```javascript - * toNum('5'); // -> 5 - * ``` - */ - - exports = function (val) - { - if (isNum(val)) return val; - - if (isObj(val)) - { - var temp = isFn(val.valueOf) ? val.valueOf() : val; - val = isObj(temp) ? (temp + '') : temp; - } - - if (!isStr(val)) return val === 0 ? val : +val; - - return +val; - }; - - return exports; - })({}); - /* ------------------------------ toStr ------------------------------ */ var toStr = _.toStr = (function (exports)