-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit for x-tag-switchboard
- Loading branch information
1 parent
e268b09
commit 5fef62a
Showing
7 changed files
with
331 additions
and
0 deletions.
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
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 @@ | ||
{ | ||
"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" | ||
] | ||
} |
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,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; | ||
}()); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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/')); | ||
}); | ||
|
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,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" | ||
} | ||
} |
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,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; | ||
}()); | ||
|