From 8eb88ffd92b9df13fd76f8bd83233d94635e1488 Mon Sep 17 00:00:00 2001 From: Vladimir Rakchaev Date: Thu, 12 Feb 2015 14:05:11 +0300 Subject: [PATCH 1/3] Added support for YModules with backward compatibility --- .../i-bem/__dom/_init/i-bem__dom_init.js | 19 ++++ .../__dom/_init/i-bem__dom_init_auto.deps.js | 6 ++ .../i-bem/__dom/_init/i-bem__dom_init_auto.js | 27 +++++- blocks-common/i-bem/__dom/i-bem__dom.js | 32 +++++++ blocks-common/next-tick/next-tick.js | 92 +++++++++++++++++++ 5 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 blocks-common/i-bem/__dom/_init/i-bem__dom_init.js create mode 100644 blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.deps.js create mode 100644 blocks-common/next-tick/next-tick.js diff --git a/blocks-common/i-bem/__dom/_init/i-bem__dom_init.js b/blocks-common/i-bem/__dom/_init/i-bem__dom_init.js new file mode 100644 index 00000000..41a07eb5 --- /dev/null +++ b/blocks-common/i-bem/__dom/_init/i-bem__dom_init.js @@ -0,0 +1,19 @@ +// Realization from bem-core#2.5.0 + +/** + * @module i-bem__dom_init + */ + +(typeof modules === 'object') && modules.define('i-bem__dom_init', ['i-bem__dom'], function(provide, BEMDOM) { + +provide( + /** + * Initializes blocks on a fragment of the DOM tree + * @exports + * @param {jQuery} [ctx=scope] Root DOM node + * @returns {jQuery} ctx Initialization context + */ + function(ctx) { + return BEMDOM.init(ctx); + }); +}); diff --git a/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.deps.js b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.deps.js new file mode 100644 index 00000000..9db5cfa8 --- /dev/null +++ b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.deps.js @@ -0,0 +1,6 @@ +({ + shouldDeps : [ + 'next-tick', + { mod : 'init' } + ] +}) diff --git a/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js index 399f10c3..c0825852 100755 --- a/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js +++ b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js @@ -1,6 +1,23 @@ -/* дефолтная инициализация */ -$(function() { - BEM.afterCurrentEvent(function() { - BEM.DOM.init(); +/** + * Auto initialization on DOM ready + */ + +// Support for YModules +if (typeof modules === 'object') { + // Realization from bem-core#2.5.0 + modules.require( + ['i-bem__dom_init', 'next-tick'], + function(init, nextTick) { + + $(function() { + nextTick(init); + }); + }); -}); +} else { + $(function() { + BEM.afterCurrentEvent(function() { + BEM.DOM.init(); + }); + }); +} diff --git a/blocks-common/i-bem/__dom/i-bem__dom.js b/blocks-common/i-bem/__dom/i-bem__dom.js index 3da34b62..3d7e2260 100644 --- a/blocks-common/i-bem/__dom/i-bem__dom.js +++ b/blocks-common/i-bem/__dom/i-bem__dom.js @@ -1715,3 +1715,35 @@ $(function() { }); })(BEM, jQuery); + + +/** + * Support for YModules + */ +if (typeof modules === 'object') { + + modules.define( + 'i-bem__dom', + function(provide) { + provide(BEM.DOM); + } + ); + + // Realization from bem-core#2.5.0 + + (function() { + + var origDefine = modules.define; + + modules.define = function(name, deps, decl) { + origDefine.apply(modules, arguments); + + name !== 'i-bem__dom_init' && arguments.length > 2 && ~deps.indexOf('i-bem__dom') && + modules.define('i-bem__dom_init', [name], function(provide, _, prev) { + provide(prev); + }); + }; + + })(); + +} diff --git a/blocks-common/next-tick/next-tick.js b/blocks-common/next-tick/next-tick.js new file mode 100644 index 00000000..e0d38a77 --- /dev/null +++ b/blocks-common/next-tick/next-tick.js @@ -0,0 +1,92 @@ +/** + * @module next-tick + * Realization from bem-core#2.5.0 + */ + +(typeof modules === 'object') && modules.define('next-tick', function(provide) { + +/** + * Executes given function on next tick. + * @exports + * @type Function + * @param {Function} fn + */ + +var global = this.global, + fns = [], + enqueueFn = function(fn) { + return fns.push(fn) === 1; + }, + callFns = function() { + var fnsToCall = fns, i = 0, len = fns.length; + fns = []; + while(i < len) { + fnsToCall[i++](); + } + }; + + /* global process */ + if(typeof process === 'object' && process.nextTick) { // nodejs + return provide(function(fn) { + enqueueFn(fn) && process.nextTick(callFns); + }); + } + + if(global.setImmediate) { // ie10 + return provide(function(fn) { + enqueueFn(fn) && global.setImmediate(callFns); + }); + } + + if(global.postMessage) { // modern browsers + var isPostMessageAsync = true; + if(global.attachEvent) { + var checkAsync = function() { + isPostMessageAsync = false; + }; + global.attachEvent('onmessage', checkAsync); + global.postMessage('__checkAsync', '*'); + global.detachEvent('onmessage', checkAsync); + } + + if(isPostMessageAsync) { + var msg = '__nextTick' + (+new Date), + onMessage = function(e) { + if(e.data === msg) { + e.stopPropagation && e.stopPropagation(); + callFns(); + } + }; + + global.addEventListener? + global.addEventListener('message', onMessage, true) : + global.attachEvent('onmessage', onMessage); + + return provide(function(fn) { + enqueueFn(fn) && global.postMessage(msg, '*'); + }); + } + } + + var doc = global.document; + if('onreadystatechange' in doc.createElement('script')) { // ie6-ie8 + var head = doc.getElementsByTagName('head')[0], + createScript = function() { + var script = doc.createElement('script'); + script.onreadystatechange = function() { + script.parentNode.removeChild(script); + script = script.onreadystatechange = null; + callFns(); + }; + head.appendChild(script); + }; + + return provide(function(fn) { + enqueueFn(fn) && createScript(); + }); + } + + provide(function(fn) { // old browsers + enqueueFn(fn) && global.setTimeout(callFns, 0); + }); +}); From c71a1d54b5aaf43b5709a5c5c056a954a69676a4 Mon Sep 17 00:00:00 2001 From: Vladimir Rakchaev Date: Thu, 12 Feb 2015 20:29:36 +0300 Subject: [PATCH 2/3] Codestyle changes --- .../i-bem/__dom/_init/i-bem__dom_init.js | 22 +++++------ .../i-bem/__dom/_init/i-bem__dom_init_auto.js | 8 +--- blocks-common/i-bem/__dom/i-bem__dom.js | 17 +++------ blocks-common/next-tick/next-tick.js | 38 +++++++++---------- 4 files changed, 38 insertions(+), 47 deletions(-) diff --git a/blocks-common/i-bem/__dom/_init/i-bem__dom_init.js b/blocks-common/i-bem/__dom/_init/i-bem__dom_init.js index 41a07eb5..d9d37926 100644 --- a/blocks-common/i-bem/__dom/_init/i-bem__dom_init.js +++ b/blocks-common/i-bem/__dom/_init/i-bem__dom_init.js @@ -1,4 +1,4 @@ -// Realization from bem-core#2.5.0 +// Implementation from bem-core#2.5.0 /** * @module i-bem__dom_init @@ -6,14 +6,14 @@ (typeof modules === 'object') && modules.define('i-bem__dom_init', ['i-bem__dom'], function(provide, BEMDOM) { -provide( - /** - * Initializes blocks on a fragment of the DOM tree - * @exports - * @param {jQuery} [ctx=scope] Root DOM node - * @returns {jQuery} ctx Initialization context - */ - function(ctx) { - return BEMDOM.init(ctx); - }); + provide( + /** + * Initializes blocks on a fragment of the DOM tree + * @exports + * @param {jQuery} [ctx=scope] Root DOM node + * @returns {jQuery} ctx Initialization context + */ + function(ctx) { + return BEMDOM.init(ctx); + }); }); diff --git a/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js index c0825852..8fe177fe 100755 --- a/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js +++ b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js @@ -4,15 +4,11 @@ // Support for YModules if (typeof modules === 'object') { - // Realization from bem-core#2.5.0 - modules.require( - ['i-bem__dom_init', 'next-tick'], - function(init, nextTick) { - + // Implementation from bem-core#2.5.0 + modules.require(['i-bem__dom_init', 'next-tick'], function(init, nextTick) { $(function() { nextTick(init); }); - }); } else { $(function() { diff --git a/blocks-common/i-bem/__dom/i-bem__dom.js b/blocks-common/i-bem/__dom/i-bem__dom.js index 3d7e2260..06a7fc04 100644 --- a/blocks-common/i-bem/__dom/i-bem__dom.js +++ b/blocks-common/i-bem/__dom/i-bem__dom.js @@ -1722,28 +1722,23 @@ $(function() { */ if (typeof modules === 'object') { - modules.define( - 'i-bem__dom', - function(provide) { - provide(BEM.DOM); - } - ); - - // Realization from bem-core#2.5.0 + modules.define('i-bem__dom', function(provide) { + provide(BEM.DOM); + }); + // Implementation from bem-core#2.5.0 (function() { - var origDefine = modules.define; modules.define = function(name, deps, decl) { origDefine.apply(modules, arguments); - name !== 'i-bem__dom_init' && arguments.length > 2 && ~deps.indexOf('i-bem__dom') && + if (name !== 'i-bem__dom_init' && arguments.length > 2 && ~deps.indexOf('i-bem__dom')) { modules.define('i-bem__dom_init', [name], function(provide, _, prev) { provide(prev); }); + } }; - })(); } diff --git a/blocks-common/next-tick/next-tick.js b/blocks-common/next-tick/next-tick.js index e0d38a77..6ed77645 100644 --- a/blocks-common/next-tick/next-tick.js +++ b/blocks-common/next-tick/next-tick.js @@ -1,29 +1,29 @@ /** * @module next-tick - * Realization from bem-core#2.5.0 + * Implementation from bem-core#2.5.0 */ (typeof modules === 'object') && modules.define('next-tick', function(provide) { -/** - * Executes given function on next tick. - * @exports - * @type Function - * @param {Function} fn - */ + /** + * Executes given function on next tick. + * @exports + * @type Function + * @param {Function} fn + */ -var global = this.global, - fns = [], - enqueueFn = function(fn) { - return fns.push(fn) === 1; - }, - callFns = function() { - var fnsToCall = fns, i = 0, len = fns.length; - fns = []; - while(i < len) { - fnsToCall[i++](); - } - }; + var global = this.global, + fns = [], + enqueueFn = function(fn) { + return fns.push(fn) === 1; + }, + callFns = function() { + var fnsToCall = fns, i = 0, len = fns.length; + fns = []; + while(i < len) { + fnsToCall[i++](); + } + }; /* global process */ if(typeof process === 'object' && process.nextTick) { // nodejs From f2bca1592447f5ab98516ca281aabc7a968534b3 Mon Sep 17 00:00:00 2001 From: Vladimir Rakchaev Date: Thu, 12 Feb 2015 21:02:15 +0300 Subject: [PATCH 3/3] Replaced next-tick with BEM.afterCurrentEvent --- .../__dom/_init/i-bem__dom_init_auto.deps.js | 1 - .../i-bem/__dom/_init/i-bem__dom_init_auto.js | 4 +- blocks-common/next-tick/next-tick.js | 92 ------------------- 3 files changed, 2 insertions(+), 95 deletions(-) delete mode 100644 blocks-common/next-tick/next-tick.js diff --git a/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.deps.js b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.deps.js index 9db5cfa8..be17cf80 100644 --- a/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.deps.js +++ b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.deps.js @@ -1,6 +1,5 @@ ({ shouldDeps : [ - 'next-tick', { mod : 'init' } ] }) diff --git a/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js index 8fe177fe..c39c8f7d 100755 --- a/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js +++ b/blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js @@ -5,9 +5,9 @@ // Support for YModules if (typeof modules === 'object') { // Implementation from bem-core#2.5.0 - modules.require(['i-bem__dom_init', 'next-tick'], function(init, nextTick) { + modules.require(['i-bem__dom_init'], function(init) { $(function() { - nextTick(init); + BEM.afterCurrentEvent(init); }); }); } else { diff --git a/blocks-common/next-tick/next-tick.js b/blocks-common/next-tick/next-tick.js deleted file mode 100644 index 6ed77645..00000000 --- a/blocks-common/next-tick/next-tick.js +++ /dev/null @@ -1,92 +0,0 @@ -/** - * @module next-tick - * Implementation from bem-core#2.5.0 - */ - -(typeof modules === 'object') && modules.define('next-tick', function(provide) { - - /** - * Executes given function on next tick. - * @exports - * @type Function - * @param {Function} fn - */ - - var global = this.global, - fns = [], - enqueueFn = function(fn) { - return fns.push(fn) === 1; - }, - callFns = function() { - var fnsToCall = fns, i = 0, len = fns.length; - fns = []; - while(i < len) { - fnsToCall[i++](); - } - }; - - /* global process */ - if(typeof process === 'object' && process.nextTick) { // nodejs - return provide(function(fn) { - enqueueFn(fn) && process.nextTick(callFns); - }); - } - - if(global.setImmediate) { // ie10 - return provide(function(fn) { - enqueueFn(fn) && global.setImmediate(callFns); - }); - } - - if(global.postMessage) { // modern browsers - var isPostMessageAsync = true; - if(global.attachEvent) { - var checkAsync = function() { - isPostMessageAsync = false; - }; - global.attachEvent('onmessage', checkAsync); - global.postMessage('__checkAsync', '*'); - global.detachEvent('onmessage', checkAsync); - } - - if(isPostMessageAsync) { - var msg = '__nextTick' + (+new Date), - onMessage = function(e) { - if(e.data === msg) { - e.stopPropagation && e.stopPropagation(); - callFns(); - } - }; - - global.addEventListener? - global.addEventListener('message', onMessage, true) : - global.attachEvent('onmessage', onMessage); - - return provide(function(fn) { - enqueueFn(fn) && global.postMessage(msg, '*'); - }); - } - } - - var doc = global.document; - if('onreadystatechange' in doc.createElement('script')) { // ie6-ie8 - var head = doc.getElementsByTagName('head')[0], - createScript = function() { - var script = doc.createElement('script'); - script.onreadystatechange = function() { - script.parentNode.removeChild(script); - script = script.onreadystatechange = null; - callFns(); - }; - head.appendChild(script); - }; - - return provide(function(fn) { - enqueueFn(fn) && createScript(); - }); - } - - provide(function(fn) { // old browsers - enqueueFn(fn) && global.setTimeout(callFns, 0); - }); -});