📄 domwidget.js
字号:
/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml*/dojo.provide("dojo.widget.DomWidget");dojo.require("dojo.event.*");dojo.require("dojo.io.*");dojo.require("dojo.widget.Widget");dojo.require("dojo.dom");dojo.require("dojo.html.style");dojo.require("dojo.xml.Parse");dojo.require("dojo.uri.*");dojo.require("dojo.lang.func");dojo.require("dojo.lang.extras");dojo.widget._cssFiles = {};dojo.widget._cssStrings = {};dojo.widget._templateCache = {};// Object: a mapping of strings that are used in template variable replacementdojo.widget.defaultStrings = { dojoRoot: dojo.hostenv.getBaseScriptUri(), baseScriptUri: dojo.hostenv.getBaseScriptUri()};dojo.widget.fillFromTemplateCache = function( /*DomWidget*/ obj, /*String||dojo.uri.Uri*/ templatePath, /*String, optional*/ templateString, /*Boolean, optional*/ avoidCache){ // summary: // static method to build from a template w/ or w/o a real widget in // place // obj: an instance of dojo.widget.DomWidget to initialize the template for // templatePath: the URL to get the template from // templateString: // a string to use in lieu of fetching the template from a URL // avoidCache: // should the template system not use whatever is in the cache and // always use the passed templatePath or templateString? // dojo.debug("avoidCache:", avoidCache); var tpath = templatePath || obj.templatePath; var tmplts = dojo.widget._templateCache; if(!obj["widgetType"]) { // don't have a real template here do { var dummyName = "__dummyTemplate__" + dojo.widget._templateCache.dummyCount++; } while(tmplts[dummyName]); obj.widgetType = dummyName; } var wt = obj.widgetType; var ts = tmplts[wt]; if(!ts){ tmplts[wt] = { "string": null, "node": null }; if(avoidCache){ ts = {}; }else{ ts = tmplts[wt]; } } if((!obj.templateString)&&(!avoidCache)){ obj.templateString = templateString || ts["string"]; } if((!obj.templateNode)&&(!avoidCache)){ obj.templateNode = ts["node"]; } if((!obj.templateNode)&&(!obj.templateString)&&(tpath)){ // fetch a text fragment and assign it to templateString // NOTE: we rely on blocking IO here! var tstring = dojo.hostenv.getText(tpath); if(tstring){ // strip <?xml ...?> declarations so that external SVG and XML // documents can be added to a document without worry tstring = tstring.replace(/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, ""); var matches = tstring.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im); if(matches){ tstring = matches[1]; } }else{ tstring = ""; } obj.templateString = tstring; if(!avoidCache){ tmplts[wt]["string"] = tstring; } } if((!ts["string"])&&(!avoidCache)){ ts.string = obj.templateString; }}dojo.widget._templateCache.dummyCount = 0;// Array: list of properties to search for node-to-property mappingsdojo.widget.attachProperties = ["dojoAttachPoint", "id"];// String: name of the property to use for mapping DOM events to widget functionsdojo.widget.eventAttachProperty = "dojoAttachEvent";// String: property name of code to evaluate when the widget is constructeddojo.widget.onBuildProperty = "dojoOnBuild";// Array: possible accessibility values to set on widget elements - role or statedojo.widget.waiNames = ["waiRole", "waiState"];// Object: Contains functions to set accessibility roles and states // onto widget elementsdojo.widget.wai = { waiRole: { // String: information for mapping accessibility role name: "waiRole", // String: URI of the namespace for the set of roles "namespace": "http://www.w3.org/TR/xhtml2", // String: alias to assign the namespace alias: "x2", // String: prefix to assign to the role value prefix: "wairole:" }, waiState: { // String: informatin for mapping accessibility state name: "waiState", // String: URI of the namespace for the set of states "namespace": "http://www.w3.org/2005/07/aaa", // String: alias to assign the namespace alias: "aaa", // String: empty string - state value does not require prefix prefix: "" }, setAttr: function(/*DomNode*/node, /*String*/ ns, /*String*/ attr, /*String|Boolean*/value){ // Summary: Use appropriate API to set the role or state attribute onto the element. // Description: In IE use the generic setAttribute() api. Append a namespace // alias to the attribute name and appropriate prefix to the value. // Otherwise, use the setAttribueNS api to set the namespaced attribute. Also // add the appropriate prefix to the attribute value. if(dojo.render.html.ie){ node.setAttribute(this[ns].alias+":"+ attr, this[ns].prefix+value); }else{ node.setAttributeNS(this[ns]["namespace"], attr, this[ns].prefix+value); } }, getAttr: function(/*DomNode*/ node, /*String*/ ns, /*String|Boolena*/ attr){ // Summary: Use the appropriate API to retrieve the role or state value // Description: In IE use the generic getAttribute() api. An alias value // was added to the attribute name to simulate a namespace when the attribute // was set. Otherwise use the getAttributeNS() api to retrieve the state value if(dojo.render.html.ie){ return node.getAttribute(this[ns].alias+":"+attr); }else{ return node.getAttributeNS(this[ns]["namespace"], attr); } }, removeAttr: function(/*DomNode*/ node, /*String*/ ns, /*String|Boolena*/ attr){ // Summary: Use the appropriate API to remove the role or state value // Description: In IE use the generic removeAttribute() api. An alias value // was added to the attribute name to simulate a namespace when the attribute // was set. Otherwise use the removeAttributeNS() api to remove the state value var success = true; //only IE returns a value if(dojo.render.html.ie){ success = node.removeAttribute(this[ns].alias+":"+attr); }else{ node.removeAttributeNS(this[ns]["namespace"], attr); } return success; }};dojo.widget.attachTemplateNodes = function( /*DomNode*/ rootNode, /*Widget*/ targetObj, /*Array*/ events ){ // summary: // map widget properties and functions to the handlers specified in // the dom node and it's descendants. This function iterates over all // nodes and looks for these properties: // * dojoAttachPoint // * dojoAttachEvent // * waiRole // * waiState // * any "dojoOn*" proprties passed in the events array // rootNode: // the node to search for properties. All children will be searched. // events: a list of properties generated from getDojoEventsFromStr. // FIXME: this method is still taking WAAAY too long. We need ways of optimizing: // a.) what we are looking for on each node // b.) the nodes that are subject to interrogation (use xpath instead?) // c.) how expensive event assignment is (less eval(), more connect()) // var start = new Date(); var elementNodeType = dojo.dom.ELEMENT_NODE; function trim(str){ return str.replace(/^\s+|\s+$/g, ""); } if(!rootNode){ rootNode = targetObj.domNode; } if(rootNode.nodeType != elementNodeType){ return; } // alert(events.length); var nodes = rootNode.all || rootNode.getElementsByTagName("*"); var _this = targetObj; for(var x=-1; x<nodes.length; x++){ var baseNode = (x == -1) ? rootNode : nodes[x]; // FIXME: is this going to have capitalization problems? Could use getAttribute(name, 0); to get attributes case-insensitve var attachPoint = []; if(!targetObj.widgetsInTemplate || !baseNode.getAttribute('dojoType')){ for(var y=0; y<this.attachProperties.length; y++){ var tmpAttachPoint = baseNode.getAttribute(this.attachProperties[y]); if(tmpAttachPoint){ attachPoint = tmpAttachPoint.split(";"); for(var z=0; z<attachPoint.length; z++){ if(dojo.lang.isArray(targetObj[attachPoint[z]])){ targetObj[attachPoint[z]].push(baseNode); }else{ targetObj[attachPoint[z]]=baseNode; } } break; } } var attachEvent = baseNode.getAttribute(this.eventAttachProperty); if(attachEvent){ // NOTE: we want to support attributes that have the form // "domEvent: nativeEvent; ..." var evts = attachEvent.split(";"); for(var y=0; y<evts.length; y++){ if((!evts[y])||(!evts[y].length)){ continue; } var thisFunc = null; var tevt = trim(evts[y]); if(evts[y].indexOf(":") >= 0){ // oh, if only JS had tuple assignment var funcNameArr = tevt.split(":"); tevt = trim(funcNameArr[0]); thisFunc = trim(funcNameArr[1]); } if(!thisFunc){ thisFunc = tevt; } var tf = function(){ var ntf = new String(thisFunc); return function(evt){ if(_this[ntf]){ _this[ntf](dojo.event.browser.fixEvent(evt, this)); } }; }(); dojo.event.browser.addListener(baseNode, tevt, tf, false, true); // dojo.event.browser.addListener(baseNode, tevt, dojo.lang.hitch(_this, thisFunc)); } } for(var y=0; y<events.length; y++){ //alert(events[x]); var evtVal = baseNode.getAttribute(events[y]); if((evtVal)&&(evtVal.length)){ var thisFunc = null; var domEvt = events[y].substr(4); // clober the "dojo" prefix thisFunc = trim(evtVal); var funcs = [thisFunc]; if(thisFunc.indexOf(";")>=0){ funcs = dojo.lang.map(thisFunc.split(";"), trim); } for(var z=0; z<funcs.length; z++){ if(!funcs[z].length){ continue; } var tf = function(){ var ntf = new String(funcs[z]); return function(evt){ if(_this[ntf]){ _this[ntf](dojo.event.browser.fixEvent(evt, this)); } } }(); dojo.event.browser.addListener(baseNode, domEvt, tf, false, true); // dojo.event.browser.addListener(baseNode, domEvt, dojo.lang.hitch(_this, funcs[z])); } } } } // continue; // FIXME: we need to put this into some kind of lookup structure // instead of direct assignment var tmpltPoint = baseNode.getAttribute(this.templateProperty); if(tmpltPoint){ targetObj[tmpltPoint]=baseNode; } dojo.lang.forEach(dojo.widget.waiNames, function(name){ var wai = dojo.widget.wai[name]; var val = baseNode.getAttribute(wai.name); if(val){ if(val.indexOf('-') == -1){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -