浏览 3024 次
锁定老帖子 主题:基于Jquery的练习
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-06
/** * @author: xaofeixa * @date: 2008-04-03 * @purpse: 练习基类 */ // 命名空间 xaofeixa = window.xaofeixa || {}; (function() { var BaseObject = function(options) { //初始化 生成成员的get set方法 BaseObject.prototype.init(this, options); }; xaofeixa.BaseObject = BaseObject; BaseObject.prototype = { action : function(who) { var propertys = new Array(); for (pro in who) { if (typeof(who[pro]) != "function") { propertys[propertys.length] = pro; } } for (i in propertys) { (function() { var property = propertys[i]; property = property.charAt(0).toUpperCase() + property.substring(1, property.length); var propertyValue = who[propertys[i]]; who["get" + property] = function() { return propertyValue; }; who["set" + property] = function(value) { propertyValue = value; }; })(); } }, init : function(_this, options) { var who = _this ? _this : this; if (options) { /* * $.each(options, function(optionName, optionValue) { * _this[optionName] = optionValue; }); */ $.extend(who, options); } this.action(who); } }; })(); /** * @author: xaofeixa * @date: 2008-04-03 * @purpse: test person * */ (function() { var Person = function(options) { Person.prototype.init(this, options); }; xaofeixa.Person = Person; $.extend(Person.prototype, xaofeixa.BaseObject.prototype); })(); /** * @author: xaofeixa * @date: 2008-04-03 * @purpse: DOM操作 */ (function() { var trim = function(str) { if (!str) { str = ""; } return str; } // /////////////////////////// var Document = function(options) { Document.prototype.init(this, options); } $.extend(Document.prototype, xaofeixa.BaseObject.prototype); Document.createDocument = function() { var root = new Element("xml"); return root; }; xaofeixa.Document = Document; // ////////////////////////// var Element = function(elementName, text, options) { this.elementName = elementName; this.text = text; this.parentElement = null; this.sonElements = new Array(); this.attributes = new Array(); Element.prototype.init(this, options); }; $.extend(Element.prototype, xaofeixa.BaseObject.prototype, { setElementName : function(elementName) { this.elementName = elementName; }, getElementName : function() { return this.elementName; }, setText : function(text) { this.text = text; }, getText : function() { return trim(this.text); }, addElement : function(elementName, text) { if (!elementName) { return this; } var element = new Element(elementName, text); element.parentElement = this; this.sonElements[this.sonElements.length] = element; return element; }, removeElement : function() { }, addAttribute : function(attributeName, attributeValue, options) { var _options = null; var _this = this; if (typeof(attributeName) != "object") { var attribute = new Attribute(attributeName, attributeValue); _this.attributes[_this.attributes.length] = attribute; } else { _options = attributeName; } if (options) { _options = options; } if (_options) { $.each(_options, function(attributeName, attributeValue) { var attribute = new Attribute(attributeName, attributeValue); _this.attributes[_this.attributes.length] = attribute; }); } return _this; }, removeAttribute : function() { }, getXmlStr : function() { var xmlStr = "<" + this.getElementName(); // attribute var attributes = this.attributes; for (var i = 0; i < attributes.length; i++) { xmlStr += " " + attributes[i].getAttributeName() + "=\"" + attributes[i].getAttributeValue() + "\""; } xmlStr += ">" + this.getText(); var sonElements = this.sonElements; for (var i = 0; i < sonElements.length; i++) { xmlStr += this.getXmlStr.apply(sonElements[i]); } xmlStr += "</" + this.getElementName() + ">"; return xmlStr; }, getSonElement : function(index) { var _index = index || 0; var sonCounts = this.sonElements.length; if (_index >= sonCounts) { return null; } else { return this.sonElements[_index]; } } }); xaofeixa.Element = Element; // //////////////////////////////////////////////// var Attribute = function(attributeName, attributeValue) { this.attributeName = attributeName; this.attributeValue = attributeValue; Attribute.prototype.init(this); }; $.extend(Attribute.prototype, xaofeixa.BaseObject.prototype); })(); /** * @author: xaofeixa * @date: 2008-04-03 * @purpse: 日志 * */ (function() { var Log = function() { Log.prototype.init(null, { logLevel : "INFO,ERROR" }); return Log.prototype.initLog(); }; $.extend(Log.prototype, xaofeixa.BaseObject.prototype, { initLog : function() { var logConsole = new xaofeixa.Element("div"); logConsole.addAttribute({ id : "console", 'class':"console" }); var aDate = new Date(); var time = aDate.getFullYear() + '-' + aDate.getMonth() + '-' + aDate.getDate() + ' ' + aDate.getHours() + ':' + aDate.getMinutes(); logConsole.setText("Star Log system time is:" + time); //logConsole.addAttribute("class","console"); $(logConsole.getXmlStr()).appendTo($('body')); this.docWidth = document.body.clientWidth; this.docHeight = document.body.clientHeight; $("#console").css("top",document.body.clientHeight-$("#console").height()); return this; }, info : function(message) { if (this.logLevel.indexOf("INFO") != -1) { var aDate = new Date(); var time = aDate.getFullYear() + '-' + aDate.getMonth() + '-' + aDate.getDate() + ' ' + aDate.getHours() + ':' + aDate.getMinutes(); message = time + "[info]: " + message; $("#console").html($("#console").html() + "<br/>" + message); } }, error : function(message) { if (this.logLevel.indexOf("ERROR") != -1) { var aDate = new Date(); var time = aDate.getFullYear() + '-' + aDate.getMonth() + '-' + aDate.getDate() + ' ' + aDate.getHours() + ':' + aDate.getMinutes(); message = time + "[error]: " + message; $("#console").html($("#console").html() + "<br/>" + message); } } }); xaofeixa.Log = Log; })(); 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |