Skip to content

Commit

Permalink
Initial commit for x-tag-switchboard
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpecor committed Jun 30, 2015
1 parent e268b09 commit 5fef62a
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "x-tag-switchboard",
"version": "1.0.0",
"homepage": "https://github.com/jasonpecor/x-tag-switchboard",
"authors": [
"jasonpecor <[email protected]>"
],
"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"
]
}
129 changes: 129 additions & 0 deletions dist/x-tag-switchboard.js
Original file line number Diff line number Diff line change
@@ -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;
}());

1 change: 1 addition & 0 deletions dist/x-tag-switchboard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -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/'));
});

30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
129 changes: 129 additions & 0 deletions src/switchboard.js
Original file line number Diff line number Diff line change
@@ -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;
}());

0 comments on commit 5fef62a

Please sign in to comment.