Permalink
Browse files

Neon 2.0.0: Now in the global as in nitrogen. Yay

  • Loading branch information...
1 parent 14173f1 commit fa83290d713bbb4bf913b538f572db7ac6537590 @rbdr rbdr committed Feb 16, 2014
View
1 .gitignore
@@ -0,0 +1 @@
+node_modules
View
14 CHANGELOG.markdown
@@ -1,6 +1,11 @@
-1.0.0 (2012-06-23)
+2.0.0 (2014-02-15)
- - Cleaned up and packed Neon library, used already in production. Added usage files, improved documentation.
+ - Requiring now adds Class, Module, Interface to global scope (neon)
+ - You can now require the stdlib with require('neon/stdlib')
+ - Requiring now adds CustomEvent, CustomEventSupport, NodeSupport
+ and BubblingSupport to the global scope (stdlib)
+ - Major version bump because it's not compatible with 1.x (The other
+ was loaded in a namespace)
1.1.0 (2014-01-29)
@@ -10,3 +15,8 @@
- BubblingSupport
- Added tests for stdlib
- Moved files to /lib
+
+1.0.0 (2012-06-23)
+
+ - Cleaned up and packed Neon library, used already in production.
+ Added usage files, improved documentation.
View
285 lib/neon.js
@@ -1,285 +0,0 @@
-/**
- * neon.js v1.0.0 http://github.com/azendal/neon
- *
- * Class DSL
- * This files provides a dsl for the following design patterns:
- * inheritance, interface, module.
- *
- * It also provides a dsl for class creation.
- *
- * The philosophy is that it should not try to emulate other languages,
- * and it preserves the javascript good parts, but with a nicer syntax to
- * create classes that ensure interfaces and include reusable functionality as modules.
- *
- * Author: Fernando Trasviña
- *
- * Usage:
- * Interface('Editable')({
- * constructor : ['x'],
- * prototype : ['x']
- * });
- *
- * Module('Composition')({
- * y : 5,
- * prototype : {
- * z : 3
- * }
- * });
- *
- * Module('Other')({
- * a : 5,
- * prototype : {
- * b : 3
- * }
- * });
- *
- * Class('Overlay').inherits(Widget).ensures(Editable).includes(Composition, Other)({
- * html : '<div></div>',
- * prototype : {
- * init : function (element) {
- * if(!element) {
- * element = document.createElement('div');
- * element.innerHTML = 'hello';
- * document.body.appendChild(element);
- * }
- * },
- * b : 5
- * }
- * });
- */
-(function(global) {
-
- var Neon = {};
-
- Neon.Interface = function Interface(nameOrNameSpace, name) {
- var nameSpace, interfaceName, factory;
- nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this;
- interfaceName = (nameOrNameSpace && name) ? name :
- (nameOrNameSpace) ? nameOrNameSpace : 'interface' + Math.random().toString();
- factory = function(definition) {
- definition.isInterface = true;
- definition.name = interfaceName;
- nameSpace[interfaceName] = definition;
- return nameSpace[interfaceName];
- };
- return factory;
- };
-
- Neon.Module = function Module(nameOrNameSpace, name) {
- var nameSpace, moduleName, factory, newModule;
-
- nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this;
- moduleName = (nameOrNameSpace && name) ? name :
- (nameOrNameSpace) ? nameOrNameSpace : 'module' + Math.random().toString();
-
- newModule = {
- moduleName : moduleName,
- prototype : {},
- __includedModules : [],
- include : function(module) {
- var property;
- for (property in module) {
- if (module.hasOwnProperty(property) &&
- property !== 'prototype' &&
- property !== 'isModule' &&
- property !== '__includedModules' &&
- property !== 'include' &&
- property !== 'moduleName') {
- newModule[property] = module[property];
- }
- }
-
- if (module.hasOwnProperty('prototype') && module.prototype) {
- for (property in module.prototype) {
- if (module.prototype.hasOwnProperty(property)) {
- newModule.prototype[property] = module.prototype[property];
- }
- }
- }
-
- this.__includedModules.push(module);
-
- return this;
- }
- };
-
-
- factory = function(definition) {
- var property;
-
- newModule.isModule = true;
-
- for (property in definition) {
- if (definition.hasOwnProperty(property) &&
- property !== 'prototype' &&
- property !== 'isModule' &&
- property !== '__includedModules' &&
- property !== 'include' &&
- property !== 'moduleName') {
- newModule[property] = definition[property];
- }
- }
-
- if (definition.hasOwnProperty('prototype') && definition.prototype) {
- for (property in definition.prototype) {
- if (definition.prototype.hasOwnProperty(property)) {
- newModule.prototype[property] = definition.prototype[property];
- }
- }
- }
-
- nameSpace[moduleName] = newModule;
-
- return nameSpace[moduleName];
- };
-
- factory.includes = function () {
- for(var i = 0; i < arguments.length; i++) {
- newModule.include(arguments[i]);
- }
- return factory;
- };
-
- return factory;
- };
-
- Neon.Class = function Class(classNameOrNameSpace, className) {
- var nameSpace, newClass, classFactory;
- nameSpace = (classNameOrNameSpace && className) ? classNameOrNameSpace : this;
- className = (classNameOrNameSpace && className) ? className :
- (classNameOrNameSpace) ? classNameOrNameSpace : 'class' + Math.random().toString();
-
- newClass = function() {
- if (this.init) {
- this.init.apply(this, arguments);
- }
- };
-
- newClass.__descendants = [];
- newClass.__implementedInterfaces = [];
- newClass.__includedModules = [];
- newClass.className = className;
-
- newClass.include = function(module) {
- var property;
- for (property in module) {
- if (module.hasOwnProperty(property) &&
- property != 'prototype' &&
- property != 'constructor' &&
- property != 'isModule' &&
- property != 'include' &&
- property != 'superClass') {
- newClass[property] = module[property];
- }
- }
-
- if (module.hasOwnProperty('prototype') && module.prototype) {
- for (property in module.prototype) {
- if (module.prototype.hasOwnProperty(property)) {
- newClass.prototype[property] = module.prototype[property];
- }
- }
- } else {
- module.prototype = {};
- }
-
- newClass.__includedModules.push(module);
- return this;
- };
-
- classFactory = function(classDefinition) {
- var i, il, j, jl, property, classPrototype = classDefinition.prototype;
- if (classPrototype) {
- for (property in classPrototype) {
- if (classPrototype.hasOwnProperty(property)) {
- newClass.prototype[property] = classPrototype[property];
- }
- }
- delete classDefinition.prototype;
- }
- for (property in classDefinition) {
- if (classDefinition.hasOwnProperty(property)) {
- newClass[property] = classDefinition[property];
- }
- }
-
- for (i = 0, il = newClass.__implementedInterfaces.length; i < il; i++) {
- for (j = 0, jl = newClass.__implementedInterfaces[i].constructor.length; j < jl; j++) {
- if (!newClass[ newClass.__implementedInterfaces[i].constructor[j] ]) {
- console.log('must implement static ' + newClass.__implementedInterfaces[i].name);
- break;
- }
- }
-
- if (newClass.__implementedInterfaces[i].hasOwnProperty('prototype') &&
- newClass.__implementedInterfaces[i].prototype) {
- for (j = 0, jl = newClass.__implementedInterfaces[i].prototype.length; j < jl; j++) {
- if (!newClass.prototype[newClass.__implementedInterfaces[i].prototype[j]]) {
- console.log('must implement prototype ' + newClass.__implementedInterfaces[i].name);
- break;
- }
- }
- }
- }
-
- nameSpace[className] = newClass;
- return newClass;
- };
-
- classFactory.inherits = function(superClass) {
- var i, inheritedClass;
- newClass.superClass = superClass;
- if (superClass.hasOwnProperty('__descendants')) {
- superClass.__descendants.push(newClass);
- }
- inheritedClass = function() {
- };
- inheritedClass.prototype = superClass.prototype;
- newClass.prototype = new inheritedClass();
- newClass.prototype.constructor = newClass;
-
- for (i in superClass) {
- if (superClass.hasOwnProperty(i) &&
- i != 'prototype' &&
- i !== 'className' &&
- i !== 'superClass' &&
- i !== 'include'&&
- i != '__descendants') {
- newClass[i] = superClass[i];
- }
- }
-
- delete this.inherits;
- return this;
- };
-
- classFactory.ensures = function(interfaces) {
- for (var i = 0; i < arguments.length; i++) {
- newClass.__implementedInterfaces.push(arguments[i]);
- }
- delete this.ensures;
- return classFactory;
- };
-
- classFactory.includes = function() {
- for (var i = 0; i < arguments.length; i++) {
- newClass.include(arguments[i]);
- }
- return classFactory;
- };
-
- return classFactory;
-
- };
-
- if (typeof define === 'function') {
- define(function() {
- return Neon;
- });
- } else {
- global.Class = Neon.Class;
- global.Module = Neon.Module;
- global.Interface = Neon.Interface;
- }
-
-}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : null)));
View
53 lib/stdlib/custom_event.js
@@ -1,53 +0,0 @@
-if(typeof exports !== 'undefined') {
- // assume we are in Node
- var Class = require('../neon.js').Class;
-}
-
-(function(global) {
- Class('CustomEvent')({
- prototype : {
- bubbles : true,
- cancelable : true,
- currentTarget : null,
- timeStamp : 0,
- target : null,
- type : '',
- isPropagationStopped : false,
- isDefaultPrevented : false,
- isImmediatePropagationStopped : false,
- areImmediateHandlersPrevented : false,
- init : function init(type, data) {
- this.type = type;
- if (typeof data !== 'undefined') {
- for(var property in data) {
- if (data.hasOwnProperty(property)) {
- this[property] = data[property];
- }
- }
- }
- },
- stopPropagation : function stopPropagation() {
- this.isPropagationStopped = true;
- },
- preventDefault : function preventDefault() {
- this.isDefaultPrevented = true;
- },
- stopImmediatePropagation : function stopImmediatePropagation() {
- this.preventImmediateHandlers();
- this.stopPropagation();
- },
- preventImmediateHandlers : function preventImmediateHandlers() {
- this.areImmediateHandlersPrevented = true;
- }
- }
- });
-
- if (typeof define === 'function') {
- define(function() {
- return CustomEvent;
- });
- } else {
- global.CustomEvent = CustomEvent;
- }
-
-}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : null)));
View
198 lib/stdlib/custom_event_support.js
@@ -1,198 +0,0 @@
-if(typeof exports !== 'undefined') {
- // assume we are in Node
- var Module = require('../neon.js').Module;
- var CustomEvent = require('./custom_event.js').CustomEvent;
-}
-
-(function(global) {
-
- CustomEventSupport = Module('CustomEventSupport')({
-
- eventListeners : null,
-
- bind : function(type, eventHandler) {
- var found, i, listeners;
-
- if(!this.eventListeners) {
- this.eventListeners = {};
- }
-
- if(!this.eventListeners[type]) {
- this.eventListeners[type] = [];
- }
-
- found = false;
-
- listeners = this.eventListeners[type];
- for (i = 0; i < listeners.length; i++) {
- if (listeners[i] === eventHandler) {
- found = true;
- break;
- }
- }
-
- if(!found) {
- this.eventListeners[type].push(eventHandler);
- }
-
- return this;
- },
-
- unbind : function(type, eventHandler) {
- var i, found, listeners;
-
- found = false;
-
- if(!this.eventListeners) {
- this.eventListeners = {};
- }
-
- if(typeof eventHandler == 'undefined') {
- this.eventListeners[type] = [];
- }
-
- listeners = this.eventListeners[type];
- for (i = 0; i < listeners.length; i++) {
- if(listeners[i] === eventHandler) {
- found = true;
- break;
- }
- }
-
- if(found) {
- this.eventListeners[type].splice(i, 1);
- }
-
- return this;
- },
-
- dispatch : function(type, data) {
- var event, listeners, instance, i;
-
- if (this.eventListeners === null) {
- this.eventListeners = {};
- }
-
- if (typeof data === 'undefined') {
- data = {};
- }
-
- if (data.hasOwnProperty('target') === false) {
- data.target = this;
- }
-
- event = new CustomEvent(type, data);
- listeners = this.eventListeners[type] || [];
- instance = this;
-
- for (i = 0; i < listeners.length; i = i + 1) {
- listeners[i].call(instance, event);
- if (event.areImmediateHandlersPrevented === true) {
- break;
- }
- }
-
- return event;
- },
-
- prototype : {
-
- eventListeners : null,
-
- bind : function(type, eventHandler) {
- var found, i, listeners;
-
- if(!this.eventListeners) {
- this.eventListeners = {};
- }
-
- if(!this.eventListeners[type]) {
- this.eventListeners[type] = [];
- }
-
- found = false;
-
- listeners = this.eventListeners[type];
- for (i = 0; i < listeners.length; i++) {
- if(listeners[i] === eventHandler) {
- found = true;
- break;
- }
- }
-
- if(!found) {
- this.eventListeners[type].push(eventHandler);
- }
-
- return this;
- },
-
- unbind : function(type, eventHandler) {
- var i, found, listeners;
-
- found = false;
- i = 0;
-
- if(!this.eventListeners) {
- this.eventListeners = {};
- }
-
- if(typeof eventHandler == 'undefined') {
- this.eventListeners[type] = [];
- }
-
- listeners = this.eventListeners[type];
- for (i = 0; i < listeners.length; i++) {
- if(listeners[i] == eventHandler) {
- found = true;
- break;
- }
- }
-
- if(found) {
- this.eventListeners[type].splice(i, 1);
- }
-
- return this;
- },
-
- dispatch : function(type, data) {
- var event, listeners, instance, i;
-
- if (this.eventListeners === null) {
- this.eventListeners = {};
- }
-
- if (typeof data === 'undefined') {
- data = {};
- }
-
- if (data.hasOwnProperty('target') === false) {
- data.target = this;
- }
-
- event = new CustomEvent(type, data);
- listeners = this.eventListeners[type] || [];
- instance = this;
-
- for (i = 0; i < listeners.length; i = i + 1) {
- listeners[i].call(instance, event);
- if (event.areImmediateHandlersPrevented === true) {
- break;
- }
- }
-
- return event;
- }
- }
- });
-
- if (typeof define === 'function') {
- define(function() {
- return CustomEventSupport;
- });
- } else {
- global.CustomEventSupport = CustomEventSupport;
- }
-
-}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : null)));
View
139 lib/stdlib/node_support.js
@@ -1,139 +0,0 @@
-if(typeof exports !== 'undefined') {
- // assume we are in Node
- var Module = require('../neon.js').Module;
-}
-
-(function(global) {
- NodeSupport = Module('NodeSupport')({
- prototype : {
- parent : null,
-
- children : [],
-
- appendChild : function(child) {
- if(child.parent) {
- child.parent.removeChild(child);
- }
-
- if(!this.hasOwnProperty('children')) {
- this.children = [];
- }
-
- this.children.push(child);
- this[child.name] = child;
- child.setParent(this);
- return child;
- },
-
- insertBefore : function (child, beforeChild) {
- var position;
-
- if (child.parent) {
- child.parent.removeChild(child);
- }
-
- if (!this.hasOwnProperty('children')) {
- this.children = [];
- }
-
- if (typeof beforeChild === 'undefined') {
- this.appendChild(child);
- } else {
- position = this.children.indexOf(beforeChild);
- this.children.splice(position, 0, child);
-
- this[child.name] = child;
- child.setParent(this);
- }
-
- return child;
-
- },
-
- insertChild : function(child, position) {
- console.warn('NodeSupport insertChild method is deprecated, try insertBefore');
-
- if (child.parent) {
- child.parent.removeChild(child);
- }
-
- if (!this.hasOwnProperty('children')) {
- this.children = [];
- }
-
- if (typeof position == 'undefined') {
- this.children.push(child);
- this[child.name] = child;
- child.setParent(this);
- return child;
- }
-
- this.children.splice(position, 0, child);
- this[child.name] = child;
- child.setParent(this);
- return child;
- },
-
- removeChild : function (child) {
- var position = this.children.indexOf(child);
-
- if (position !== -1) {
- this.children.splice(position, 1);
- delete this[child.name];
- child.parent = null;
- }
-
- return child;
- },
-
- setParent : function (parent) {
- this.parent = parent;
- return this;
- },
-
- getDescendants : function () {
- var nodes = [];
- this.children.forEach(function (node) {
- nodes.push(node);
- });
- this.children.forEach(function (node) {
- nodes = nodes.concat(node.getDescendants());
- });
- return nodes;
- },
-
- getPreviousSibling : function () {
- if (typeof this.parent === 'undefined') {
- return;
- }
-
- if (this.parent.children[0] === this) {
- return;
- }
-
- return this.parent.children[ this.parent.children.indexOf(this) - 1 ];
- },
-
- getNextSibling : function () {
- if (typeof this.parent === 'undefined') {
- return;
- }
-
- if (this.parent.children[ this.parent.children.length - 1 ] === this) {
- return;
- }
-
- return this.parent.children[ this.parent.children.indexOf(this) + 1 ];
- }
- }
- });
-
- if (typeof define === 'function') {
- define(function() {
- return NodeSupport;
- });
- } else {
- global.NodeSupport = NodeSupport;
- }
-
-}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : null)));
View
234 neon.js
@@ -0,0 +1,234 @@
+
+if (typeof global === "undefined") {
+ global = window;
+}
+
+global.Interface = function Interface(nameOrNameSpace, name) {
+ var nameSpace, interfaceName, factory;
+ nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this;
+ interfaceName = (nameOrNameSpace && name) ? name :
+ (nameOrNameSpace) ? nameOrNameSpace : 'interface' + Math.random().toString();
+ factory = function(definition) {
+ definition.isInterface = true;
+ definition.name = interfaceName;
+ nameSpace[interfaceName] = definition;
+ return nameSpace[interfaceName];
+ };
+ return factory;
+};
+
+global.Module = function Module(nameOrNameSpace, name) {
+ var nameSpace, moduleName, factory, newModule;
+
+ nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this;
+ moduleName = (nameOrNameSpace && name) ? name :
+ (nameOrNameSpace) ? nameOrNameSpace : 'module' + Math.random().toString();
+
+ newModule = {
+ moduleName : moduleName,
+ prototype : {},
+ __includedModules : [],
+ include : function(module) {
+ var property;
+ for (property in module) {
+ if (module.hasOwnProperty(property)
+ && property !== 'prototype'
+ && property !== 'isModule'
+ && property !== '__includedModules'
+ && property !== 'include'
+ && property !== 'moduleName') {
+ newModule[property] = module[property];
+ }
+ }
+
+ if (module.hasOwnProperty('prototype') && module.prototype) {
+ for (property in module.prototype) {
+ if (module.prototype.hasOwnProperty(property)) {
+ newModule.prototype[property] = module.prototype[property];
+ }
+ }
+ }
+ else {
+ module.prototype = {};
+ }
+
+ this.__includedModules.push(module);
+
+ return this;
+ }
+ }
+
+ factory = function(definition){
+ var property;
+
+ newModule.isModule = true;
+
+ for (property in definition) {
+ if (definition.hasOwnProperty(property)
+ && property !== 'prototype'
+ && property !== 'isModule'
+ && property !== '__includedModules'
+ && property !== 'include'
+ && property !== 'moduleName') {
+ newModule[property] = definition[property];
+ }
+ }
+
+ if (definition.hasOwnProperty('prototype') && definition.prototype) {
+ for (property in definition.prototype) {
+ if (definition.prototype.hasOwnProperty(property)) {
+ newModule.prototype[property] = definition.prototype[property];
+ }
+ }
+ }
+
+ nameSpace[moduleName] = newModule;
+
+ return nameSpace[moduleName];
+ };
+
+ factory.includes = function () {
+ for(var i = 0; i < arguments.length; i++){
+ newModule.include(arguments[i]);
+ }
+ return factory;
+ };
+
+ return factory;
+};
+
+global.Class = function Class(classNameOrNameSpace, className) {
+ var nameSpace, newClass, classFactory;
+ nameSpace = (classNameOrNameSpace && className) ? classNameOrNameSpace : global;
+ className = (classNameOrNameSpace && className) ? className :
+ (classNameOrNameSpace) ? classNameOrNameSpace : 'class' + Math.random().toString();
+
+ newClass = function() {
+ if (this.init) {
+ this.init.apply(this, arguments);
+ }
+ };
+
+ newClass.__descendants = [];
+ newClass.__implementedInterfaces = [];
+ newClass.__includedModules = [];
+ newClass.className = className;
+ newClass.include = function(module) {
+ var property;
+ for (property in module) {
+ if (module.hasOwnProperty(property)
+ && property != 'prototype'
+ && property != 'constructor'
+ && property != 'isModule'
+ && property != 'superClass'
+ && property != 'include') {
+ newClass[property] = module[property];
+ }
+ }
+
+ if (module.hasOwnProperty('prototype') && module.prototype) {
+ for (property in module.prototype) {
+ if (module.prototype.hasOwnProperty(property)) {
+ newClass.prototype[property] = module.prototype[property];
+ }
+ }
+ } else {
+ module.prototype = {};
+ }
+
+ newClass.__includedModules.push(module);
+ return this;
+ };
+
+ classFactory = function(classDefinition) {
+ var i, il, j, jl, property, classPrototype = classDefinition.prototype;
+ if (classPrototype) {
+ for (property in classPrototype) {
+ if (classPrototype.hasOwnProperty(property)) {
+ newClass.prototype[property] = classPrototype[property];
+ }
+ }
+ delete classDefinition.prototype;
+ }
+ for (property in classDefinition) {
+ if (classDefinition.hasOwnProperty(property)) {
+ newClass[property] = classDefinition[property];
+ }
+ }
+
+ for (i = 0, il = newClass.__implementedInterfaces.length; i < il; i++) {
+ for (j = 0, jl = newClass.__implementedInterfaces[i].constructor.length; j < jl; j++) {
+ if (!newClass[ newClass.__implementedInterfaces[i].constructor[j] ]) {
+ console.log('must implement static ' + newClass.__implementedInterfaces[i].name);
+ break;
+ }
+ }
+
+ if (newClass.__implementedInterfaces[i].hasOwnProperty('prototype')
+ && newClass.__implementedInterfaces[i].prototype) {
+ for (j = 0, jl = newClass.__implementedInterfaces[i].prototype.length; j < jl; j++) {
+ if (!newClass.prototype[newClass.__implementedInterfaces[i].prototype[j]]) {
+ console.log('must implement prototype ' + newClass.__implementedInterfaces[i].name);
+ break;
+ }
+ }
+ }
+ }
+
+ try {
+ if (Li && Li.ObjectSpy && Li.Spy) {
+ newClass.__objectSpy = new Li.ObjectSpy();
+ newClass.__objectSpy.spy(newClass);
+ newClass.__objectSpy.spy(newClass.prototype);
+ }
+ } catch (error) {}
+
+ nameSpace[className] = newClass;
+ return newClass;
+ };
+
+ classFactory.inherits = function(superClass) {
+ var i, inheritedClass;
+ newClass.superClass = superClass;
+ if (superClass.hasOwnProperty('__descendants')) {
+ superClass.__descendants.push(newClass);
+ }
+ inheritedClass = function() {
+ };
+ inheritedClass.prototype = superClass.prototype;
+ newClass.prototype = new inheritedClass();
+ newClass.prototype.constructor = newClass;
+
+ for (i in superClass) {
+ if (superClass.hasOwnProperty(i)
+ && i != 'prototype'
+ && i !== 'className'
+ && i !== 'superClass'
+ && i !== 'include'
+ && i != '__descendants') {
+ newClass[i] = superClass[i];
+ }
+ }
+
+ delete this.inherits;
+ return this;
+ };
+
+ classFactory.ensures = function(interfaces) {
+ for (var i = 0; i < arguments.length; i++) {
+ newClass.__implementedInterfaces.push(arguments[i]);
+ }
+ delete this.ensures;
+ return classFactory;
+ };
+
+ classFactory.includes = function() {
+ for (var i = 0; i < arguments.length; i++) {
+ newClass.include(arguments[i]);
+ }
+ return classFactory;
+ };
+
+ return classFactory;
+
+};
View
9 package.json
@@ -1,8 +1,11 @@
{
- "name" : "neon",
- "version" : "1.1.0",
+ "name" : "neon",
+ "version" : "2.0.0",
"description" : "Javascript DSL for Classical Inheritance",
- "main" : "./lib/neon",
+ "main" : "./neon",
+ "directories" : {
+ "lib" : "./lib"
+ },
"repository": {
"type" : "git",
"url" : "https://github.com/azendal/neon.git"
View
20 lib/stdlib/bubbling_support.js → stdlib/bubbling_support.js
@@ -1,12 +1,4 @@
-if(typeof exports !== 'undefined') {
- // assume we are in Node
- var Module = require('../neon.js').Module;
- var CustomEventSupport = require('./custom_event_support.js').CustomEventSupport;
-}
-
-(function(global) {
-
- BubblingSupport = Module('BubblingSupport')({
+Module('BubblingSupport')({
dispatch : function (type, data) {
data = data || {};
var event = CustomEventSupport.prototype.dispatch.call(this, type, data);
@@ -38,13 +30,3 @@ if(typeof exports !== 'undefined') {
}
}
});
-
- if (typeof define === 'function') {
- define(function() {
- return BubblingSupport;
- });
- } else {
- global.BubblingSupport = BubblingSupport;
- }
-
-}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : null)));
View
37 stdlib/custom_event.js
@@ -0,0 +1,37 @@
+Class('CustomEvent')({
+ prototype : {
+ bubbles : true,
+ cancelable : true,
+ currentTarget : null,
+ timeStamp : 0,
+ target : null,
+ type : '',
+ isPropagationStopped : false,
+ isDefaultPrevented : false,
+ isImmediatePropagationStopped : false,
+ areImmediateHandlersPrevented : false,
+ init : function init(type, data) {
+ this.type = type;
+ if (typeof data !== 'undefined') {
+ for(var property in data) {
+ if (data.hasOwnProperty(property)) {
+ this[property] = data[property];
+ }
+ }
+ }
+ },
+ stopPropagation : function stopPropagation() {
+ this.isPropagationStopped = true;
+ },
+ preventDefault : function preventDefault() {
+ this.isDefaultPrevented = true;
+ },
+ stopImmediatePropagation : function stopImmediatePropagation() {
+ this.preventImmediateHandlers();
+ this.stopPropagation();
+ },
+ preventImmediateHandlers : function preventImmediateHandlers() {
+ this.areImmediateHandlersPrevented = true;
+ }
+ }
+});
View
180 stdlib/custom_event_support.js
@@ -0,0 +1,180 @@
+Module('CustomEventSupport')({
+
+ eventListeners : null,
+
+ bind : function(type, eventHandler) {
+ var found, i, listeners;
+
+ if(!this.eventListeners) {
+ this.eventListeners = {};
+ }
+
+ if(!this.eventListeners[type]) {
+ this.eventListeners[type] = [];
+ }
+
+ found = false;
+
+ listeners = this.eventListeners[type];
+ for (i = 0; i < listeners.length; i++) {
+ if (listeners[i] === eventHandler) {
+ found = true;
+ break;
+ }
+ }
+
+ if(!found) {
+ this.eventListeners[type].push(eventHandler);
+ }
+
+ return this;
+ },
+
+ unbind : function(type, eventHandler) {
+ var i, found, listeners;
+
+ found = false;
+
+ if(!this.eventListeners) {
+ this.eventListeners = {};
+ }
+
+ if(typeof eventHandler == 'undefined') {
+ this.eventListeners[type] = [];
+ }
+
+ listeners = this.eventListeners[type];
+ for (i = 0; i < listeners.length; i++) {
+ if(listeners[i] === eventHandler) {
+ found = true;
+ break;
+ }
+ }
+
+ if(found) {
+ this.eventListeners[type].splice(i, 1);
+ }
+
+ return this;
+ },
+
+ dispatch : function(type, data) {
+ var event, listeners, instance, i;
+
+ if (this.eventListeners === null) {
+ this.eventListeners = {};
+ }
+
+ if (typeof data === 'undefined') {
+ data = {};
+ }
+
+ if (data.hasOwnProperty('target') === false) {
+ data.target = this;
+ }
+
+ event = new CustomEvent(type, data);
+ listeners = this.eventListeners[type] || [];
+ instance = this;
+
+ for (i = 0; i < listeners.length; i = i + 1) {
+ listeners[i].call(instance, event);
+ if (event.areImmediateHandlersPrevented === true) {
+ break;
+ }
+ }
+
+ return event;
+ },
+
+ prototype : {
+
+ eventListeners : null,
+
+ bind : function(type, eventHandler) {
+ var found, i, listeners;
+
+ if(!this.eventListeners) {
+ this.eventListeners = {};
+ }
+
+ if(!this.eventListeners[type]) {
+ this.eventListeners[type] = [];
+ }
+
+ found = false;
+
+ listeners = this.eventListeners[type];
+ for (i = 0; i < listeners.length; i++) {
+ if(listeners[i] === eventHandler) {
+ found = true;
+ break;
+ }
+ }
+
+ if(!found) {
+ this.eventListeners[type].push(eventHandler);
+ }
+
+ return this;
+ },
+
+ unbind : function(type, eventHandler) {
+ var i, found, listeners;
+
+ found = false;
+ i = 0;
+
+ if(!this.eventListeners) {
+ this.eventListeners = {};
+ }
+
+ if(typeof eventHandler == 'undefined') {
+ this.eventListeners[type] = [];
+ }
+
+ listeners = this.eventListeners[type];
+ for (i = 0; i < listeners.length; i++) {
+ if(listeners[i] == eventHandler) {
+ found = true;
+ break;
+ }
+ }
+
+ if(found) {
+ this.eventListeners[type].splice(i, 1);
+ }
+
+ return this;
+ },
+
+ dispatch : function(type, data) {
+ var event, listeners, instance, i;
+
+ if (this.eventListeners === null) {
+ this.eventListeners = {};
+ }
+
+ if (typeof data === 'undefined') {
+ data = {};
+ }
+
+ if (data.hasOwnProperty('target') === false) {
+ data.target = this;
+ }
+
+ event = new CustomEvent(type, data);
+ listeners = this.eventListeners[type] || [];
+ instance = this;
+
+ for (i = 0; i < listeners.length; i = i + 1) {
+ listeners[i].call(instance, event);
+ if (event.areImmediateHandlersPrevented === true) {
+ break;
+ }
+ }
+
+ return event;
+ }
+ }
+});
View
7 stdlib/index.js
@@ -0,0 +1,7 @@
+// This file is node only
+if(typeof require !== 'undefined'){
+ require('./custom_event');
+ require('./custom_event_support');
+ require('./node_support');
+ require('./bubbling_support');
+}
View
123 stdlib/node_support.js
@@ -0,0 +1,123 @@
+Module('NodeSupport')({
+ prototype : {
+ parent : null,
+
+ children : [],
+
+ appendChild : function(child) {
+ if(child.parent) {
+ child.parent.removeChild(child);
+ }
+
+ if(!this.hasOwnProperty('children')) {
+ this.children = [];
+ }
+
+ this.children.push(child);
+ this[child.name] = child;
+ child.setParent(this);
+ return child;
+ },
+
+ insertBefore : function (child, beforeChild) {
+ var position;
+
+ if (child.parent) {
+ child.parent.removeChild(child);
+ }
+
+ if (!this.hasOwnProperty('children')) {
+ this.children = [];
+ }
+
+ if (typeof beforeChild === 'undefined') {
+ this.appendChild(child);
+ } else {
+ position = this.children.indexOf(beforeChild);
+ this.children.splice(position, 0, child);
+
+ this[child.name] = child;
+ child.setParent(this);
+ }
+
+ return child;
+
+ },
+
+ insertChild : function(child, position) {
+ console.warn('NodeSupport insertChild method is deprecated, try insertBefore');
+
+ if (child.parent) {
+ child.parent.removeChild(child);
+ }
+
+ if (!this.hasOwnProperty('children')) {
+ this.children = [];
+ }
+
+ if (typeof position == 'undefined') {
+ this.children.push(child);
+ this[child.name] = child;
+ child.setParent(this);
+ return child;
+ }
+
+ this.children.splice(position, 0, child);
+ this[child.name] = child;
+ child.setParent(this);
+ return child;
+ },
+
+ removeChild : function (child) {
+ var position = this.children.indexOf(child);
+
+ if (position !== -1) {
+ this.children.splice(position, 1);
+ delete this[child.name];
+ child.parent = null;
+ }
+
+ return child;
+ },
+
+ setParent : function (parent) {
+ this.parent = parent;
+ return this;
+ },
+
+ getDescendants : function () {
+ var nodes = [];
+ this.children.forEach(function (node) {
+ nodes.push(node);
+ });
+ this.children.forEach(function (node) {
+ nodes = nodes.concat(node.getDescendants());
+ });
+ return nodes;
+ },
+
+ getPreviousSibling : function () {
+ if (typeof this.parent === 'undefined') {
+ return;
+ }
+
+ if (this.parent.children[0] === this) {
+ return;
+ }
+
+ return this.parent.children[ this.parent.children.indexOf(this) - 1 ];
+ },
+
+ getNextSibling : function () {
+ if (typeof this.parent === 'undefined') {
+ return;
+ }
+
+ if (this.parent.children[ this.parent.children.length - 1 ] === this) {
+ return;
+ }
+
+ return this.parent.children[ this.parent.children.indexOf(this) + 1 ];
+ }
+ }
+});
View
2 test/neon_browser.html
@@ -2,7 +2,7 @@
<html>
<head>
<title>Neon</title>
- <script src="../lib/neon.js" type="text/javascript"></script>
+ <script src="../neon.js" type="text/javascript"></script>
<script src="neon_test.js" type="text/javascript"></script>
</head>
<body>
View
12 test/neon_stdlib_browser.html
@@ -2,14 +2,14 @@
<html>
<head>
<title>Neon</title>
- <script src="../lib/neon.js" type="text/javascript"></script>
- <script src="../lib/stdlib/node_support.js" type="text/javascript"></script>
- <script src="../lib/stdlib/custom_event.js" type="text/javascript"></script>
- <script src="../lib/stdlib/custom_event_support.js" type="text/javascript"></script>
- <script src="../lib/stdlib/bubbling_support.js" type="text/javascript"></script>
+ <script src="../neon.js" type="text/javascript"></script>
+ <script src="../stdlib/node_support.js" type="text/javascript"></script>
+ <script src="../stdlib/custom_event.js" type="text/javascript"></script>
+ <script src="../stdlib/custom_event_support.js" type="text/javascript"></script>
+ <script src="../stdlib/bubbling_support.js" type="text/javascript"></script>
<script src="neon_stdlib_test.js" type="text/javascript"></script>
</head>
<body>
<h1>Neon</h1>
</body>
-</html>
+</html>
View
7 test/neon_stdlib_test.js
@@ -1,11 +1,8 @@
// @TODO: Make proper spec'ing and testing for these modules
if(typeof require !== 'undefined') {
-
console.log("Requiring neon stdlib from file");
- var Class = require('../lib/neon.js').Class;
- var CustomEventSupport = require('../lib/stdlib/custom_event_support.js').CustomEventSupport;
- var NodeSupport = require('../lib/stdlib/node_support.js').NodeSupport;
- var BubblingSupport = require('../lib/stdlib/bubbling_support.js').BubblingSupport;
+ require('neon');
+ require('neon/stdlib');
}
console.log("CustomEvent ", typeof CustomEvent !== 'undefined');
View
8 test/neon_test.js
@@ -1,15 +1,9 @@
if(typeof require !== 'undefined') { // We are in Node, need to require the file
console.log("Requiring neon from file");
- var Neon = require('../lib/neon.js');
- console.log(Neon);
-
+ require('neon');
// In Coffeescript, the following construct is recommended
// { Class, Module, Interface } = require('neon')
-
- var Class = Neon.Class;
- var Module = Neon.Module;
- var Interface = Neon.Interface;
}

0 comments on commit fa83290

Please sign in to comment.