From 5fef62a9a4bced4bf8f624ce60b5d1456db9eea5 Mon Sep 17 00:00:00 2001 From: jasonpecor Date: Tue, 30 Jun 2015 08:56:42 -0400 Subject: [PATCH] Initial commit for x-tag-switchboard --- .gitignore | 3 + bower.json | 23 ++++++ dist/x-tag-switchboard.js | 129 ++++++++++++++++++++++++++++++++++ dist/x-tag-switchboard.min.js | 1 + gulpfile.js | 16 +++++ package.json | 30 ++++++++ src/switchboard.js | 129 ++++++++++++++++++++++++++++++++++ 7 files changed, 331 insertions(+) create mode 100644 bower.json create mode 100644 dist/x-tag-switchboard.js create mode 100644 dist/x-tag-switchboard.min.js create mode 100644 gulpfile.js create mode 100644 package.json create mode 100644 src/switchboard.js diff --git a/.gitignore b/.gitignore index 123ae94..f663c3e 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules + +# Bower dependencies +bower_components diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..5f6b625 --- /dev/null +++ b/bower.json @@ -0,0 +1,23 @@ +{ + "name": "x-tag-switchboard", + "version": "1.0.0", + "homepage": "https://github.com/jasonpecor/x-tag-switchboard", + "authors": [ + "jasonpecor " + ], + "description": "Mixin for managing global events for x-tags", + "main": "dist/switchboard.js", + "keywords": [ + "x-tag", + "mixin", + "events" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/dist/x-tag-switchboard.js b/dist/x-tag-switchboard.js new file mode 100644 index 0000000..64f6739 --- /dev/null +++ b/dist/x-tag-switchboard.js @@ -0,0 +1,129 @@ +xtag.mixins.switchboard = (function () { + "use strict"; + + var connections = {}, // connection pools + online = true; // switchboard online + + // Add a connection to an event pool + + function addConnection(evt, element) { + if (!connections[evt]) + connections[evt] = []; + + if (connections[evt].indexOf(element) === -1) + connections[evt].push(element); + } + + // Remove a connection from an event pool + + function removeConnection(evt, element) { + if (!connections[evt]) return false; + var i = connections[evt].indexOf(element); + if (i === -1) return false; + connections[evt].splice(i,1); + return true; + } + + // Switchboard API + + var api = { + patch: function (element, events) { + // prepare certain events to be patched into the switchboard for this element + if (typeof events === 'string') + element._patches.push(events); + else if (events instanceof Array) + element._patches = element._patches.concat(events); + else + throw new TypeError("Cannot patch event: Must be string or array of strings"); + }, + connect: function (element) { + // connect the element to the switchboard + if (!element._patches) return; + + var n = element._patches.length; + + for (var i = 0; i < n; i++) + addConnection(element._patches[i], element); + }, + disconnect: function (element) { + if (!element._patches) return; + + var n = element._patches.length; + + for (var i = 0; i < n; i++) + removeConnection(element._patches[i], element); + }, + unpatch: function (element, events) { + if (events) { + var n = events.length; + for (var i = 0; i < n; i++) { + var x = element._patches.indexOf(events[i]); + if (x !== -1) { + element._patches.splice(x, 1); + if (element.parentNode) + removeConnection(events[i], element); + } + } + } else { + if (element.parentNode) + api.disconnect(element); + element._patches = []; + } + }, + transmit: function (evt, options) { + // no sender means anonymous + if (!online) return 0; + if (!connections[evt]) return 0; + + var n = connections[evt].length; + + for (var i = 0; i < n; i++) + xtag.fireEvent(connections[evt][i], evt, options); + + return n; + }, + get online() { + return online; + }, + set online(v) { + online = !!v; + }, + get connections() { // TODO: REMOVE THIS!!! + return connections; + } + }; + + // Make Switchboard API globally available + + xtag.switchboard = api; + + // Define switchboard mixin for x-tag + + var mixin = { + lifecycle: { + created: function () { + + Object.defineProperty(this, 'switchboard', { + value: api, + writable: false, + enumerable: false + }); + + Object.defineProperty(this, '_patches', { + value: [], + enumerable: false, + writable: true + }); + }, + inserted: function () { + this.switchboard.connect(this); + }, + removed: function () { + this.switchboard.disconnect(this); + } + } + }; + + return mixin; +}()); + \ No newline at end of file diff --git a/dist/x-tag-switchboard.min.js b/dist/x-tag-switchboard.min.js new file mode 100644 index 0000000..4068c6f --- /dev/null +++ b/dist/x-tag-switchboard.min.js @@ -0,0 +1 @@ +xtag.mixins.switchboard=function(){"use strict";function t(t,e){n[t]||(n[t]=[]),-1===n[t].indexOf(e)&&n[t].push(e)}function e(t,e){if(!n[t])return!1;var c=n[t].indexOf(e);return-1===c?!1:(n[t].splice(c,1),!0)}var n={},c=!0,r={patch:function(t,e){if("string"==typeof e)t._patches.push(e);else{if(!(e instanceof Array))throw new TypeError("Cannot patch event: Must be string or array of strings");t._patches=t._patches.concat(e)}},connect:function(e){if(e._patches)for(var n=e._patches.length,c=0;n>c;c++)t(e._patches[c],e)},disconnect:function(t){if(t._patches)for(var n=t._patches.length,c=0;n>c;c++)e(t._patches[c],t)},unpatch:function(t,n){if(n)for(var c=n.length,i=0;c>i;i++){var a=t._patches.indexOf(n[i]);-1!==a&&(t._patches.splice(a,1),t.parentNode&&e(n[i],t))}else t.parentNode&&r.disconnect(t),t._patches=[]},transmit:function(t,e){if(!c)return 0;if(!n[t])return 0;for(var r=n[t].length,i=0;r>i;i++)xtag.fireEvent(n[t][i],t,e);return r},get online(){return c},set online(t){c=!!t},get connections(){return n}};xtag.switchboard=r;var i={lifecycle:{created:function(){Object.defineProperty(this,"switchboard",{value:r,writable:!1,enumerable:!1}),Object.defineProperty(this,"_patches",{value:[],enumerable:!1,writable:!0})},inserted:function(){this.switchboard.connect(this)},removed:function(){this.switchboard.disconnect(this)}}};return i}(); \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..0c1d3e8 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,16 @@ +var pkg = require('./package.json'), + gulp = require('gulp'), + uglify = require('gulp-uglify'), + rename = require('gulp-rename'), + concat = require('gulp-concat'), + del = require('del'); + +gulp.task('build', function () { + gulp.src('src/**/*.js') + .pipe(concat(pkg.name + '.js')) + .pipe(gulp.dest('dist/')) + .pipe(uglify()) + .pipe(rename(pkg.name + '.min.js')) + .pipe(gulp.dest('dist/')); +}); + diff --git a/package.json b/package.json new file mode 100644 index 0000000..1d3ddd0 --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "x-tag-switchboard", + "version": "0.0.0", + "description": "Mixin for managing global events for x-tags", + "main": "dist/switchboard.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git://github.com/jasonpecor/x-tag-switchboard" + }, + "keywords": [ + "x-tag", + "mixin", + "events" + ], + "author": "Jason Pecor", + "license": "MIT", + "bugs": { + "url": "https://github.com/jasonpecor/x-tag-switchboard/issues" + }, + "homepage": "https://github.com/jasonpecor/x-tag-switchboard", + "devDependencies": { + "del": "^1.2.0", + "gulp-concat": "^2.6.0", + "gulp-rename": "^1.2.2", + "gulp-uglify": "^1.2.0" + } +} diff --git a/src/switchboard.js b/src/switchboard.js new file mode 100644 index 0000000..64f6739 --- /dev/null +++ b/src/switchboard.js @@ -0,0 +1,129 @@ +xtag.mixins.switchboard = (function () { + "use strict"; + + var connections = {}, // connection pools + online = true; // switchboard online + + // Add a connection to an event pool + + function addConnection(evt, element) { + if (!connections[evt]) + connections[evt] = []; + + if (connections[evt].indexOf(element) === -1) + connections[evt].push(element); + } + + // Remove a connection from an event pool + + function removeConnection(evt, element) { + if (!connections[evt]) return false; + var i = connections[evt].indexOf(element); + if (i === -1) return false; + connections[evt].splice(i,1); + return true; + } + + // Switchboard API + + var api = { + patch: function (element, events) { + // prepare certain events to be patched into the switchboard for this element + if (typeof events === 'string') + element._patches.push(events); + else if (events instanceof Array) + element._patches = element._patches.concat(events); + else + throw new TypeError("Cannot patch event: Must be string or array of strings"); + }, + connect: function (element) { + // connect the element to the switchboard + if (!element._patches) return; + + var n = element._patches.length; + + for (var i = 0; i < n; i++) + addConnection(element._patches[i], element); + }, + disconnect: function (element) { + if (!element._patches) return; + + var n = element._patches.length; + + for (var i = 0; i < n; i++) + removeConnection(element._patches[i], element); + }, + unpatch: function (element, events) { + if (events) { + var n = events.length; + for (var i = 0; i < n; i++) { + var x = element._patches.indexOf(events[i]); + if (x !== -1) { + element._patches.splice(x, 1); + if (element.parentNode) + removeConnection(events[i], element); + } + } + } else { + if (element.parentNode) + api.disconnect(element); + element._patches = []; + } + }, + transmit: function (evt, options) { + // no sender means anonymous + if (!online) return 0; + if (!connections[evt]) return 0; + + var n = connections[evt].length; + + for (var i = 0; i < n; i++) + xtag.fireEvent(connections[evt][i], evt, options); + + return n; + }, + get online() { + return online; + }, + set online(v) { + online = !!v; + }, + get connections() { // TODO: REMOVE THIS!!! + return connections; + } + }; + + // Make Switchboard API globally available + + xtag.switchboard = api; + + // Define switchboard mixin for x-tag + + var mixin = { + lifecycle: { + created: function () { + + Object.defineProperty(this, 'switchboard', { + value: api, + writable: false, + enumerable: false + }); + + Object.defineProperty(this, '_patches', { + value: [], + enumerable: false, + writable: true + }); + }, + inserted: function () { + this.switchboard.connect(this); + }, + removed: function () { + this.switchboard.disconnect(this); + } + } + }; + + return mixin; +}()); + \ No newline at end of file