Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

publish version 0.0.2

  • Loading branch information...
commit 3b65cb5ac1cf4b5e0c586a746aeab77ed19daa49 1 parent 7ceb337
@brighthas brighthas authored
View
3  .gitignore
@@ -13,3 +13,6 @@ results
npm-debug.log
node_modules
+
+build
+components
View
1  .npmignore
@@ -0,0 +1 @@
+build components node_modules
View
0  History.md
No changes.
View
11 Makefile
@@ -0,0 +1,11 @@
+
+build: components index.js
+ @component build --dev
+
+components: component.json
+ @component install --dev
+
+clean:
+ rm -fr build components template.js
+
+.PHONY: clean
View
23 component.json
@@ -0,0 +1,23 @@
+{
+ "name": "tree-node",
+ "repo": "brighthas/tree-node",
+ "description": "tree node",
+ "version": "0.0.1",
+ "keywords": [],
+ "dependencies": {
+ "gingkoapp/object-id": "*",
+ "component/emitter": "*"
+ },
+ "development": {
+ "component/assert":"*",
+ "component/inherit" : "*",
+ "component/type" : "*",
+ "visionmedia/mocha":"*"
+ },
+ "license": "MIT",
+ "main": "index.js",
+ "scripts": [
+ "index.js"
+ ],
+ "remotes": []
+}
View
342 index.js
@@ -0,0 +1,342 @@
+var isBrowser = this.window ? true : false,
+ objectId = isBrowser ? require("object-id") : require("./object-id"),
+ inherits = isBrowser ? require("inherit") : require("util").inherits,
+ EventEmitter = isBrowser ? require("emitter") : require("events").EventEmitter;
+
+module.exports = Node;
+
+
+function Node(){
+ EventEmitter.call(this);
+ this._id = objectId();
+ this._childs = {};
+ this._childIdsList = [];
+ this._parent = null;
+ this._data = {};
+}
+
+if(isBrowser){
+ EventEmitter(o);
+}else{
+ inherits(Node,EventEmitter);
+}
+
+var o = Node.prototype;
+
+o.getChild = function(childId){
+ var child = this._childs[childId];
+ if(child){
+ return child;
+ }else{
+ for(var cid in this._childIdsList){
+ child = this._childs[cid];
+ if(child && child.getChild(childId)){
+ return child;
+ }
+ }
+ return null;
+ }
+}
+
+/**
+* @member Node#createChild
+* @return {Node} , return new child node.
+*/
+o.createChild = function(){
+ var child = new Node();
+ child._parent = this;
+ this._childs[child.id] = child;
+ this._childIdsList.push(child.id);
+ return child;
+}
+
+/**
+* @member Node#appendChild
+* @param {Node} child , child can't contain root.
+* @return {Node} , self.
+*/
+o.appendChild = function(child){
+ if(this.root.getChild(child.id) || child._childIdsList.length !== 0){
+ return this;
+ }else if(child.parent){
+ child.parent.removeChild(child.id);
+ }
+ child._parent = this;
+ this._childs[child.id] = child;
+ this._childIdsList.push(child.id);
+ return this;
+}
+
+o.removeChild = function(childId){
+
+ child = this.getChild(childId);
+
+ var parent;
+
+ if(child && (parent = child._parent)){
+ delete parent._childs[childId];
+ child._parent = null;
+ var index = parent._childIdsList.indexOf(childId);
+ parent._childIdsList.splice(index,1);
+ }
+
+ return this;
+
+}
+
+o.replaceNode = function(child,targetId){
+
+ var target = this.getChild(targetId);
+
+ if( !child || !target || target.getChild(this.id) || child.getChild(this.id) || target.getChild(child.id) ){
+ return this;
+ }
+
+ var parent = child.parent;
+ if(parent){
+ parent.removeChild(child.id);
+ }
+
+ var index = target.position();
+
+ target.parent._childIdsList.splice(index,1,child);
+ child._parent = target.parent;
+ delete target.parent._childs[target.id];
+ target._parent = null;
+
+ return this;
+
+}
+
+o.position = function(childId){
+
+ var child = this._getNode(childId);
+
+ if(child){
+ var parent = child.parent;
+ return parent ? parent._childIdsList.indexOf(child.id) : null;
+ }else{
+ return null;
+ }
+
+}
+
+o.layer = function(childId){
+ var child = this._getNode(childId),layer = 0;
+
+ if(child){
+ var parent = child.parent;
+ while(parent){
+ layer += 1;
+ parent = parent.parent;
+ }
+ return layer;
+ }else{
+ return 0;
+ }
+}
+
+o._getNode = function(childId){
+
+ var child;
+
+ if(childId){
+ if(childId === this.id){
+ child = this;
+ }else{
+ child = this.getChild(childId);
+ }
+ }else{
+ child = this;
+ }
+
+ return child;
+
+}
+
+o.top = function(childId){
+ var child = this._getNode(childId),parent;
+ if(child && (parent = child.parent)){
+ var index = parent._childIdsList.indexOf(childId);
+ parent._childIdsList.splice(index,1);
+ parent._childIdsList.unshift(childId);
+ }
+
+ return this;
+}
+
+o.up = function(childId){
+ var child = this._getNode(childId),parent;
+ if(child && (parent = child.parent)){
+ var index = parent._childIdsList.indexOf(child.id);
+ parent._childIdsList.splice(index,1);
+ if(index !== 0){
+ parent._childIdsList.splice(index-1,0,child.id);
+ }
+ }
+ return this;
+}
+
+
+o.down = function(childId){
+ var child = this._getNode(childId),parent;
+ if(child && (parent = child.parent)){
+ var nextNode = child.nextNode();
+ if(nextNode){
+ nextNode.up();
+ }
+ }
+ return this;
+}
+
+o.nextNode = function(childId){
+ var child = this._getNode(childId);
+ if(child){
+ var parent = child.parent,
+ index = child.position(),
+ nextChildId = parent._childIdsList[index+1];
+
+ if(nextChildId){
+ return parent.getChild(nextChildId);
+ }
+ }
+}
+
+o.prevNode = function(childId){
+ var child = this._getNode(childId);
+ if(child){
+ var parent = child.parent,
+ index = child.position(),
+ prevChildId = parent._childIdsList[index-1];
+
+ if(prevChildId){
+ return parent.getChild(prevChildId);
+ }
+ }
+}
+
+// move childId node to parentId node.
+o.move = function(childId,parentId){
+
+ var child = this.getChild(childId),
+ parent = parentId === this.parent ? this : this.getChild(parentId);
+
+ if( child && parent && !child.getChild(parentId) ){
+ var childParent = child.parent;
+ childParent.removeChild(child.id);
+ parent.appendChild(child);
+ }
+
+ return this;
+
+}
+
+o.isRoot = function(){
+ return this.parent ? false : true ;
+}
+
+o.data = function(){
+
+ var obj = {}
+
+ if(arguments.length === 2){
+ obj[arguments[0]] = arguments[1];
+ }else if(arguments.length === 1){
+ if(typeof arguments[0] === "string"){
+ return this._data[arguments[0]];
+ }else{
+ obj = arguments[0];
+ }
+ }else{
+ return this._data;
+ }
+
+ for(var k in obj){
+ this._data[k] = obj[k];
+ }
+
+}
+
+o.reborn = function(jsonObj){
+
+ if(this._childIdsList.length === 0 && this.isRoot()){
+ var self = this;
+ this._id = jsonObj.id;
+ this._childs = {};
+ this._childIdsList = [];
+ this._parent = jsonObj.parent;
+ this._data = jsonObj.data;
+
+ jsonObj.childIdsList.forEach(function(cid){
+ var child = new Node();
+ child.reborn(jsonObj.childs[cid]);
+ self._childs[cid] = child;
+ });
+ }
+ return this;
+
+}
+
+Object.defineProperties(o,{
+
+ parent : {
+ get : function(){
+ return this._parent;
+ }
+ },
+
+ id: {
+ get : function(){
+ return this._id;
+ }
+ },
+
+ route:{
+ get : function(){
+ var route = [this.id],
+ parent = this.parent;
+ while(parent){
+ route.unshift(parent.id);
+ parent = parent.parent;
+ }
+ return route;
+ }
+ },
+
+ root:{
+ get:function(){
+ var root = this.parent;
+ while(root){
+ root = root.parent;
+ }
+ return root || this;
+ }
+ },
+
+ json:{
+ get : function(){
+
+ var jsonObj = {
+ id : this._id,
+ childs : {},
+ childIdsList : this._childIdsList,
+ parent : this._parent ? this._parent.id : null,
+ data : this._data
+ }, self = this;
+
+ this._childIdsList.forEach(function(cid){
+ jsonObj.childs[cid] = self._childs[cid].json;
+ })
+
+
+
+ return JSON.parse(JSON.stringify(jsonObj));
+
+ }
+ }
+
+});
+
+
+
+
View
16 object-id.js
@@ -0,0 +1,16 @@
+/**
+ * Implementation of ObjectId generator
+ * http://docs.mongodb.org/manual/core/object-id/
+ */
+
+var increment = 0x1000000;
+var localId1 = ((1+Math.random())*0x100000 | 0).toString(16).substring(1);
+var localId2 = ((1+Math.random())*0x100000 | 0).toString(16).substring(1);
+
+function objectId() {
+ var dateNow = ((new Date()).getTime()/100 | 0).toString(16);
+ return dateNow + localId1 + localId2 + (++increment).toString(16).substring(1);
+}
+
+// Expose `objectId`
+module.exports = objectId;
View
27 package.json
@@ -0,0 +1,27 @@
+{
+ "name": "tree-node",
+ "version": "0.0.2",
+ "description": "tree node",
+ "main": "index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/brighthas/tree-node.git"
+ },
+ "keywords": [
+ "tree",
+ "node",
+ "tree",
+ "node"
+ ],
+ "author": "brighthas",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/brighthas/tree-node/issues"
+ },
+ "directories": {
+ "test": "test"
+ }
+}
View
1  test/index.html
@@ -0,0 +1 @@
+<!doctype html>
View
1  test/index.js
@@ -0,0 +1 @@
+
View
32 test/type.js
@@ -0,0 +1,32 @@
+
+/**
+ * toString ref.
+ */
+
+var toString = Object.prototype.toString;
+
+/**
+ * Return the type of `val`.
+ *
+ * @param {Mixed} val
+ * @return {String}
+ * @api public
+ */
+
+module.exports = function(val){
+ switch (toString.call(val)) {
+ case '[object Function]': return 'function';
+ case '[object Date]': return 'date';
+ case '[object RegExp]': return 'regexp';
+ case '[object Arguments]': return 'arguments';
+ case '[object Array]': return 'array';
+ case '[object String]': return 'string';
+ }
+
+ if (val === null) return 'null';
+ if (val === undefined) return 'undefined';
+ if (val && val.nodeType === 1) return 'element';
+ if (val === Object(val)) return 'object';
+
+ return typeof val;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.