Skip to content
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

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions blocks-common/i-bem/__dom/_init/i-bem__dom_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Realization from bem-core#2.5.0
Copy link
Member

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)


/**
* @module i-bem__dom_init
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These style @module comments are not used in bem-bl

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found usage of @module comments in bem-bl:

./blocks-common/i-jquery/__debounce/i-jquery__debounce.js: * @module i-jquery__debounce
./blocks-common/i-jquery/__decodeuri/i-jquery__decodeuri.js: * @module i-jquery__decodeuri
./blocks-common/i-jquery/__observable/i-jquery__observable.js: * @module i-jquery__observable
./blocks-common/i-geolocation/i-geolocation.js: * @module i-geolocation
./blocks-common/i-ecma/__json/i-ecma__json.js: * @module i-ecma__json
./blocks-common/i-ecma/__function/i-ecma__function.js: * @module i-ecma__function
./blocks-common/i-ecma/__string/i-ecma__string.js: * @module i-ecma__trim
./blocks-common/i-ecma/__object/i-ecma__object.js: * @module i-ecma__object
./blocks-common/i-ecma/__array/i-ecma__array.js: * @module i-ecma__array
./blocks-common/i-menu/i-menu.js: * @module i-menu
./blocks-common/i-bem/i-bem.js: * @module i-bem
./blocks-common/i-bem/__internal/i-bem__internal.js: * @module  i-bem__internal
./blocks-common/i-bem/__dom/_init/i-bem__dom_init.js: * @module i-bem__dom_init
./blocks-common/i-bem/__dom/i-bem__dom.js: * @module i-bem__dom
./blocks-common/b-menu-vert/__trigger/b-menu-vert__trigger.js: * @module b-menu-vert
./blocks-common/i-system/i-system.js: * @module i-system


(typeof modules === 'object') && modules.define('i-bem__dom_init', ['i-bem__dom'], function(provide, BEMDOM) {

provide(
Copy link
Member

Choose a reason for hiding this comment

The 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);
});
});
6 changes: 6 additions & 0 deletions blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
({
shouldDeps : [
'next-tick',
Copy link
Member

Choose a reason for hiding this comment

The 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 ym.

Could we replace usage of next-tick block with BEM.afterCurrentEvent().

{ mod : 'init' }
]
})
27 changes: 22 additions & 5 deletions blocks-common/i-bem/__dom/_init/i-bem__dom_init_auto.js
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() {
Copy link
Member

Choose a reason for hiding this comment

The 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();
});
});
}
32 changes: 32 additions & 0 deletions blocks-common/i-bem/__dom/i-bem__dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rewrite this to the simple if statement?

Copy link
Member

Choose a reason for hiding this comment

The 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);
});
};

})();

}
92 changes: 92 additions & 0 deletions blocks-common/next-tick/next-tick.js
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One level of padding missed for this var block (and comment above)

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);
});
});