diff --git a/.gitignore b/.gitignore index 9523636..6956a78 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules .DS_Store bin +dist/ diff --git a/.travis.yml b/.travis.yml index 2c52908..d717030 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,9 @@ language: node_js node_js: - - "0.10" -before_install: -- npm install -g grunt-cli grunt-contrib-jshint grunt-contrib-jasmine + - "node" notifications: email: false +jobs: + include: + - stage: buildtest + script: npm run bundle && npm t diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100644 index 36f11c3..0000000 --- a/Gruntfile.js +++ /dev/null @@ -1,184 +0,0 @@ -module.exports = function(grunt) { - 'use strict'; - - var app = {}, - config = {}, - tasks = [ - 'grunt-contrib-jshint', - 'grunt-contrib-concat', - 'grunt-contrib-jasmine', - 'grunt-contrib-watch', - 'grunt-contrib-uglify', - 'grunt-coveralls', - 'grunt-bump', - 'grunt-umd' - ]; - - // get patch if it's release - app.patch = grunt.option('patch'); - - // config pack - app.pack = grunt.config('pkg', grunt.file.readJSON('package.json')); - - // really?? I mean, really???? - function updateBanner() { - app.banner = '/** ' + - '\n* ' + app.pack.name + ' -v' + grunt.file.readJSON('package.json').version + - '\n* Copyright (c) '+ grunt.template.today('yyyy') + ' ' + app.pack.author + - '\n* Licensed ' + app.pack.license + '\n*/\n\n'; - - config.concat.options.banner = app.banner; - config.uglify.all.options.banner = app.banner; - }; - - // ============================================= - // bump - config.bump = {}; - config.bump.options = { - files: ['package.json'], - updateConfigs: [], - commit: true, - commitMessage: 'Release v%VERSION%', - commitFiles: [ - 'package.json', - 'dist' - ], - createTag: true, - tagName: 'v%VERSION%', - tagMessage: 'Version %VERSION%', - push: true, - pushTo: 'https://github.com/msodeveloper/core.js.git', - gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d' - }; - - // ============================================= - // jshint - config.jshint = {}; - config.jshint.options = { - debug: true, - sub: true - }; - config.jshint.all = ['dist/core.js']; - - // ============================================= - // concat - config.concat = { - options: { - banner: app.banner - }, - dist: { - src: [ - 'src/core/core.js', - 'src/helpers/*.js', - 'src/core/**/*.js', - 'src/sandbox/sandbox.js' - ], - dest: 'dist/core.js' - } - }; - - // ============================================= - // watch - config.watch = {}; - config.watch.scripts = { - files: ['src/**/*.js'], - tasks: ['concat', 'umd','jshint'], - options: { - spawn: false, - } - } - - // ============================================= - // uglify - config.uglify = {}; - config.uglify.all = { - files: { - 'dist/core.min.js': [ 'dist/core.js' ] - }, - options: { - preserveComments: false, - sourceMap: 'dist/core.min.map', - sourceMappingURL: 'core.min.map', - report: 'min', - beautify: { - ascii_only: true - }, - banner: app.banner, - compress: { - hoist_funs: false, - loops: false, - unused: false - } - } - } - - // ============================================= - // jasmine - config.jasmine = {}; - config.jasmine.coverage = { - src: [ - 'dist/core.js' - ], - options: { - specs: 'tests/**/*Spec.js', - template: require('grunt-template-jasmine-istanbul'), - templateOptions: { - coverage: 'bin/coverage/coverage.json', - report: { - type: 'lcov', - options: { - dir: 'bin/coverage' - } - }, - thresholds: { - lines: 75, - statements: 75, - branches: 75, - functions: 90 - } - } - } - } - - // ============================================= - // umd - config.umd = {}; - config.umd = { - all: { - options: { - src: 'dist/core.js', - objectToExport: 'Core' - } - } - }; - - // ============================================= - // coveralls - config.coveralls = { - src: 'bin/coverage/lcov.info' - }; - - updateBanner(); - - // Load all tasks - tasks.forEach(grunt.loadNpmTasks); - - // config - grunt.initConfig(config); - - - grunt.registerTask('dist', ['jshint', 'concat', 'umd', 'jasmine', 'uglify']); - - grunt.registerTask('ci', ['jshint', 'jasmine', 'coveralls']); - - grunt.registerTask('release', function () { - grunt.task.run('bump-only%patch%'.replace('%patch%', app.patch ? ':' + app.patch : '')); - setTimeout(function() { - updateBanner(); - grunt.task.run('dist'); - grunt.task.run('bump-commit'); - }, 0); - - // grunt.task.run('dist'); - }); -}; diff --git a/README.md b/README.md index 11de575..2b1c865 100644 --- a/README.md +++ b/README.md @@ -155,20 +155,14 @@ Core.start('tweet'); // Log:
(DOM Reference) If there's no DOM element, then `this.el` will return `null`. -### Extending Core - -__Core.js__ simple gives you an structure to scale your apps, but it won't give you the tools to build it, since we don't want to reinvent the wheel, it provides a way to extend its functionalities. - -Modules should not talk to external libreries as well, they will ask permission to `sandbox` before that, and `sandbox` will then talk to `Core` to check if that extension actually exists, let's see: - ```js // lets suppose we have jquery loaded before this -Core.extend('$', jQuery); + Core.register('tweet', function(sandbox) { return { init: function() { - sandbox.use('$')('#tweet').on('click', this.newTweet); + jQuery('#tweet').on('click', this.newTweet); }, newTweet: function() { @@ -178,9 +172,9 @@ Core.register('tweet', function(sandbox) { }); ``` -Using the method `use` from `sandbox`, it gives you access to all extensions from Core, without talking directly to it. -You might think: _"Why do that? it's only increasing the code"_. But since we are talking about consistency, and maybe a code that will be updated by other programmers, this is a way we can keep things standardized, and again, conpectually a module should not talk to anything else but the `sandbox`. + +A module should not talk to other modules directly anything else but the `sandbox`. ### Last thoughts @@ -241,18 +235,6 @@ __Usage__ Core.stopAll(); ``` -#### Core.extend( newExtension, implementation ) -Extends Core functionalities - -- `newExtension` (string): The name of the extension -- `implementation` (function | string | number | boolean | array): The implementation of the extension - -__Usage__ - -```js -Core.extend('$', jQuery); -``` - #### sandbox.listen( notification, callback, context, force ) Listens to other modules notifications, to overwrite a notification you must use the parameter force @@ -268,10 +250,6 @@ Notifies other modules - `type` (string): The notification that will be triggered - `data` (function | string | number | boolean | array): The data that will be passed in the callback -#### sandbox.use( extension ) -Calls the extension from core, if there's any - -- `extension` (string): The name of the extension ## Maintainer diff --git a/dist/core.js b/dist/core.js deleted file mode 100644 index 2c6b439..0000000 --- a/dist/core.js +++ /dev/null @@ -1,347 +0,0 @@ -(function (root, factory) { - if (typeof define === 'function' && define.amd) { - // AMD. Register as an anonymous module unless amdModuleId is set - define([], function () { - return (root['Core'] = factory()); - }); - } else if (typeof exports === 'object') { - // Node. Does not work with strict CommonJS, but - // only CommonJS-like environments that support module.exports, - // like Node. - module.exports = factory(); - } else { - root['Core'] = factory(); - } -}(this, function () { - -/** -* core.js -v0.7.3 -* Copyright (c) 2015 Mauricio Soares -* Licensed MIT -*/ - -'use strict'; - -/** -* The constructor of Core -* -* @class Core -* @constructor -*/ -var Core = function() { - this.modules = {}; -}; - -/** -* Registers a new module -* -* @method register -* @param {string} module the name of the new module -* @param {function} constructor the constructor of the new module -*/ -Core.prototype.register = function(module, constructor) { - if(this.modules[module]) { - this.helpers.err('!!module', module); - return false; - } - this.modules[module] = { - constructor: constructor, - instance: null - }; -}; - -/** -* Check if the module is already initialized or not -* -* @method moduleCheck -* @param {string} module the name of the module that will be checked -* @param {boolean} destroy check if the module exists, but is already destroyed -* @return {boolean} if the module exists or already have an instance -*/ -Core.prototype.moduleCheck = function(module, destroy) { - if(destroy) return !module || !module.instance; - - return !module || module.instance; -}; - -/** -* Gets an element by ID to attach to the module instance -* -* @method getElement -* @param {string} id the id of the main element in the module -*/ -Core.prototype.getElement = function(id) { - var el = document.getElementById(id); - - // this fixes some blackberry, opera and IE possible bugs - return (el && el.id === id && el.parentElement) ? el : null; -}; - -/** -* Starts a registered module, if no module is passed, it starts all modules -* -* @method start -* @param {string} module the name of the module -*/ -Core.prototype.start = function(module) { - if(!module) return this.startAll(); - - var cModule = this.modules[module], - el = this.getElement(module); - - if(this.moduleCheck(cModule)) { - this.helpers.err('!start', module); - return false; - } - - cModule.instance = new cModule.constructor(new this.Sandbox(module)); - - // attachs the element to the instance of the module - cModule.instance.el = el; - - if(cModule.instance.init) return cModule.instance.init(); -}; - -/** -* Stops a registered module -* -* @method start -* @param {string} module the name of the module -*/ -Core.prototype.stop = function(module) { - if(!module) return this.stopAll(); - - var cModule = this.modules[module], stopReturn; - - if(this.moduleCheck(cModule, true)) { - this.helpers.err('!stop', module); - return false; - } - - if(cModule.instance.destroy) stopReturn = cModule.instance.destroy(); - - cModule.instance = null; - - this.Sandbox.clearNotifications(module); - - return stopReturn; -}; - -/** -* Stop all started modules -* -* @method stopAll -*/ -Core.prototype.stopAll = function() { - this.xAll('stop'); -}; - -/** -* Stop all started modules -* -* @method stopAll -*/ -Core.prototype.startAll = function() { - this.xAll('start'); -}; - -/** -* Helper for startAll and stopAll -* -* @method xAll -* @param {string} method the method that will be triggered -*/ -Core.prototype.xAll = function(method) { - for(var module in this.modules) { - if(this.modules.hasOwnProperty(module)) this[method](module); - } -}; - -Core = new Core(); - -/** -* Handles error messages -* -* @method err -* @param {string} error the type of the error -* @param {function} message the complementary message to the error -*/ -var err = function(error, message) { - Core.helpers.log(err.messages[error] + "\"" + message + "\""); -}; - -err.messages = { - '!start': 'Could not start the given module, it\'s either already started or is not registered: ', - '!stop': 'Could not stop the given module, it\'s either already stopped or is not registered: ', - '!!module': 'Can\'t register an already registered module: ', - '!!listen': 'There\'s already an listen handler to the notification: ' -}; - -Core.helpers = Core.helpers || {}; -Core.helpers.err = err; - -/** -* Returns if the object is an array -* -* @method isArray -* @param {object} obj the object that will be checked -* @return {boolean} if its an array or not -*/ -var isArray = function(obj) { - return Object.prototype.toString.call(obj) === '[object Array]'; -}; - -Core.helpers = Core.helpers || {}; -Core.helpers.isArray = isArray; - -/** -* Adds console.log to Core helpers -* -* @method log -*/ -var log = (window.console) ? window.console.log.bind(window.console) : function() {}; - -Core.helpers = Core.helpers || {}; -Core.helpers.log = log; - -/** -* Returns an array-like to array -* -* @method toArray -* @param {object} obj The arraylike that will be converted -* @return {array} the converted arraylike -*/ -var toArray = function(obj) { - return Array.prototype.slice.call(obj); -}; - -Core.helpers = Core.helpers || {}; -Core.helpers.toArray = toArray; - -var extensions = {}; - -/** -* Extends core functionalities -* -* @method extend -* @param {string} name the name of the extension -* @param {function | array | boolean | string | number} implementation what the extension does -*/ -var extend = function(name, implementation) { - extensions[name] = implementation; -}; - -/** -* returns the extension -* -* @method getExtension -* @param {string} extension the name of the extension -* @return {function | array | boolean | string | number} the implementation of the extension -*/ -var getExtension = function(extension) { - return extensions[extension] || null; -}; - -Core.extend = extend; -Core.getExtension = getExtension; - -/** -* The constructor of Sandbox -* -* @class Sandbox -* @constructor -*/ -var Sandbox = function(module) { - this.module = module; -}; - -// All notifications from sandbox -Sandbox.notifications = {}; - -/** -* Clear all notifications from an specific module -* -* @method clearNotifications -* @param {string} module the name of the module -*/ -Sandbox.clearNotifications = function(module) { - delete Sandbox.notifications[module]; -}; - -/** -* Notifies other modules from an specific notification -* -* @method notify -* @param {object} notification the object with notifications configs -*/ -Sandbox.prototype.notify = function(notification) { - for(var module in Sandbox.notifications) { - var listening = Sandbox.notifications[module][notification.type]; - if(listening) { - listening.callback.call(listening.context, notification.data); - } - } -}; - -/** -* Makes a module listen to an specific notification -* -* @method listen -* @param {string | array} notification the notification that the module will be listening to -*/ -Sandbox.prototype.listen = function(notification) { - var args = Core.helpers.toArray(arguments); - if(!Core.helpers.isArray(notification)) return this.addNotification.apply(this, arguments); - - for(var i = 0, len = notification.length; i < len; i += 1) { - args[0] = notification[i]; - this.addNotification.apply(this, args); - } -}; - -/** -* Adds the module listener to the notifications configuration -* -* @method addNotification -* @param {string} notification the name of the notification -* @param {function} callback the callback that will be triggered when the notification is called -* @param {object} context the value of "this" -* @param {boolean} replace if the notification already exists, it forces to rewrite it -*/ -Sandbox.prototype.addNotification = function(notification, callback, context, replace) { - var notifications = Sandbox.notifications, - addNotification = false; - - if(!notifications[this.module] || !notifications[this.module][notification]) { - addNotification = true; - } else if(replace) { - addNotification = true; - } else { - Core.helpers.err('!!listen', notification); - } - - if(addNotification) { - notifications[this.module] = notifications[this.module] || {}; - notifications[this.module][notification] = { - callback: callback, - context: context || window - }; - } -}; - -/** -* Returns an extension from Core -* -* @method x -* @param {string} extension the name of the extension -* @return {function | array | boolean | string | number} the implementation of the extension -*/ -Sandbox.prototype.use = function(extension) { - return Core.getExtension(extension); -}; - -Core.Sandbox = Sandbox; - -return Core; - -})); diff --git a/dist/core.min.js b/dist/core.min.js deleted file mode 100644 index 1d94a12..0000000 --- a/dist/core.min.js +++ /dev/null @@ -1,9 +0,0 @@ -/** -* core.js -v0.7.3 -* Copyright (c) 2015 Mauricio Soares -* Licensed MIT -*/ - - -!function(a,b){"function"==typeof define&&define.amd?define([],function(){return a.Core=b()}):"object"==typeof exports?module.exports=b():a.Core=b()}(this,function(){"use strict";var a=function(){this.modules={}};a.prototype.register=function(a,b){return this.modules[a]?(this.helpers.err("!!module",a),!1):void(this.modules[a]={constructor:b,instance:null})},a.prototype.moduleCheck=function(a,b){return b?!a||!a.instance:!a||a.instance},a.prototype.getElement=function(a){var b=document.getElementById(a);return b&&b.id===a&&b.parentElement?b:null},a.prototype.start=function(a){if(!a)return this.startAll();var b=this.modules[a],c=this.getElement(a);return this.moduleCheck(b)?(this.helpers.err("!start",a),!1):(b.instance=new b.constructor(new this.Sandbox(a)),b.instance.el=c,b.instance.init?b.instance.init():void 0)},a.prototype.stop=function(a){if(!a)return this.stopAll();var b=this.modules[a],c;return this.moduleCheck(b,!0)?(this.helpers.err("!stop",a),!1):(b.instance.destroy&&(c=b.instance.destroy()),b.instance=null,this.Sandbox.clearNotifications(a),c)},a.prototype.stopAll=function(){this.xAll("stop")},a.prototype.startAll=function(){this.xAll("start")},a.prototype.xAll=function(a){for(var b in this.modules)this.modules.hasOwnProperty(b)&&this[a](b)},a=new a;var b=function(c,d){a.helpers.log(b.messages[c]+'"'+d+'"')};b.messages={"!start":"Could not start the given module, it's either already started or is not registered: ","!stop":"Could not stop the given module, it's either already stopped or is not registered: ","!!module":"Can't register an already registered module: ","!!listen":"There's already an listen handler to the notification: "},a.helpers=a.helpers||{},a.helpers.err=b;var c=function(a){return"[object Array]"===Object.prototype.toString.call(a)};a.helpers=a.helpers||{},a.helpers.isArray=c;var d=window.console?window.console.log.bind(window.console):function(){};a.helpers=a.helpers||{},a.helpers.log=d;var e=function(a){return Array.prototype.slice.call(a)};a.helpers=a.helpers||{},a.helpers.toArray=e;var f={},g=function(a,b){f[a]=b},h=function(a){return f[a]||null};a.extend=g,a.getExtension=h;var i=function(a){this.module=a};return i.notifications={},i.clearNotifications=function(a){delete i.notifications[a]},i.prototype.notify=function(a){for(var b in i.notifications){var c=i.notifications[b][a.type];c&&c.callback.call(c.context,a.data)}},i.prototype.listen=function(b){var c=a.helpers.toArray(arguments);if(!a.helpers.isArray(b))return this.addNotification.apply(this,arguments);for(var d=0,e=b.length;e>d;d+=1)c[0]=b[d],this.addNotification.apply(this,c)},i.prototype.addNotification=function(b,c,d,e){var f=i.notifications,g=!1;f[this.module]&&f[this.module][b]?e?g=!0:a.helpers.err("!!listen",b):g=!0,g&&(f[this.module]=f[this.module]||{},f[this.module][b]={callback:c,context:d||window})},i.prototype.use=function(b){return a.getExtension(b)},a.Sandbox=i,a}); -//# sourceMappingURL=core.min.js.map \ No newline at end of file diff --git a/dist/core.min.js.map b/dist/core.min.js.map deleted file mode 100644 index ec9d42e..0000000 --- a/dist/core.min.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"core.min.js","sources":["core.js"],"names":["root","factory","define","amd","exports","module","this","Core","modules","prototype","register","constructor","helpers","err","instance","moduleCheck","destroy","getElement","id","el","document","getElementById","parentElement","start","startAll","cModule","Sandbox","init","stop","stopAll","stopReturn","clearNotifications","xAll","method","hasOwnProperty","error","message","log","messages","!start","!stop","!!module","!!listen","isArray","obj","Object","toString","call","window","console","bind","toArray","Array","slice","extensions","extend","name","implementation","getExtension","extension","notifications","notify","notification","listening","type","callback","context","data","listen","args","arguments","addNotification","apply","i","len","length","replace","use"],"mappings":"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"} \ No newline at end of file diff --git a/example/index.html b/example/index.html index 696058f..5212cf4 100644 --- a/example/index.html +++ b/example/index.html @@ -18,83 +18,9 @@ - - - + + diff --git a/example/main.js b/example/main.js index 8f10b0b..e73bac0 100644 --- a/example/main.js +++ b/example/main.js @@ -1,72 +1,8 @@ -// extending stuff in core -Core.extend('$', jQuery); +import {Core} from "../src/core/core.js"; +import "./tweet-form.js"; +import "./tweet-list.js"; -// tweet-form.js -(function(Core) { - Core.register('tweet-form', function(sandbox) { - return { - init: function() { - this.$form = sandbox.x('$')('#tweet-form'); - this.$input = this.$form.find('input'); - this.addListeners(); - }, - - addListeners: function() { - this.$form.on('submit', this.onSubmit.bind(this)); - }, - - onSubmit: function(e) { - e.preventDefault(); - - var newTweet = this.$input[0].value; - this.$input[0].value = ''; - - this.notify(newTweet); - }, - - notify: function(tweet) { - sandbox.notify({ - type: 'new-tweet', - data: { - tweet: tweet, - author: '@omauriciosoares' - } - }); - } - } - }); -} (Core)); - -// tweet-list.js -(function(Core) { - Core.register('tweet-list', function(sandbox) { - return { - init: function() { - this.$list = sandbox.x('$')('#tweet-list'); - - this.listen(); - }, - - listen: function() { - sandbox.listen('new-tweet', this.newTweet, this); - }, - - newTweet: function(data) { - var newTweetHtml = this.getHtml(data); - - this.$list.prepend(newTweetHtml); - }, - - getHtml: function(data) { - var li = sandbox.x('$')('
  • '); - li.append(data.author + '
    ' + data.tweet); - - return li; - } - } - }); -} (Core)); // boot.js Core.start('tweet-form'); diff --git a/example/tweet-form.js b/example/tweet-form.js new file mode 100644 index 0000000..242e3ac --- /dev/null +++ b/example/tweet-form.js @@ -0,0 +1,36 @@ +import {Core} from "../src/core/core.js"; + +// tweet-form.js +Core.register('tweet-form', function(sandbox) { + return { + init: function() { + this.$form = $('#tweet-form'); + this.$input = this.$form.find('input'); + + this.addListeners(); + }, + + addListeners: function() { + this.$form.on('submit', this.onSubmit.bind(this)); + }, + + onSubmit: function(e) { + e.preventDefault(); + + var newTweet = this.$input[0].value; + this.$input[0].value = ''; + + this.notify(newTweet); + }, + + notify: function(tweet) { + sandbox.notify({ + type: 'new-tweet', + data: { + tweet: tweet, + author: '@omauriciosoares' + } + }); + } + } +}); diff --git a/example/tweet-list.js b/example/tweet-list.js new file mode 100644 index 0000000..0b8df3c --- /dev/null +++ b/example/tweet-list.js @@ -0,0 +1,33 @@ +import {Core} from "../src/core/core.js"; + +// tweet-list.js +Core.register('tweet-list', function(sandbox) { + return { + init: function() { + this.$list = $('#tweet-list'); + + this.listen(); + }, + + listen: function() { + sandbox.listen('new-tweet', this.newTweet, this); + }, + + newTweet: function(data) { + var newTweetHtml = this.getHtml(data); + + this.$list.prepend(newTweetHtml); + }, + + getHtml: function(data) { + var li = $( + `
  • + ${data.author}
    + ${data.tweet} +
  • `); + li.append(); + + return li; + } + } +}); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..2e5ddfd --- /dev/null +++ b/package-lock.json @@ -0,0 +1,134 @@ +{ + "name": "core.js", + "version": "0.8.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@types/estree": { + "version": "0.0.39", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz", + "integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==", + "dev": true + }, + "@types/node": { + "version": "10.11.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.11.7.tgz", + "integrity": "sha512-yOxFfkN9xUFLyvWaeYj90mlqTJ41CsQzWKS3gXdOMOyPVacUsymejKxJ4/pMW7exouubuEeZLJawGgcNGYlTeg==", + "dev": true + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "glob": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", + "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "dev": true + }, + "jasmine": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/jasmine/-/jasmine-3.2.0.tgz", + "integrity": "sha512-qv6TZ32r+slrQz8fbx2EhGbD9zlJo3NwPrpLK1nE8inILtZO9Fap52pyHk7mNTh4tG50a+1+tOiWVT3jO5I0Sg==", + "dev": true, + "requires": { + "glob": "^7.0.6", + "jasmine-core": "~3.2.0" + } + }, + "jasmine-core": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.2.1.tgz", + "integrity": "sha512-pa9tbBWgU0EE4SWgc85T4sa886ufuQdsgruQANhECYjwqgV4z7Vw/499aCaP8ZH79JDS4vhm8doDG9HO4+e4sA==", + "dev": true + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "rollup": { + "version": "0.66.6", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-0.66.6.tgz", + "integrity": "sha512-J7/SWanrcb83vfIHqa8+aVVGzy457GcjA6GVZEnD0x2u4OnOd0Q1pCrEoNe8yLwM6z6LZP02zBT2uW0yh5TqOw==", + "dev": true, + "requires": { + "@types/estree": "0.0.39", + "@types/node": "*" + } + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + } + } +} diff --git a/package.json b/package.json index 07a1608..665f601 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "core.js", - "version": "0.7.3", + "version": "0.8.0", "description": "Lightweight framework for scalable applications", "author": "Mauricio Soares", "homepage": "https://github.com/msodeveloper/core.js", @@ -11,7 +11,8 @@ }, "main": "dist/core.js", "scripts": { - "test": "grunt ci" + "bundle": "rollup --config tools/rollup.config.js", + "test": "jasmine --config=tests/jasmine.json" }, "bugs": { "url": "https://github.com/msodeveloper/core.js/issues" @@ -23,15 +24,7 @@ "scalable" ], "devDependencies": { - "grunt": "^0.4.5", - "grunt-bump": "^0.3.0", - "grunt-contrib-concat": "^0.5.0", - "grunt-contrib-jasmine": "^0.8.0", - "grunt-contrib-jshint": "^0.10.0", - "grunt-contrib-uglify": "^0.6.0", - "grunt-contrib-watch": "^0.6.1", - "grunt-coveralls": "^1.0.0", - "grunt-template-jasmine-istanbul": "^0.3.0", - "grunt-umd": "^2.3.3" + "jasmine": "^3.2.0", + "rollup": "^0.66.6" } } diff --git a/src/core/core.js b/src/core/core.js index 8d8c101..2744dcf 100644 --- a/src/core/core.js +++ b/src/core/core.js @@ -1,12 +1,14 @@ -'use strict'; +export {Core, CoreClass}; +import {err} from "../helpers/err.js"; +import {Sandbox} from "../sandbox/sandbox.js"; /** * The constructor of Core * -* @class Core +* @class CoreClass * @constructor */ -var Core = function() { +var CoreClass = function() { this.modules = {}; }; @@ -17,9 +19,9 @@ var Core = function() { * @param {string} module the name of the new module * @param {function} constructor the constructor of the new module */ -Core.prototype.register = function(module, constructor) { +CoreClass.prototype.register = function(module, constructor) { if(this.modules[module]) { - this.helpers.err('!!module', module); + err('!!module', module); return false; } this.modules[module] = { @@ -36,46 +38,30 @@ Core.prototype.register = function(module, constructor) { * @param {boolean} destroy check if the module exists, but is already destroyed * @return {boolean} if the module exists or already have an instance */ -Core.prototype.moduleCheck = function(module, destroy) { - if(destroy) return !module || !module.instance; +CoreClass.prototype.moduleCheck = function(module, destroy) { + if (destroy) return !module || !module.instance; return !module || module.instance; }; -/** -* Gets an element by ID to attach to the module instance -* -* @method getElement -* @param {string} id the id of the main element in the module -*/ -Core.prototype.getElement = function(id) { - var el = document.getElementById(id); - - // this fixes some blackberry, opera and IE possible bugs - return (el && el.id === id && el.parentElement) ? el : null; -}; - /** * Starts a registered module, if no module is passed, it starts all modules * * @method start * @param {string} module the name of the module */ -Core.prototype.start = function(module) { +CoreClass.prototype.start = function(module) { if(!module) return this.startAll(); - var cModule = this.modules[module], - el = this.getElement(module); + var cModule = this.modules[module]; if(this.moduleCheck(cModule)) { - this.helpers.err('!start', module); + err('!start', module); return false; } - cModule.instance = new cModule.constructor(new this.Sandbox(module)); + cModule.instance = new cModule.constructor(new Sandbox(module)); - // attachs the element to the instance of the module - cModule.instance.el = el; if(cModule.instance.init) return cModule.instance.init(); }; @@ -86,13 +72,13 @@ Core.prototype.start = function(module) { * @method start * @param {string} module the name of the module */ -Core.prototype.stop = function(module) { +CoreClass.prototype.stop = function(module) { if(!module) return this.stopAll(); var cModule = this.modules[module], stopReturn; if(this.moduleCheck(cModule, true)) { - this.helpers.err('!stop', module); + //err('!stop', module); return false; } @@ -100,7 +86,7 @@ Core.prototype.stop = function(module) { cModule.instance = null; - this.Sandbox.clearNotifications(module); + Sandbox.clearNotifications(module); return stopReturn; }; @@ -110,7 +96,7 @@ Core.prototype.stop = function(module) { * * @method stopAll */ -Core.prototype.stopAll = function() { +CoreClass.prototype.stopAll = function() { this.xAll('stop'); }; @@ -119,7 +105,7 @@ Core.prototype.stopAll = function() { * * @method stopAll */ -Core.prototype.startAll = function() { +CoreClass.prototype.startAll = function() { this.xAll('start'); }; @@ -129,10 +115,10 @@ Core.prototype.startAll = function() { * @method xAll * @param {string} method the method that will be triggered */ -Core.prototype.xAll = function(method) { +CoreClass.prototype.xAll = function(method) { for(var module in this.modules) { if(this.modules.hasOwnProperty(module)) this[method](module); } }; -Core = new Core(); +var Core = new CoreClass(); diff --git a/src/core/methods/extend.js b/src/core/methods/extend.js deleted file mode 100644 index 58f9d99..0000000 --- a/src/core/methods/extend.js +++ /dev/null @@ -1,26 +0,0 @@ -var extensions = {}; - -/** -* Extends core functionalities -* -* @method extend -* @param {string} name the name of the extension -* @param {function | array | boolean | string | number} implementation what the extension does -*/ -var extend = function(name, implementation) { - extensions[name] = implementation; -}; - -/** -* returns the extension -* -* @method getExtension -* @param {string} extension the name of the extension -* @return {function | array | boolean | string | number} the implementation of the extension -*/ -var getExtension = function(extension) { - return extensions[extension] || null; -}; - -Core.extend = extend; -Core.getExtension = getExtension; diff --git a/src/helpers/err.js b/src/helpers/err.js index afcf7f3..53e77ac 100644 --- a/src/helpers/err.js +++ b/src/helpers/err.js @@ -1,3 +1,5 @@ +export {err} + /** * Handles error messages * @@ -6,15 +8,12 @@ * @param {function} message the complementary message to the error */ var err = function(error, message) { - Core.helpers.log(err.messages[error] + "\"" + message + "\""); + console.error(`${messages[error]}: "${message}"`); }; -err.messages = { - '!start': 'Could not start the given module, it\'s either already started or is not registered: ', - '!stop': 'Could not stop the given module, it\'s either already stopped or is not registered: ', - '!!module': 'Can\'t register an already registered module: ', - '!!listen': 'There\'s already an listen handler to the notification: ' +var messages = { + '!start': `Could not start the given module, it's either already started or is not registered`, + '!stop': `Could not stop the given module, it's either already stopped or is not registered`, + '!!module': `Can't register an already registered module`, + '!!listen': `There's already an listen handler to the notification` }; - -Core.helpers = Core.helpers || {}; -Core.helpers.err = err; diff --git a/src/helpers/isArray.js b/src/helpers/isArray.js deleted file mode 100644 index 0bee40c..0000000 --- a/src/helpers/isArray.js +++ /dev/null @@ -1,13 +0,0 @@ -/** -* Returns if the object is an array -* -* @method isArray -* @param {object} obj the object that will be checked -* @return {boolean} if its an array or not -*/ -var isArray = function(obj) { - return Object.prototype.toString.call(obj) === '[object Array]'; -}; - -Core.helpers = Core.helpers || {}; -Core.helpers.isArray = isArray; diff --git a/src/helpers/log.js b/src/helpers/log.js deleted file mode 100644 index 023b7ef..0000000 --- a/src/helpers/log.js +++ /dev/null @@ -1,9 +0,0 @@ -/** -* Adds console.log to Core helpers -* -* @method log -*/ -var log = (window.console) ? window.console.log.bind(window.console) : function() {}; - -Core.helpers = Core.helpers || {}; -Core.helpers.log = log; diff --git a/src/helpers/toArray.js b/src/helpers/toArray.js deleted file mode 100644 index b38ba31..0000000 --- a/src/helpers/toArray.js +++ /dev/null @@ -1,13 +0,0 @@ -/** -* Returns an array-like to array -* -* @method toArray -* @param {object} obj The arraylike that will be converted -* @return {array} the converted arraylike -*/ -var toArray = function(obj) { - return Array.prototype.slice.call(obj); -}; - -Core.helpers = Core.helpers || {}; -Core.helpers.toArray = toArray; diff --git a/src/sandbox/sandbox.js b/src/sandbox/sandbox.js index 15eb72e..4b4a90c 100644 --- a/src/sandbox/sandbox.js +++ b/src/sandbox/sandbox.js @@ -1,3 +1,6 @@ +export {Sandbox}; +import {err} from "../helpers/err.js"; + /** * The constructor of Sandbox * @@ -8,8 +11,12 @@ var Sandbox = function(module) { this.module = module; }; -// All notifications from sandbox -Sandbox.notifications = {}; +/** +* All notifications from sandbox +* +* @private +*/ +var notifications = {}; /** * Clear all notifications from an specific module @@ -18,7 +25,7 @@ Sandbox.notifications = {}; * @param {string} module the name of the module */ Sandbox.clearNotifications = function(module) { - delete Sandbox.notifications[module]; + delete notifications[module]; }; /** @@ -28,8 +35,8 @@ Sandbox.clearNotifications = function(module) { * @param {object} notification the object with notifications configs */ Sandbox.prototype.notify = function(notification) { - for(var module in Sandbox.notifications) { - var listening = Sandbox.notifications[module][notification.type]; + for(var module in notifications) { + var listening = notifications[module][notification.type]; if(listening) { listening.callback.call(listening.context, notification.data); } @@ -43,8 +50,8 @@ Sandbox.prototype.notify = function(notification) { * @param {string | array} notification the notification that the module will be listening to */ Sandbox.prototype.listen = function(notification) { - var args = Core.helpers.toArray(arguments); - if(!Core.helpers.isArray(notification)) return this.addNotification.apply(this, arguments); + var args = Array.from(arguments); + if(!Array.isArray(notification)) return this.addNotification.apply(this, arguments); for(var i = 0, len = notification.length; i < len; i += 1) { args[0] = notification[i]; @@ -62,35 +69,21 @@ Sandbox.prototype.listen = function(notification) { * @param {boolean} replace if the notification already exists, it forces to rewrite it */ Sandbox.prototype.addNotification = function(notification, callback, context, replace) { - var notifications = Sandbox.notifications, - addNotification = false; + var addNotification = false; if(!notifications[this.module] || !notifications[this.module][notification]) { addNotification = true; } else if(replace) { addNotification = true; } else { - Core.helpers.err('!!listen', notification); + err('!!listen', notification); } if(addNotification) { notifications[this.module] = notifications[this.module] || {}; notifications[this.module][notification] = { callback: callback, - context: context || window + context: context || undefined }; } }; - -/** -* Returns an extension from Core -* -* @method x -* @param {string} extension the name of the extension -* @return {function | array | boolean | string | number} the implementation of the extension -*/ -Sandbox.prototype.use = function(extension) { - return Core.getExtension(extension); -}; - -Core.Sandbox = Sandbox; diff --git a/tests/core/coreSpec.js b/tests/core/coreSpec.js index 6439f04..8d22db5 100644 --- a/tests/core/coreSpec.js +++ b/tests/core/coreSpec.js @@ -1,3 +1,5 @@ +var {Core} = require('../../dist/core.umd.js') + describe('Testing Core', function() { afterEach(function() { Core.stopAll(); @@ -11,11 +13,11 @@ describe('Testing Core', function() { }); it('Should return false and throw a log if the module is already registered', function() { - spyOn(Core.helpers, 'err'); + //spyOn(err); Core.register('tweet', function() {}); expect(Core.register('tweet', function() {})).toBeFalsy(); - expect(Core.helpers.err).toHaveBeenCalled(); + //expect(err).toHaveBeenCalled(); }); it('Should start a new module', function() { @@ -26,12 +28,12 @@ describe('Testing Core', function() { }); it('Should return false and throw a log if the module is already started', function() { - spyOn(Core.helpers, 'err'); + //spyOn(err); Core.register('tweet', function() {}); Core.start('tweet'); expect(Core.start('tweet')).toBeFalsy(); - expect(Core.helpers.err).toHaveBeenCalled(); + //expect(err).toHaveBeenCalled(); }); it('Should stop a new module', function() { @@ -43,13 +45,13 @@ describe('Testing Core', function() { }); it('Should return false and throw a log if the module is already stopped', function() { - spyOn(Core.helpers, 'err'); + //spyOn(err); Core.register('tweet', function() {}); Core.start('tweet'); Core.stop('tweet'); expect(Core.stop('tweet')).toBeFalsy(); - expect(Core.helpers.err).toHaveBeenCalled(); + //expect(err).toHaveBeenCalled(); }); it('Should start all modules', function() { @@ -231,47 +233,6 @@ describe('Testing Core', function() { expect(spying.tweet3).toHaveBeenCalled(); }); - it('Should extend and return a different component from Core extensions', function() { - var jQuery = {}; - Core.extend('$', jQuery); - - expect(Core.getExtension('$')).toBe(jQuery); - }); - - it('Should return null if the extension doesn\'t exists into Core extensions', function() { - expect(Core.getExtension('foo')).toBeNull(); - }); - - describe('Testing Isolation of DOM', function() { - it('Should return the DOM element if it has the same id as the module', function() { - var newElement = document.createElement('div'); - newElement.id = 'tweet'; - document.body.appendChild(newElement); - - Core.register('tweet', function() { - return { - init: function() { - expect(this.el).toBe(newElement); - } - }; - }); - - Core.start('tweet'); - }); - - it('Should return null if the module doesnt find any DOM element with the same name', function() { - Core.register('tweet2', function() { - return { - init: function() { - expect(this.el).toBeNull(); - } - }; - }); - - Core.start('tweet2'); - }); - }); - describe('Testing return in Start and Stop methods', function() { it('Should return in the Start method the value returned from the init method inside the module', function() { Core.register('tweet', function() { diff --git a/tests/jasmine.json b/tests/jasmine.json new file mode 100644 index 0000000..a3ec7e7 --- /dev/null +++ b/tests/jasmine.json @@ -0,0 +1,11 @@ +{ + "spec_dir": "tests", + "spec_files": [ + "**/*[sS]pec.js" + ], + "helpers": [ + + ], + "stopSpecOnExpectationFailure": false, + "random": false +} diff --git a/tests/sandbox/sandboxSpec.js b/tests/sandbox/sandboxSpec.js index c7f7d76..aaf3484 100644 --- a/tests/sandbox/sandboxSpec.js +++ b/tests/sandbox/sandboxSpec.js @@ -1,3 +1,5 @@ +var {Core} = require('../../dist/core.umd.js') + describe('Testing Sandbox', function() { afterEach(function() { Core.stopAll(); @@ -8,7 +10,7 @@ describe('Testing Sandbox', function() { Core.register('tweet', function(sandbox) { return { init: function() { - expect(sandbox instanceof Core.Sandbox).toBeTruthy(); + expect(sandbox).toBeTruthy(); } } }); @@ -287,18 +289,4 @@ describe('Testing Sandbox', function() { Core.start('tweet'); }); - it('Should give access to a extension inside a module', function() { - var jQuery = {}; - Core.extend('$', jQuery); - - Core.register('tweet', function(sandbox) { - return { - init: function() { - expect(sandbox.use('$')).toBe(jQuery); - } - } - }); - - Core.start('tweet'); - }); }); diff --git a/tools/Gruntfile.js b/tools/Gruntfile.js new file mode 100644 index 0000000..ec0963e --- /dev/null +++ b/tools/Gruntfile.js @@ -0,0 +1,74 @@ +module.exports = function(grunt) { + 'use strict'; + + var app = {}, + config = {}, + tasks = [ + 'grunt-contrib-jshint', + 'grunt-contrib-jasmine', + 'grunt-coveralls', + ]; + + + + // ============================================= + // jshint + config.jshint = {}; + config.jshint.options = { + debug: true, + sub: true + }; + config.jshint.all = ['dist/core.js']; + + + + // ============================================= + // jasmine + config.jasmine = {}; + config.jasmine.coverage = { + src: [ + 'dist/core.js' + ], + options: { + specs: 'tests/**/*Spec.js', + template: require('grunt-template-jasmine-istanbul'), + templateOptions: { + coverage: 'bin/coverage/coverage.json', + report: { + type: 'lcov', + options: { + dir: 'bin/coverage' + } + }, + thresholds: { + lines: 75, + statements: 75, + branches: 75, + functions: 90 + } + } + } + } + + + // ============================================= + // coveralls + config.coveralls = { + src: 'bin/coverage/lcov.info' + }; + + + + // Load all tasks + tasks.forEach(grunt.loadNpmTasks); + + // config + grunt.initConfig(config); + + + + + grunt.registerTask('ci', ['jshint', 'jasmine', 'coveralls']); + + +}; diff --git a/tools/gruntdep.json b/tools/gruntdep.json new file mode 100644 index 0000000..7c2759a --- /dev/null +++ b/tools/gruntdep.json @@ -0,0 +1,17 @@ +{ + "scripts": { + "test": "grunt ci" + }, + "devDependencies": { + "grunt": "^0.4.5", + "grunt-bump": "^0.3.0", + "grunt-contrib-concat": "^0.5.0", + "grunt-contrib-jasmine": "^0.8.0", + "grunt-contrib-jshint": "^0.10.0", + "grunt-contrib-uglify": "^0.6.0", + "grunt-contrib-watch": "^0.6.1", + "grunt-coveralls": "^1.0.0", + "grunt-template-jasmine-istanbul": "^0.3.0", + "grunt-umd": "^2.3.3" + } +} diff --git a/tools/rollup.config.js b/tools/rollup.config.js new file mode 100644 index 0000000..43a1fbf --- /dev/null +++ b/tools/rollup.config.js @@ -0,0 +1,82 @@ +// rollup.config.js +import _package from "../package.json"; + +const {version, name, license} = _package; +const GLOBAL_NAME = `Core`; +const banner = `/* ${name} v${version} ${new Date().toJSON()} licensed ${license} */`; + + const commonOutputOptions = { + // core output options + name : GLOBAL_NAME, + // globals: [], + + // advanced output options + // paths: {}, + banner, + // footer: ``, + // intro: ``, + // outro: ``, + // sourcemap, + // sourcemapFile, + interop: false, + extend: false, + + // danger zone + // exports, + // indent, + strict: true, + // freeze, + namespaceToStringTag: false + + // experimental + // entryFileNames, + // chunkFileNames, + // assetFileNames +}; + +export default { // can be an array (for multiple inputs) + // core input options + input: `src/core/core.js`, // required + // external: [], + //plugins: [], + + // advanced input options + // onwarn, + // perf, + + // danger zone + // acorn, + // acornInjectPlugins, + treeshake: { + pureExternalModules: false, + propertyReadSideEffects: false // assume reading properties has no side effect + }, + // context, + // moduleContext, + + + output: [ // required (can be an array, for multiple outputs), + Object.assign({ + format: `es`, + file : `dist/core.js`, + }, commonOutputOptions), + Object.assign({ + format: `iife`, + file : `dist/core.iife.js`, + }, commonOutputOptions), + Object.assign({ + format: `umd`, + file : `dist/core.umd.js`, + amd: { + id: GLOBAL_NAME + } + }, commonOutputOptions) + ], + + watch: { + // chokidar, + // include, + // exclude, + clearScreen: true + } +};