-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ad0900
commit 96ffcc3
Showing
7 changed files
with
329 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
dist | ||
# dist | ||
src | ||
.DS_Store | ||
.nyc_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.App = undefined; | ||
|
||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
|
||
var _keys2 = _interopRequireDefault(_keys); | ||
|
||
var _set = require('babel-runtime/core-js/set'); | ||
|
||
var _set2 = _interopRequireDefault(_set); | ||
|
||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
|
||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
|
||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
|
||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
|
||
var _invokeMap = require('lodash/invokeMap'); | ||
|
||
var _invokeMap2 = _interopRequireDefault(_invokeMap); | ||
|
||
var _remove = require('lodash/remove'); | ||
|
||
var _remove2 = _interopRequireDefault(_remove); | ||
|
||
var _Model = require('./Model'); | ||
|
||
var _initComponent = require('./utils/initComponent'); | ||
|
||
var _symbols = require('./symbols'); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
/** | ||
* Exposes the main public interface to manage application. | ||
*/ | ||
|
||
var App = exports.App = function () { | ||
function App() { | ||
(0, _classCallCheck3.default)(this, App); | ||
|
||
this[_symbols.COMPONENTS] = {}; | ||
this[_symbols.ELEMENTS] = new _set2.default(); | ||
this.components = []; | ||
this.data = new _Model.Model(); | ||
this.data.on('change', this.update.bind(this)); | ||
} | ||
|
||
/** | ||
* Registers application model with some predefined data | ||
* @param {any} data model data | ||
* @return {App} | ||
*/ | ||
|
||
|
||
(0, _createClass3.default)(App, [{ | ||
key: 'model', | ||
value: function model(data) { | ||
this.data.reset(data); | ||
return this; | ||
} | ||
|
||
/** | ||
* Registers component constructor bound to selector. | ||
* @param {String} selector selector to bind component instance to | ||
* @param {Function} ctor component constructor | ||
* @return {App} | ||
*/ | ||
|
||
}, { | ||
key: 'component', | ||
value: function component(selector, ctor) { | ||
this[_symbols.COMPONENTS][selector] = ctor; | ||
return this; | ||
} | ||
|
||
/** | ||
* Instantiates all registered components within element. | ||
* @param {Element} root | ||
*/ | ||
|
||
}, { | ||
key: 'bootstrap', | ||
value: function bootstrap(root) { | ||
var _this = this; | ||
|
||
(0, _keys2.default)(this[_symbols.COMPONENTS]).forEach(function (selector) { | ||
var elements = root.querySelectorAll(selector); | ||
(0, _initComponent.initComponent)(_this[_symbols.COMPONENTS][selector], elements, _this); | ||
}); | ||
} | ||
|
||
/** | ||
* Invokes all components' `update` function | ||
*/ | ||
|
||
}, { | ||
key: 'update', | ||
value: function update() { | ||
(0, _invokeMap2.default)(this.components, 'update'); | ||
} | ||
|
||
/** | ||
* Registers component instance within application. | ||
* @param {Component} component | ||
*/ | ||
|
||
}, { | ||
key: 'register', | ||
value: function register(component) { | ||
this.components.push(component); | ||
this[_symbols.ELEMENTS].add(component.element); | ||
} | ||
|
||
/** | ||
* Removes component instance from application | ||
* @param {Component} component | ||
*/ | ||
|
||
}, { | ||
key: 'destroy', | ||
value: function destroy(component) { | ||
(0, _remove2.default)(this.components, component); | ||
} | ||
}]); | ||
return App; | ||
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.Component = undefined; | ||
|
||
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck"); | ||
|
||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
|
||
var _createClass2 = require("babel-runtime/helpers/createClass"); | ||
|
||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
/** | ||
* Provides class to extend all components from | ||
*/ | ||
|
||
var Component = exports.Component = function () { | ||
function Component(element, app, model) { | ||
(0, _classCallCheck3.default)(this, Component); | ||
|
||
this.element = element; | ||
this.app = app; | ||
this.model = model; | ||
} | ||
|
||
(0, _createClass3.default)(Component, [{ | ||
key: "update", | ||
value: function update() { | ||
// implemented in concrete classes | ||
} | ||
}]); | ||
return Component; | ||
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.Model = undefined; | ||
|
||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
|
||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | ||
|
||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
|
||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
|
||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
|
||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
|
||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
|
||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
|
||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
|
||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
|
||
var _events = require('events'); | ||
|
||
var _get2 = require('lodash/get'); | ||
|
||
var _get3 = _interopRequireDefault(_get2); | ||
|
||
var _set2 = require('lodash/set'); | ||
|
||
var _set3 = _interopRequireDefault(_set2); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
/** | ||
* Exposes application model interface | ||
*/ | ||
|
||
var Model = exports.Model = function (_EventEmitter) { | ||
(0, _inherits3.default)(Model, _EventEmitter); | ||
|
||
function Model() { | ||
(0, _classCallCheck3.default)(this, Model); | ||
|
||
var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Model).call(this)); | ||
|
||
_this.params = {}; | ||
return _this; | ||
} | ||
|
||
/** | ||
* Resets all the model data and fires change event | ||
* @param {any} data | ||
* @return {Model} | ||
*/ | ||
|
||
|
||
(0, _createClass3.default)(Model, [{ | ||
key: 'reset', | ||
value: function reset(data) { | ||
this.params = data; | ||
this.emit('change'); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets some value. | ||
* @param {String} path deep path to place value in | ||
* @param {any} value | ||
* @return {Model} | ||
*/ | ||
|
||
}, { | ||
key: 'set', | ||
value: function set(path, value) { | ||
(0, _set3.default)(this.params, path, value); | ||
this.emit('change'); | ||
return this; | ||
} | ||
|
||
/** | ||
* Returns deep nested value from Model. | ||
* @param {String} path path to value | ||
* @param {any} [defaultValue] optional default value | ||
* @return {any} | ||
*/ | ||
|
||
}, { | ||
key: 'get', | ||
value: function get(path, defaultValue) { | ||
return (0, _get3.default)(this.params, path, defaultValue); | ||
} | ||
}]); | ||
return Model; | ||
}(_events.EventEmitter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _App = require('./App'); | ||
|
||
Object.defineProperty(exports, 'App', { | ||
enumerable: true, | ||
get: function get() { | ||
return _App.App; | ||
} | ||
}); | ||
|
||
var _Component = require('./Component'); | ||
|
||
Object.defineProperty(exports, 'Component', { | ||
enumerable: true, | ||
get: function get() { | ||
return _Component.Component; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.COMPONENTS = exports.ELEMENTS = undefined; | ||
|
||
var _symbol = require('babel-runtime/core-js/symbol'); | ||
|
||
var _symbol2 = _interopRequireDefault(_symbol); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
var ELEMENTS = exports.ELEMENTS = (0, _symbol2.default)('__elements__'); | ||
var COMPONENTS = exports.COMPONENTS = (0, _symbol2.default)('__components__'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.initComponent = initComponent; | ||
|
||
var _symbols = require('../symbols'); | ||
|
||
function initComponent(ctor, elements, app) { | ||
Array.prototype.forEach.call(elements, function (element) { | ||
if (!app[_symbols.ELEMENTS].has(element)) { | ||
var instance = new ctor(element, app, app.data); | ||
app.register(instance); | ||
instance.update(); | ||
} | ||
}); | ||
} |