-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for YModules with backward compatibility #606
base: dev
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Realization from bem-core#2.5.0 | ||
|
||
/** | ||
* @module i-bem__dom_init | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These style There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've found usage of
|
||
|
||
(typeof modules === 'object') && modules.define('i-bem__dom_init', ['i-bem__dom'], function(provide, BEMDOM) { | ||
|
||
provide( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One level of padding is missed |
||
/** | ||
* 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); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
({ | ||
shouldDeps : [ | ||
'next-tick', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This dependency will add another 100 lines of js. Even for the projects that will be not using Could we replace usage of |
||
{ mod : 'init' } | ||
] | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One level of padding is missed here |
||
nextTick(init); | ||
}); | ||
|
||
}); | ||
}); | ||
} else { | ||
$(function() { | ||
BEM.afterCurrentEvent(function() { | ||
BEM.DOM.init(); | ||
}); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you rewrite this to the simple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @arikon I think it's not a good idea just because it's a 1-to-1 copy from bem-core. If these lines will change in bem-core it will be easier to move diff here. |
||
modules.define('i-bem__dom_init', [name], function(provide, _, prev) { | ||
provide(prev); | ||
}); | ||
}; | ||
|
||
})(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One level of padding missed for this |
||
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realization -> Implementation
It will be more correct (here and in the other files)