mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2026-02-02 09:04:34 +01:00
Moved all js file under src folder.
This commit is contained in:
80
src/js/components/controllersFeature.js
Normal file
80
src/js/components/controllersFeature.js
Normal file
@@ -0,0 +1,80 @@
|
||||
(function(app, core) {
|
||||
"use strict";
|
||||
|
||||
var should = core.should;
|
||||
|
||||
app.controller = function(name, instOrFactory) {
|
||||
should.beString(name, "name");
|
||||
if(instOrFactory == null) {
|
||||
return this.get(name);
|
||||
}
|
||||
|
||||
var reg = new core.Container.Registration(instOrFactory);
|
||||
|
||||
reg.onFirstTimeResolve = function (inst) {
|
||||
addControllerMixin(inst);
|
||||
};
|
||||
|
||||
this.set(name, reg);
|
||||
};
|
||||
|
||||
app.run(function(){
|
||||
attachControllers(app.get('rootView'), app.di);
|
||||
});
|
||||
|
||||
function addControllerMixin(ctrl) {
|
||||
ctrl.attachView = function(viewElement) {
|
||||
|
||||
this.viewElement = viewElement;
|
||||
|
||||
if(typeof ctrl.onViewAttached == 'function') {
|
||||
ctrl.onViewAttached(viewElement);
|
||||
}
|
||||
};
|
||||
|
||||
ctrl.detachView = function() {
|
||||
|
||||
this.viewElement = null;
|
||||
|
||||
if(typeof ctrl.onViewDetached == 'function') {
|
||||
ctrl.onViewDetached(viewElement);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function attachControllers(rootViewElement) {
|
||||
var elements = rootViewElement.querySelectorAll('[data-controller]'),
|
||||
i = 0, l = elements.length,
|
||||
ctrlName,
|
||||
ctrl, element;
|
||||
|
||||
for(;i<l;i++){
|
||||
element = elements[i];
|
||||
ctrlName = element.getAttribute('data-controller');
|
||||
ctrl = app.controller(ctrlName);
|
||||
|
||||
if(ctrl == null) {
|
||||
console.warn(ctrlName + ' controller wasn\'t found');
|
||||
continue;
|
||||
}
|
||||
|
||||
ctrl.attachView(element);
|
||||
|
||||
// console.log(ctrlName + ' Controller: view attached');
|
||||
|
||||
if(typeof ctrl.detachView != "function") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: get rid from closure
|
||||
element.addEventListener('DOMNodeRemoved', function (evt) {
|
||||
if(element === evt.target) {
|
||||
ctrl.detachView();
|
||||
}
|
||||
|
||||
// console.log(ctrlName + ' Controller: view detached');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
})(window.app, window.core);
|
||||
Reference in New Issue
Block a user