From 618aad8f702b823579c8878ce0cacc99c198ea3e Mon Sep 17 00:00:00 2001 From: Raynos Date: Tue, 21 Aug 2012 23:53:52 -0700 Subject: [PATCH] first --- .gitignore | 3 + LICENCE | 19 ++++ Makefile | 0 README.md | 36 +++++++ example/Makefile | 2 + example/index.js | 18 ++++ example/static/bundle.js | 213 ++++++++++++++++++++++++++++++++++++++ example/static/index.html | 24 +++++ index.js | 31 ++++++ package.json | 30 ++++++ 10 files changed, 376 insertions(+) create mode 100644 .gitignore create mode 100644 LICENCE create mode 100644 Makefile create mode 100644 README.md create mode 100644 example/Makefile create mode 100644 example/index.js create mode 100644 example/static/bundle.js create mode 100644 example/static/index.html create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..062c11e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +*.log +*.err \ No newline at end of file diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..a23e08a --- /dev/null +++ b/LICENCE @@ -0,0 +1,19 @@ +Copyright (c) 2012 Raynos. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..7489a8d --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# selectron + +Keep track of the currently selected thing + +## Example + +``` js +var selectron = require("selectron") + +var sel = selectron() + , ul = document.getElementById("list") + +ul.addEventListener("click", function (evt) { + if (evt.target.tagName === "LI") { + sel.select(evt.target) + } +}) + +sel.on("select", function (li) { + li.classList.add("selected") +}) + +sel.on("unselect", function (li) { + li.classList.remove("selected") +}) +``` + +## Installation + +`npm install selectron` + +## Contributors + + - Raynos + +## MIT Licenced \ No newline at end of file diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..398c9ea --- /dev/null +++ b/example/Makefile @@ -0,0 +1,2 @@ +run: + node ../node_modules/.bin/browserify-server \ No newline at end of file diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..e794d74 --- /dev/null +++ b/example/index.js @@ -0,0 +1,18 @@ +var selectron = require("../index") + +var sel = selectron() + , ul = document.getElementById("list") + +ul.addEventListener("click", function (evt) { + if (evt.target.tagName === "LI") { + sel.select(evt.target) + } +}) + +sel.on("select", function (li) { + li.classList.add("selected") +}) + +sel.on("unselect", function (li) { + li.classList.remove("selected") +}) \ No newline at end of file diff --git a/example/static/bundle.js b/example/static/bundle.js new file mode 100644 index 0000000..541d647 --- /dev/null +++ b/example/static/bundle.js @@ -0,0 +1,213 @@ +(function(){var require = function (file, cwd) { + var resolved = require.resolve(file, cwd || '/'); + var mod = require.modules[resolved]; + if (!mod) throw new Error( + 'Failed to resolve module ' + file + ', tried ' + resolved + ); + var cached = require.cache[resolved]; + var res = cached? cached.exports : mod(); + return res; +}; + +require.paths = []; +require.modules = {}; +require.cache = {}; +require.extensions = [".js",".coffee",".html"]; + +require._core = { + 'assert': true, + 'events': true, + 'fs': true, + 'path': true, + 'vm': true +}; + +require.resolve = (function () { + return function (x, cwd) { + if (!cwd) cwd = '/'; + + if (require._core[x]) return x; + var path = require.modules.path(); + cwd = path.resolve('/', cwd); + var y = cwd || '/'; + + if (x.match(/^(?:\.\.?\/|\/)/)) { + var m = loadAsFileSync(path.resolve(y, x)) + || loadAsDirectorySync(path.resolve(y, x)); + if (m) return m; + } + + var n = loadNodeModulesSync(x, y); + if (n) return n; + + throw new Error("Cannot find module '" + x + "'"); + + function loadAsFileSync (x) { + x = path.normalize(x); + if (require.modules[x]) { + return x; + } + + for (var i = 0; i < require.extensions.length; i++) { + var ext = require.extensions[i]; + if (require.modules[x + ext]) return x + ext; + } + } + + function loadAsDirectorySync (x) { + x = x.replace(/\/+$/, ''); + var pkgfile = path.normalize(x + '/package.json'); + if (require.modules[pkgfile]) { + var pkg = require.modules[pkgfile](); + var b = pkg.browserify; + if (typeof b === 'object' && b.main) { + var m = loadAsFileSync(path.resolve(x, b.main)); + if (m) return m; + } + else if (typeof b === 'string') { + var m = loadAsFileSync(path.resolve(x, b)); + if (m) return m; + } + else if (pkg.main) { + var m = loadAsFileSync(path.resolve(x, pkg.main)); + if (m) return m; + } + } + + return loadAsFileSync(x + '/index'); + } + + function loadNodeModulesSync (x, start) { + var dirs = nodeModulesPathsSync(start); + for (var i = 0; i < dirs.length; i++) { + var dir = dirs[i]; + var m = loadAsFileSync(dir + '/' + x); + if (m) return m; + var n = loadAsDirectorySync(dir + '/' + x); + if (n) return n; + } + + var m = loadAsFileSync(x); + if (m) return m; + } + + function nodeModulesPathsSync (start) { + var parts; + if (start === '/') parts = [ '' ]; + else parts = path.normalize(start).split('/'); + + var dirs = []; + for (var i = parts.length - 1; i >= 0; i--) { + if (parts[i] === 'node_modules') continue; + var dir = parts.slice(0, i + 1).join('/') + '/node_modules'; + dirs.push(dir); + } + + return dirs; + } + }; +})(); + +require.alias = function (from, to) { + var path = require.modules.path(); + var res = null; + try { + res = require.resolve(from + '/package.json', '/'); + } + catch (err) { + res = require.resolve(from, '/'); + } + var basedir = path.dirname(res); + + var keys = (Object.keys || function (obj) { + var res = []; + for (var key in obj) res.push(key); + return res; + })(require.modules); + + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + if (key.slice(0, basedir.length + 1) === basedir + '/') { + var f = key.slice(basedir.length); + require.modules[to + f] = require.modules[basedir + f]; + } + else if (key === basedir) { + require.modules[to] = require.modules[basedir]; + } + } +}; + +(function () { + var process = {}; + + require.define = function (filename, fn) { + if (require.modules.__browserify_process) { + process = require.modules.__browserify_process(); + } + + var dirname = require._core[filename] + ? '' + : require.modules.path().dirname(filename) + ; + + var require_ = function (file) { + var requiredModule = require(file, dirname); + var cached = require.cache[require.resolve(file, dirname)]; + + if (cached && cached.parent === null) { + cached.parent = module_; + } + + return requiredModule; + }; + require_.resolve = function (name) { + return require.resolve(name, dirname); + }; + require_.modules = require.modules; + require_.define = require.define; + require_.cache = require.cache; + var module_ = { + id : filename, + filename: filename, + exports : {}, + loaded : false, + parent: null + }; + + require.modules[filename] = function () { + require.cache[filename] = module_; + fn.call( + module_.exports, + require_, + module_, + module_.exports, + dirname, + filename, + process + ); + module_.loaded = true; + return module_.exports; + }; + }; +})(); + + +require.define("path",Function(['require','module','exports','__dirname','__filename','process'],"function filter (xs, fn) {\n var res = [];\n for (var i = 0; i < xs.length; i++) {\n if (fn(xs[i], i, xs)) res.push(xs[i]);\n }\n return res;\n}\n\n// resolves . and .. elements in a path array with directory names there\n// must be no slashes, empty elements, or device names (c:\\) in the array\n// (so also no leading and trailing slashes - it does not distinguish\n// relative and absolute paths)\nfunction normalizeArray(parts, allowAboveRoot) {\n // if the path tries to go above the root, `up` ends up > 0\n var up = 0;\n for (var i = parts.length; i >= 0; i--) {\n var last = parts[i];\n if (last == '.') {\n parts.splice(i, 1);\n } else if (last === '..') {\n parts.splice(i, 1);\n up++;\n } else if (up) {\n parts.splice(i, 1);\n up--;\n }\n }\n\n // if the path is allowed to go above the root, restore leading ..s\n if (allowAboveRoot) {\n for (; up--; up) {\n parts.unshift('..');\n }\n }\n\n return parts;\n}\n\n// Regex to split a filename into [*, dir, basename, ext]\n// posix version\nvar splitPathRe = /^(.+\\/(?!$)|\\/)?((?:.+?)?(\\.[^.]*)?)$/;\n\n// path.resolve([from ...], to)\n// posix version\nexports.resolve = function() {\nvar resolvedPath = '',\n resolvedAbsolute = false;\n\nfor (var i = arguments.length; i >= -1 && !resolvedAbsolute; i--) {\n var path = (i >= 0)\n ? arguments[i]\n : process.cwd();\n\n // Skip empty and invalid entries\n if (typeof path !== 'string' || !path) {\n continue;\n }\n\n resolvedPath = path + '/' + resolvedPath;\n resolvedAbsolute = path.charAt(0) === '/';\n}\n\n// At this point the path should be resolved to a full absolute path, but\n// handle relative paths to be safe (might happen when process.cwd() fails)\n\n// Normalize the path\nresolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {\n return !!p;\n }), !resolvedAbsolute).join('/');\n\n return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';\n};\n\n// path.normalize(path)\n// posix version\nexports.normalize = function(path) {\nvar isAbsolute = path.charAt(0) === '/',\n trailingSlash = path.slice(-1) === '/';\n\n// Normalize the path\npath = normalizeArray(filter(path.split('/'), function(p) {\n return !!p;\n }), !isAbsolute).join('/');\n\n if (!path && !isAbsolute) {\n path = '.';\n }\n if (path && trailingSlash) {\n path += '/';\n }\n \n return (isAbsolute ? '/' : '') + path;\n};\n\n\n// posix version\nexports.join = function() {\n var paths = Array.prototype.slice.call(arguments, 0);\n return exports.normalize(filter(paths, function(p, index) {\n return p && typeof p === 'string';\n }).join('/'));\n};\n\n\nexports.dirname = function(path) {\n var dir = splitPathRe.exec(path)[1] || '';\n var isWindows = false;\n if (!dir) {\n // No dirname\n return '.';\n } else if (dir.length === 1 ||\n (isWindows && dir.length <= 3 && dir.charAt(1) === ':')) {\n // It is just a slash or a drive letter with a slash\n return dir;\n } else {\n // It is a full dirname, strip trailing slash\n return dir.substring(0, dir.length - 1);\n }\n};\n\n\nexports.basename = function(path, ext) {\n var f = splitPathRe.exec(path)[2] || '';\n // TODO: make this comparison case-insensitive on windows?\n if (ext && f.substr(-1 * ext.length) === ext) {\n f = f.substr(0, f.length - ext.length);\n }\n return f;\n};\n\n\nexports.extname = function(path) {\n return splitPathRe.exec(path)[3] || '';\n};\n\n//@ sourceURL=path")); + +require.define("__browserify_process",Function(['require','module','exports','__dirname','__filename','process'],"var process = module.exports = {};\n\nprocess.nextTick = (function () {\n var queue = [];\n var canPost = typeof window !== 'undefined'\n && window.postMessage && window.addEventListener\n ;\n \n if (canPost) {\n window.addEventListener('message', function (ev) {\n if (ev.source === window && ev.data === 'browserify-tick') {\n ev.stopPropagation();\n if (queue.length > 0) {\n var fn = queue.shift();\n fn();\n }\n }\n }, true);\n }\n \n return function (fn) {\n if (canPost) {\n queue.push(fn);\n window.postMessage('browserify-tick', '*');\n }\n else setTimeout(fn, 0);\n };\n})();\n\nprocess.title = 'browser';\nprocess.browser = true;\nprocess.env = {};\nprocess.argv = [];\n\nprocess.binding = function (name) {\n if (name === 'evals') return (require)('vm')\n else throw new Error('No such module. (Possibly not yet loaded)')\n};\n\n(function () {\n var cwd = '/';\n var path;\n process.cwd = function () { return cwd };\n process.chdir = function (dir) {\n if (!path) path = require('path');\n cwd = path.resolve(dir, cwd);\n };\n})();\n//@ sourceURL=__browserify_process")); + +require.define("vm",Function(['require','module','exports','__dirname','__filename','process'],"module.exports = require(\"vm-browserify\")\n//@ sourceURL=vm")); + +require.define("/node_modules/vm-browserify/package.json",Function(['require','module','exports','__dirname','__filename','process'],"module.exports = {\"main\":\"index.js\"}\n//@ sourceURL=/node_modules/vm-browserify/package.json")); + +require.define("/node_modules/vm-browserify/index.js",Function(['require','module','exports','__dirname','__filename','process'],"var Object_keys = function (obj) {\n if (Object.keys) return Object.keys(obj)\n else {\n var res = [];\n for (var key in obj) res.push(key)\n return res;\n }\n};\n\nvar forEach = function (xs, fn) {\n if (xs.forEach) return xs.forEach(fn)\n else for (var i = 0; i < xs.length; i++) {\n fn(xs[i], i, xs);\n }\n};\n\nvar Script = exports.Script = function NodeScript (code) {\n if (!(this instanceof Script)) return new Script(code);\n this.code = code;\n};\n\nScript.prototype.runInNewContext = function (context) {\n if (!context) context = {};\n \n var iframe = document.createElement('iframe');\n if (!iframe.style) iframe.style = {};\n iframe.style.display = 'none';\n \n document.body.appendChild(iframe);\n \n var win = iframe.contentWindow;\n \n forEach(Object_keys(context), function (key) {\n win[key] = context[key];\n });\n \n if (!win.eval && win.execScript) {\n // win.eval() magically appears when this is called in IE:\n win.execScript('null');\n }\n \n var res = win.eval(this.code);\n \n forEach(Object_keys(win), function (key) {\n context[key] = win[key];\n });\n \n document.body.removeChild(iframe);\n \n return res;\n};\n\nScript.prototype.runInThisContext = function () {\n return eval(this.code); // maybe...\n};\n\nScript.prototype.runInContext = function (context) {\n // seems to be just runInNewContext on magical context objects which are\n // otherwise indistinguishable from objects except plain old objects\n // for the parameter segfaults node\n return this.runInNewContext(context);\n};\n\nforEach(Object_keys(Script.prototype), function (name) {\n exports[name] = Script[name] = function (code) {\n var s = Script(code);\n return s[name].apply(s, [].slice.call(arguments, 1));\n };\n});\n\nexports.createScript = function (code) {\n return exports.Script(code);\n};\n\nexports.createContext = Script.createContext = function (context) {\n // not really sure what this one does\n // seems to just make a shallow copy\n var copy = {};\n if(typeof context === 'object') {\n forEach(Object_keys(context), function (key) {\n copy[key] = context[key];\n });\n }\n return copy;\n};\n\n//@ sourceURL=/node_modules/vm-browserify/index.js")); + +require.define("/package.json",Function(['require','module','exports','__dirname','__filename','process'],"module.exports = {\"main\":\"index\"}\n//@ sourceURL=/package.json")); + +require.define("/index.js",Function(['require','module','exports','__dirname','__filename','process'],"var EventEmitter = require(\"events\").EventEmitter\n\nmodule.exports = selectron\n\nfunction selectron() {\n var sel = new EventEmitter()\n , selected\n\n sel.select = select\n sel.unselect = unselect\n\n return sel\n\n function select(thing) {\n unselect()\n\n selected = thing\n\n selected.select && selected.select()\n sel.emit(\"select\", selected)\n }\n\n function unselect() {\n if (selected) {\n selected.unselect && selected.unselect()\n sel.emit(\"unselect\", selected)\n }\n\n selected = null\n }\n}\n//@ sourceURL=/index.js")); + +require.define("events",Function(['require','module','exports','__dirname','__filename','process'],"if (!process.EventEmitter) process.EventEmitter = function () {};\n\nvar EventEmitter = exports.EventEmitter = process.EventEmitter;\nvar isArray = typeof Array.isArray === 'function'\n ? Array.isArray\n : function (xs) {\n return Object.prototype.toString.call(xs) === '[object Array]'\n }\n;\n\n// By default EventEmitters will print a warning if more than\n// 10 listeners are added to it. This is a useful default which\n// helps finding memory leaks.\n//\n// Obviously not all Emitters should be limited to 10. This function allows\n// that to be increased. Set to zero for unlimited.\nvar defaultMaxListeners = 10;\nEventEmitter.prototype.setMaxListeners = function(n) {\n if (!this._events) this._events = {};\n this._events.maxListeners = n;\n};\n\n\nEventEmitter.prototype.emit = function(type) {\n // If there is no 'error' event listener then throw.\n if (type === 'error') {\n if (!this._events || !this._events.error ||\n (isArray(this._events.error) && !this._events.error.length))\n {\n if (arguments[1] instanceof Error) {\n throw arguments[1]; // Unhandled 'error' event\n } else {\n throw new Error(\"Uncaught, unspecified 'error' event.\");\n }\n return false;\n }\n }\n\n if (!this._events) return false;\n var handler = this._events[type];\n if (!handler) return false;\n\n if (typeof handler == 'function') {\n switch (arguments.length) {\n // fast cases\n case 1:\n handler.call(this);\n break;\n case 2:\n handler.call(this, arguments[1]);\n break;\n case 3:\n handler.call(this, arguments[1], arguments[2]);\n break;\n // slower\n default:\n var args = Array.prototype.slice.call(arguments, 1);\n handler.apply(this, args);\n }\n return true;\n\n } else if (isArray(handler)) {\n var args = Array.prototype.slice.call(arguments, 1);\n\n var listeners = handler.slice();\n for (var i = 0, l = listeners.length; i < l; i++) {\n listeners[i].apply(this, args);\n }\n return true;\n\n } else {\n return false;\n }\n};\n\n// EventEmitter is defined in src/node_events.cc\n// EventEmitter.prototype.emit() is also defined there.\nEventEmitter.prototype.addListener = function(type, listener) {\n if ('function' !== typeof listener) {\n throw new Error('addListener only takes instances of Function');\n }\n\n if (!this._events) this._events = {};\n\n // To avoid recursion in the case that type == \"newListeners\"! Before\n // adding it to the listeners, first emit \"newListeners\".\n this.emit('newListener', type, listener);\n\n if (!this._events[type]) {\n // Optimize the case of one listener. Don't need the extra array object.\n this._events[type] = listener;\n } else if (isArray(this._events[type])) {\n\n // Check for listener leak\n if (!this._events[type].warned) {\n var m;\n if (this._events.maxListeners !== undefined) {\n m = this._events.maxListeners;\n } else {\n m = defaultMaxListeners;\n }\n\n if (m && m > 0 && this._events[type].length > m) {\n this._events[type].warned = true;\n console.error('(node) warning: possible EventEmitter memory ' +\n 'leak detected. %d listeners added. ' +\n 'Use emitter.setMaxListeners() to increase limit.',\n this._events[type].length);\n console.trace();\n }\n }\n\n // If we've already got an array, just append.\n this._events[type].push(listener);\n } else {\n // Adding the second element, need to change to array.\n this._events[type] = [this._events[type], listener];\n }\n\n return this;\n};\n\nEventEmitter.prototype.on = EventEmitter.prototype.addListener;\n\nEventEmitter.prototype.once = function(type, listener) {\n var self = this;\n self.on(type, function g() {\n self.removeListener(type, g);\n listener.apply(this, arguments);\n });\n\n return this;\n};\n\nEventEmitter.prototype.removeListener = function(type, listener) {\n if ('function' !== typeof listener) {\n throw new Error('removeListener only takes instances of Function');\n }\n\n // does not use listeners(), so no side effect of creating _events[type]\n if (!this._events || !this._events[type]) return this;\n\n var list = this._events[type];\n\n if (isArray(list)) {\n var i = list.indexOf(listener);\n if (i < 0) return this;\n list.splice(i, 1);\n if (list.length == 0)\n delete this._events[type];\n } else if (this._events[type] === listener) {\n delete this._events[type];\n }\n\n return this;\n};\n\nEventEmitter.prototype.removeAllListeners = function(type) {\n // does not use listeners(), so no side effect of creating _events[type]\n if (type && this._events && this._events[type]) this._events[type] = null;\n return this;\n};\n\nEventEmitter.prototype.listeners = function(type) {\n if (!this._events) this._events = {};\n if (!this._events[type]) this._events[type] = [];\n if (!isArray(this._events[type])) {\n this._events[type] = [this._events[type]];\n }\n return this._events[type];\n};\n\n//@ sourceURL=events")); + +require.define("/example/index.js",Function(['require','module','exports','__dirname','__filename','process'],"var selectron = require(\"../index\")\n\nvar sel = selectron()\n , ul = document.getElementById(\"list\")\n\nul.addEventListener(\"click\", function (evt) {\n if (evt.target.tagName === \"LI\") {\n sel.select(evt.target)\n }\n})\n\nsel.on(\"select\", function (li) {\n li.classList.add(\"selected\")\n})\n\nsel.on(\"unselect\", function (li) {\n li.classList.remove(\"selected\")\n})\n//@ sourceURL=/example/index.js")); +require("/example/index.js"); +})(); diff --git a/example/static/index.html b/example/static/index.html new file mode 100644 index 0000000..044b2b2 --- /dev/null +++ b/example/static/index.html @@ -0,0 +1,24 @@ + + + + Your title + + + + + + + + + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..0d14544 --- /dev/null +++ b/index.js @@ -0,0 +1,31 @@ +var EventEmitter = require("events").EventEmitter + +module.exports = selectron + +function selectron() { + var sel = new EventEmitter() + , selected + + sel.select = select + sel.unselect = unselect + + return sel + + function select(thing) { + unselect() + + selected = thing + + selected.select && selected.select() + sel.emit("select", selected) + } + + function unselect() { + if (selected) { + selected.unselect && selected.unselect() + sel.emit("unselect", selected) + } + + selected = null + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..7484c37 --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "selectron", + "version": "0.0.1", + "description": "Keep track of the currently selected thing", + "keywords": [], + "author": "Raynos ", + "repository": "git://github.com/Raynos/selectron.git", + "main": "index", + "homepage": "https://github.com/Raynos/selectron", + "contributors": [ + { + "name": "Jake Verbaten" + } + ], + "bugs": { + "url": "https://github.com/Raynos/selectron/issues", + "email": "raynos2@gmail.com" + }, + "dependencies": {}, + "devDependencies": { + "browserify-server": "~1.0.2" + }, + "licenses": [ + { + "type": "MIT", + "url": "http://github.com/Raynos/selectron/raw/master/LICENSE" + } + ], + "scripts": {} +}