common cnt js

Download Common Cnt Js

If you can't read please download the document

Upload: vien-quang-pham

Post on 14-Dec-2015

212 views

Category:

Documents


0 download

DESCRIPTION

Simple idea..

TRANSCRIPT

cnt = (function() {function Gadget() {// function for debugfunction log(o, fn) {if (typeof fn != 'function') {if (console) fn = console.log;else fn = alert;if (o && o.constructor == Array) {fn('length: ' + o.length);for (var i = 0; i < o.length; o++) {fn('o[' + i + '] = ' + o[i]);}} else fn(o);} else fn(o);}/** ----------- private ---------------*/var gadget = {uid: 0, // unique IDprefix: 'cnt-',xtypes: {prop: {},list: [],get: function(xtype) {return this.prop[xtype];},set: function(xtype, oClass) {if (typeof xtype == 'string' && typeof oClass == 'function') {if (!this.get(xtype)) {this.list.push(xtype);this.prop[xtype] = oClass;}}},print: function(fn) {log(this.list, fn);}},hasXType: function(xtype) {return (this.xtypes.get(xtype) ? true : false);},getXType: function(xtype) {return this.xtypes.get(xtype);},components: {},id: function() {return this.prefix + (++this.uid);}};// public accessthis.publicAccess = function(gn) {//this.G = gadget;gn = (typeof gn == 'string' ? gn : 'G');this[gn] = gadget;}// simple copyfunction copy(p, c) {c = c || {};for (var i in p) {c[i] = p[i];}return c;}// deep copy - slowfunction deepCopy(p, c) {var c = c || {}; for (var i in p) {if (typeof p[i] === 'object') {c[i] = (p[i].constructor === Array) ? [] : {};deepCopy(p[i], c[i]);} else {c[i] = p[i];} }return c;}// register objectfunction register(source, name, value) {if (typeof source == 'object' && typeof name == 'string' && name != '') {var test = source[name];if (test) {if (test.locked) return test;else delete test;}source[name] = value || {};return source[name];}}// extend objectfunction extend(child, parent, config) {if (arguments.length == 3) {// may be extend from other classchild = deepCopy(parent, child);return copy(config, child);} else {return copy(parent, child);}}/** ----------- public ---------------*/this.className = 'Gadget';this.prefix = function() { return gadget.prefix; }this.toString = function() { return this.className; }this.log = log;this.copy = copy;this.deepCopy = deepCopy;// manager out-objectsthis.manager = {//private: true,locked: true,hash: {},get: function(name) { return this.hash[name]; },set: function(name, object) { this.hash[name] = object; }}// short-hand manager getthis.get = function(id) {var el = this.manager.get(id);if (!el) {el = document.getElementById(id);}return el;}// short-hand manager setthis.set = function(id, object) {this.manager.set(id, object);}// get uniqueIDthis.id = function() {var uniqueID = gadget.id();if (this.get(uniqueID))return this.id();elsereturn uniqueID;}// register new objectthis.reg = function(name, o) {if (typeof o == 'object' && o.private) {return register(gadget.components, name, o);}return register(this, name, o);}// register, get x-type (new class)this.regx = function(xtype, oClass) {if (!gadget.hasXType(xtype)) {gadget.xtypes.set(xtype, oClass);}return gadget.getXType(xtype);}// create objectthis.newObject = function(oClass, config) {if (typeof oClass == 'string')oClass = this.regx(oClass);return (new oClass(config));}// create name-spacethis.namespace = function(name) {if (typeof name == 'string') {var parts = name.split('.');var current = this, i = 0;if (parts[0] == 'cnt') {i = 1;}while (i < parts.length) {if (!current[parts[i]]) {current[parts[i]] = {};}current = current[parts[i]];i++; // next.}return current;}}// short-namethis.ns = this.namespace;// get class by name-spacethis.getClass = function(name) {var index = name.lastIndexOf('.');var ns = name.substring(0, index); // name-space.var cn = name.substring(index + 1); // class-name.return {ns: this.ns(ns),cn: cn}}this.destroy = function(o) {if (o) delete o;}}return (new Gadget());}());/** version */cnt.reg('version', 'Common Js v1.0');cnt.reg('support', function(library) {var b = new Boolean(window[library]);return b.valueOf();});cnt.reg('exist', function(object, name) {if (arguments.length == 1) {name = object;object = cnt;}var b = new Boolean(object[name]);return b.valueOf();});// short-namecnt.reg('rx', cnt.regx);cnt.copy({create: (function() {var subclass;this.init = function() {}return function(className, superclass, config) {config = config || {};var oc = cnt.getClass(className);subclass = oc.ns[oc.cn] = (typeof config.init == "function" ?config.init : this.init);var F = function() {},subclassProto, superclassProto = superclass.prototype;F.prototype = superclassProto; subclassProto = subclass.prototype = new F();subclassProto.constructor = subclass;subclass.superclass = superclassProto;// override class namesubclassProto.className = className;cnt.copy(config, subclass);return subclass;}}()),extend: function() {console.log('extend-function');}}, cnt);/**------------------------------ Methods Define -----------------------------*/cnt.reg('initMt', function() {cnt.reg('qsParser', function(strQuery) {var temp, pname, pvalue,query = {};var names = [], values = [];var array = strQuery.split('&');for (var i = 0; i < array.length; i++) {temp = array[i].split('=');pname = temp[0];pvalue = (temp.length > 1 ? temp[1] : '');query[pname] = pvalue;names.push(pname);values.push(pvalue);}// return parserreturn {query: query,names: names,values: values,length: array.length,get: function(pname) {return this.query[pname];},getAt: function(index) {return this.values[index];}}});cnt.reg('parseParams', function(o) {var s = '';if (typeof o == 'object') {for (var i in o) {if (typeof o[i] == 'object') s += cnt.parseParams(o[i]);else s += ('&' + i + '=' + o[i]);}}return s;});// remove this method//cnt.destroy(cnt.initMethods);});/**------------------------------ Methods Define -----------------------------*//**------------------------------ Components Define -----------------------------*/cnt.reg('initCt', function() {cnt.Component = function(config) {this.initConfig = config || {};cnt.copy(this.initConfig, this);this.id = (this.id || cnt.id());}cnt.Component.prototype = {type: 'cmp',className: 'cnt.Component',toString: cnt.toString,cn: function(className) {if (typeof className == 'string')this.className = className;return this.className;},getType: function() {return this.type;},init: function() {console.log('call init ' + this.cn());this.register();}};cnt.rx('cmp', cnt.Component);cnt.ns('cnt.common');cnt.create('cnt.common.Selecter', cnt.Component, {init: function(config) {this.constructor.superclass.init(config);console.log(this)}});cnt.rx('selecter', cnt.common.Selecter);// remove this method//cnt.destroy(cnt.initComponents);});/**------------------------------ Components Define -----------------------------*/