Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Copied files over, not packaged though

  • Loading branch information...
commit a307306d7bc81fc0fc140ea8db7f299e29cba68c 1 parent 734c1d9
Rubén Medellín authored
View
4 README.markdown
@@ -13,6 +13,10 @@ Neon packs a DSL for Class creation, that helps in making programs following an
The philosophy is that it should not try to emulate other languages, so it preserves the JavaScript good parts,
but with a nicer syntax to create classes that ensure interfaces and include reusable functionality as modules.
+== Why another Class System?
+
+As the web applications are getting more complex these times, backend and frontend engineers work has fusioned, and they need to be able to establish a common language. It was created for people coming from OOP languages like Ruby to Javascript.
+
=== Usage
Interface('Editable')({
View
32 lib/bubbling_support.js
@@ -0,0 +1,32 @@
+BubblingSupport = Module('BubblingSupport')({
+ dispatch : function (type, data) {
+ data = data || {};
+ var event = CustomEventSupport.prototype.dispatch.call(this, type, data);
+ if (event.isPropagationStopped === false) {
+ if (this.parent && this.parent.dispatch) {
+ data.target = event.target;
+ data.currentTarget = this.parent;
+ this.parent.dispatch(event.type, data);
+ }
+ }
+ return event;
+ },
+
+ prototype : {
+ dispatch : function (type, data) {
+ data = data || {};
+
+ var event = CustomEventSupport.prototype.dispatch.call(this, type, data);
+
+ if (event.isPropagationStopped === false && event.bubbles === true) {
+ if (this.parent && this.parent.dispatch) {
+ data.target = event.target;
+ data.currentTarget = this.parent;
+ this.parent.dispatch(event.type, data);
+ }
+ }
+
+ return event;
+ }
+ }
+});
View
37 lib/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
171 lib/custom_event_support.js
@@ -0,0 +1,171 @@
+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;
+ }
+ }
+});
View
54 neon.js → lib/neon.js
@@ -79,12 +79,12 @@
include : function(module) {
var property;
for (property in module) {
- if (module.hasOwnProperty(property)
- && property !== 'prototype'
- && property !== 'isModule'
- && property !== '__includedModules'
- && property !== 'include'
- && property !== 'moduleName') {
+ if (module.hasOwnProperty(property) &&
+ property !== 'prototype' &&
+ property !== 'isModule' &&
+ property !== '__includedModules' &&
+ property !== 'include' &&
+ property !== 'moduleName') {
newModule[property] = module[property];
}
}
@@ -110,12 +110,12 @@
newModule.isModule = true;
for (property in definition) {
- if (definition.hasOwnProperty(property)
- && property !== 'prototype'
- && property !== 'isModule'
- && property !== '__includedModules'
- && property !== 'include'
- && property !== 'moduleName') {
+ if (definition.hasOwnProperty(property) &&
+ property !== 'prototype' &&
+ property !== 'isModule' &&
+ property !== '__includedModules' &&
+ property !== 'include' &&
+ property !== 'moduleName') {
newModule[property] = definition[property];
}
}
@@ -141,7 +141,7 @@
};
return factory;
- };
+ };
Neon.Class = function Class(classNameOrNameSpace, className) {
var nameSpace, newClass, classFactory;
@@ -162,12 +162,12 @@
newClass.include = function(module) {
var property;
for (property in module) {
- if (module.hasOwnProperty(property)
- && property != 'prototype'
- && property != 'constructor'
- && property != 'isModule'
- && property != 'include'
- && property != 'superClass') {
+ if (module.hasOwnProperty(property) &&
+ property != 'prototype' &&
+ property != 'constructor' &&
+ property != 'isModule' &&
+ property != 'include' &&
+ property != 'superClass') {
newClass[property] = module[property];
}
}
@@ -210,8 +210,8 @@
}
}
- if (newClass.__implementedInterfaces[i].hasOwnProperty('prototype')
- && newClass.__implementedInterfaces[i].prototype) {
+ 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);
@@ -238,12 +238,12 @@
newClass.prototype.constructor = newClass;
for (i in superClass) {
- if (superClass.hasOwnProperty(i)
- && i != 'prototype'
- && i !== 'className'
- && i !== 'superClass'
- && i !== 'include'
- && i != '__descendants') {
+ if (superClass.hasOwnProperty(i) &&
+ i != 'prototype' &&
+ i !== 'className' &&
+ i !== 'superClass' &&
+ i !== 'include'&&
+ i != '__descendants') {
newClass[i] = superClass[i];
}
}
View
127 lib/node_support.js
@@ -0,0 +1,127 @@
+(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 ];
+ }
+ }
+});
+
+}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : null)));
View
2  package.json
@@ -1,7 +1,7 @@
{
"name" : "neon",
"version" : "1.0.1",
- "main" : "./neon",
+ "main" : "./lib/neon",
"repository": {
"type" : "git",
"url" : "https://github.com/azendal/neon.git"
Please sign in to comment.
Something went wrong with that request. Please try again.