Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simple tests for all modules in the console

Next is testing in the browser
  • Loading branch information...
commit c3680386d3583eea94403235bf62654eb469027f 1 parent 1993719
Rubén Medellín authored
View
32 lib/bubbling_support.js
@@ -1,32 +0,0 @@
-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
@@ -1,37 +0,0 @@
-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
@@ -1,171 +0,0 @@
-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
127 lib/node_support.js
@@ -1,127 +0,0 @@
-(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
50 lib/stdlib/bubbling_support.js
@@ -0,0 +1,50 @@
+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')({
+ 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;
+ }
+ }
+ });
+
+ if (typeof define === 'function') {
+ define(function() {
+ return BubblingSupport;
+ });
+ } else {
+ global.BubblingSupport = BubblingSupport;
+ }
+
+}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : null)));
View
53 lib/stdlib/custom_event.js
@@ -0,0 +1,53 @@
+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
188 lib/stdlib/custom_event_support.js
@@ -0,0 +1,188 @@
+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
@@ -0,0 +1,139 @@
+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
87 test/neon_stdlib_test.js
@@ -0,0 +1,87 @@
+// @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;
+}
+
+console.log("CustomEvent ", typeof CustomEvent !== 'undefined');
+console.log("CustomEventSupport ", typeof CustomEventSupport !== 'undefined');
+console.log("NodeSupport ", typeof NodeSupport !== 'undefined');
+console.log("BubblingSupport ", typeof BubblingSupport !== 'undefined');
+
+console.log("--- node support")
+
+Class('TreeNode').includes(NodeSupport)({
+ prototype : {
+ init : function(name) {
+ this.name = name;
+ }
+ }
+});
+
+var root = new TreeNode('root');
+
+console.log(root);
+
+var left = new TreeNode('left');
+var right = new TreeNode('right');
+
+root.appendChild(left);
+right.setParent(root);
+
+console.log(root.children.indexOf(left));
+console.log(left == right.parent.left);
+
+Class('X').includes(CustomEventSupport)({
+ prototype : {
+ init : function() {
+
+ }
+ }
+});
+
+console.log('--- custom event support');
+
+var sender = new X();
+
+sender.dispatch('alert', { message : 'ERROR: nobody should be receiving yet' });
+sender.bind('alert', function(event) {
+ console.log(event.message == 'TEST');
+});
+
+sender.dispatch('alert', { message : 'TEST' });
+
+console.log('--- bubbling support');
+
+Class('BubblingTreeNode').includes(NodeSupport, CustomEventSupport, BubblingSupport)({
+ prototype : {
+ init : function(name) {
+ this.name = name;
+ this.bind('alert', function(event) {
+ console.log("Bound to alert on init (" + this.name + ")");
+ });
+ }
+ }
+});
+
+var root = new BubblingTreeNode('root');
+var son = root.appendChild(new BubblingTreeNode('son'));
+var grandson = son.appendChild(new BubblingTreeNode('grandson'));
+
+console.log("Bubbles up");
+grandson.dispatch('alert');
+
+console.log("Doesn't bubble down");
+root.dispatch('alert');
+
+// @ TODO : Leave this tests to azendal, I have to read the browser's propagation spec
+// console.log("Prevent default");
+// son.bind('alert', function(event) {
+// console.log(event);
+// event.preventImmediatePropagation();
+// });
+// grandson.dispatch('alert');
Please sign in to comment.
Something went wrong with that request. Please try again.