From 96ffcc3329df66710611934a58cb1734d576320e Mon Sep 17 00:00:00 2001 From: Anton Telesh Date: Fri, 18 Mar 2016 10:46:03 +0300 Subject: [PATCH] build 2.1.0 --- .gitignore | 3 +- dist/App.js | 133 ++++++++++++++++++++++++++++++++++++ dist/Component.js | 38 +++++++++++ dist/Model.js | 100 +++++++++++++++++++++++++++ dist/index.js | 23 +++++++ dist/symbols.js | 15 ++++ dist/utils/initComponent.js | 18 +++++ 7 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 dist/App.js create mode 100644 dist/Component.js create mode 100644 dist/Model.js create mode 100644 dist/index.js create mode 100644 dist/symbols.js create mode 100644 dist/utils/initComponent.js diff --git a/.gitignore b/.gitignore index a8022c5..9916113 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules -dist +# dist +src .DS_Store .nyc_output diff --git a/dist/App.js b/dist/App.js new file mode 100644 index 0000000..29583e6 --- /dev/null +++ b/dist/App.js @@ -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; +}(); \ No newline at end of file diff --git a/dist/Component.js b/dist/Component.js new file mode 100644 index 0000000..ee72092 --- /dev/null +++ b/dist/Component.js @@ -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; +}(); \ No newline at end of file diff --git a/dist/Model.js b/dist/Model.js new file mode 100644 index 0000000..c7cbd8a --- /dev/null +++ b/dist/Model.js @@ -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); \ No newline at end of file diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..378d4e2 --- /dev/null +++ b/dist/index.js @@ -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; + } +}); \ No newline at end of file diff --git a/dist/symbols.js b/dist/symbols.js new file mode 100644 index 0000000..09d30f9 --- /dev/null +++ b/dist/symbols.js @@ -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__'); \ No newline at end of file diff --git a/dist/utils/initComponent.js b/dist/utils/initComponent.js new file mode 100644 index 0000000..f5ceb0b --- /dev/null +++ b/dist/utils/initComponent.js @@ -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(); + } + }); +} \ No newline at end of file