home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / AIMP2 / aimp_2.61.583.exe / $TEMP / YandexPackSetup.msi / filBBF0DE058CECD8F3BA0983AE82A4C2A5 < prev    next >
Text File  |  2010-07-12  |  13KB  |  327 lines

  1. XB.UI = {
  2.     consts: {
  3.         STR_XUL_NS:     "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  4.         STR_HTML_NS:    "http://www.w3.org/1999/xhtml"
  5.     }
  6. };
  7.  
  8. XB.UIElementsCollection = function() {
  9.     this._widgets = {__proto__: null};
  10. }
  11.  
  12. XB.UIElementsCollection.prototype = {
  13.     put: function(widgetInstanceId, elementId, comment, nodes) {
  14.         var widget = this._widgets[widgetInstanceId] || (this._widgets[widgetInstanceId] = {__proto__: null});
  15.         widget[elementId] = {comment: comment, nodes: nodes};
  16.     },
  17.     
  18.     get: function(widgetInstanceId, elementId) {
  19.         return this._widgets[widgetInstanceId][elementId];
  20.     },
  21.     
  22.     remove: function(widgetInstanceId, elementId) {
  23.         delete this._widgets[widgetInstanceId][elementId];
  24.     },
  25.     
  26.     finalize: function() {
  27.         for (var instance in this._widgets) {
  28.             var widget = this._widgets[instance];
  29.             for (var element in widget)
  30.                 delete widget[element];
  31.             delete this._widgets[instance];
  32.         }
  33.     },
  34.     
  35.     _widgets: null
  36. }
  37.  
  38. XB.UI.Builder = function(windowEngine, overlayController) {
  39.     this._xid = XB._base.genXULBuilderID();
  40.     this._logger = XB._base.application.core.Log4Moz.repository.getLogger(XB._base.loggersRoot + ".UI" + this._xid);
  41.     this._windowEngine = windowEngine;
  42.     this._collection = new XB.UIElementsCollection();
  43.     this._overlayController = overlayController;
  44.     this._fullWidgetItemIDPattern = new RegExp(XB._base.application.name + "\\.xb\\-(.+)\\-inst\\-(.+)");
  45.     this.deaf = false;
  46.     
  47.     this._logger.debug("Constructing");
  48. }
  49.  
  50. XB.UI.Builder.prototype = {
  51.     registerExtActionsHelper: function UIBuilder_registerExtActionsHelper(handler) {
  52.         if ( handler && (typeof handler.handleXBAction == "function") )
  53.             this._extActionHandler = handler;
  54.         else
  55.             throw new TypeError("Provided helper does not support needed interface");
  56.     },
  57.     
  58.     unregisterExtActionsHelper: function UIBuilder_registerExtActionsHelper(handler) {
  59.         if (handler && this._extActionHandler === handler)
  60.             this._extActionHandler = null;
  61.     },
  62.     
  63.     build: function UIBuilder_build(element, container, widgetInstanceId) {
  64.         try {
  65.             var elements = element.childNodes,
  66.                 document = container.ownerDocument;
  67.             
  68.             var child = null;
  69.             while (child = container.firstChild)
  70.                 container.removeChild(child);
  71.             
  72.             var nodes = this._constructElements(elements, document, widgetInstanceId);
  73.             for (let i = 0; i < nodes.length; i++) {
  74.                 let node = nodes[i];
  75.                 if (node instanceof Ci.nsIDOMAttr)
  76.                     container.behaviour.setAttribute(node.localName, node.nodeValue);
  77.                 else
  78.                     container.appendChild(node);
  79.             }
  80.         }
  81.         catch (e) {
  82.             this._logger.error("Couldn't build XB element " + element.nodeName + ". " + misc.formatError(e));
  83.             this._logger.debug(e.stack);
  84.         }
  85.     },
  86.     
  87.     finalize: function UIBuilder_finalize() {
  88.         this._collection.finalize();
  89.         
  90.     },
  91.     
  92.     handleDataChange: function UIBuilder_handleDataChange(widgetInstanceId, elementId, value) {
  93.         this._logger.trace("handleDataChange: " + [widgetInstanceId, elementId, value]);
  94.         
  95.         var item = this._collection.get(widgetInstanceId, elementId),
  96.             nodes = item.nodes,
  97.             comment = item.comment;
  98.         
  99.         for (var i = nodes.length; i--;) {
  100.             let node = nodes[i];
  101.             if (node) {
  102.                 /*if (node instanceof Ci.nsIDOMAttr) {
  103.                     let owner = node.ownerElement;
  104.                     if (owner) {
  105.                         owner.behaviour.setAttribute(node.localName, null);
  106.                     }
  107.                 }
  108.                 else */{
  109.                     let parent = node.parentNode;
  110.                     if (parent) {
  111.                         parent.removeChild(node);
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.         
  117.         if (!comment.parentNode) {
  118.             this.unsubscribe(widgetInstanceId, elementId);
  119.             return;
  120.         }
  121.         
  122.         
  123.         nodes = this._constructComputedElement(widgetInstanceId, elementId, value, comment.ownerDocument);
  124.         
  125.         this._collection.put(widgetInstanceId, elementId, comment, nodes);
  126.         
  127.         for each (let node in nodes) {
  128.             if (node instanceof Ci.nsIDOMAttr)
  129.                 comment.parentNode.behaviour.setAttribute(node.localName, node.nodeValue);
  130.             else
  131.                 comment.parentNode.insertBefore(node, comment);
  132.         }
  133.     },
  134.     
  135.     unsubscribe: function UIBuilder_unsubscribe(widgetInstanceId, elementId) {
  136.         this._logger.trace("Builder::unsubscribe: " + [widgetInstanceId, elementId]);
  137.         
  138.         this._windowEngine.valueNotNeeded(widgetInstanceId, elementId);
  139.         this._collection.remove(widgetInstanceId, elementId);
  140.     },
  141.     
  142.     dispatchCommands: function UIBuilder_dispatchCommands(WIID, commandsArray, eventInfo) {
  143.         for each (let command in commandsArray) {
  144.             try {
  145.                 this._windowEngine.perform(WIID, command, eventInfo);
  146.             }
  147.             catch (e) {
  148.                 try {
  149.                     if (e instanceof XB.WindowEngine.ENoActionHandler) {
  150.                         if (!this._extActionHandler)
  151.                             throw new XB.WindowEngine.ENoActionHandler("Extened actions handler is not registered");
  152.                         this._extActionHandler.handleXBAction(WIID, command);
  153.                     }
  154.                     else
  155.                         throw e;
  156.                 }
  157.                 catch (e) {
  158.                     this._logger.error("Error running action " + [WIID, command].join(":") + ". " +
  159.                                        misc.formatError(e) );
  160.                     this._logger.debug(e.stack);
  161.                     break;
  162.                 }
  163.             }
  164.         }
  165.     },
  166.     
  167.     cleanNode: function UIBuilder_cleanNode(node) {
  168.         if (!node)
  169.             return;
  170.         var node = node.firstChild;
  171.         while (node) {
  172.             if ((node.nodeType == node.ELEMENT_NODE) && !node.behaviour)
  173.                 this.cleanNode(node);
  174.             var next = node.nextSibling;
  175.             this.removeNode(node);
  176.             node = next;
  177.         }
  178.     },
  179.     
  180.     removeNode: function UIBuilder_removeNode(node) {
  181.         if (node && node.parentNode)
  182.             node.parentNode.removeChild(node);
  183.     },
  184.     
  185.     resolveURI: function UIBuilder_resolveURI(spec, widgetInstanceId) {
  186.         if (!spec)
  187.             return "";
  188.         
  189.         if (!(/^http(s)?:\/\//i.test(spec) || spec.indexOf("data:image/") == 0))
  190.             spec = this._windowEngine.getWidget(widgetInstanceId).prototype.unit.unitPackage.resolvePath(spec);
  191.         
  192.         var uri = sysutils.createFixupURIFromString(spec);
  193.         return uri ? uri.spec : "";
  194.     },
  195.     
  196.     get controller() {
  197.         return this._overlayController;
  198.     },
  199.     
  200.     handleEvent: function UIBuilder_handleEvent(event) {
  201.         if (!(event instanceof Ci.nsIDOMEvent && event.isTrusted === true))
  202.           return;
  203.         switch (event.type) {
  204.             case "DOMNodeInserted":
  205.                 this.onInserted(event);
  206.             break;
  207.             case "DOMNodeRemoved":
  208.                 this.onRemoved(event);
  209.             break;
  210.         }
  211.     },
  212.     
  213.     _xid: undefined,
  214.     _logger: null,
  215.     _windowEngine: null,
  216.     _collection: null,
  217.     _overlayController: null,
  218.     _fullWidgetItemIDPattern: null,
  219.     _extActionHandler: null,
  220.     deaf: undefined,
  221.     
  222.     
  223.     _constructElements: function UIBuilder_constructElements(elements, document, widgetInstanceId) {
  224.         var length = elements.length,
  225.             nodes = [];
  226.         
  227.         for (let i = 0; i < length; i++) {
  228.             let element = elements[i];
  229.             
  230.             switch (element.nodeType) {
  231.                 case element.TEXT_NODE:
  232.                     nodes = nodes.concat(this._constructTextElement(element.nodeValue, document));
  233.                 break;
  234.                 
  235.                 case element.ELEMENT_NODE:
  236.                     if (element.namespaceURI == XB._base.consts.STR_UI_NS) {
  237.                         switch (element.localName) {
  238.                             case XB._base.consts.STR_VAL_REF_ELEM_NAME: {
  239.                                 let elementId = element.getUserData(XB._base.consts.STR_VAL_REF_ID_KEY_NAME);
  240.                                 if (!elementId) {
  241.                                     this._logger.warn("No value reference ID found @" + element.nodeName);
  242.                                     this._logger.debug(XB._base.runtime.serializeXML(element));
  243.                                     continue;
  244.                                 }
  245.                                 let value = this._windowEngine.getValue(widgetInstanceId, elementId);
  246.                                 let computed = this._constructComputedElement(widgetInstanceId, elementId, value, document);
  247.                                 
  248.                                 nodes = nodes.concat(computed);
  249.                                 let comment = document.createComment("xb.computed");
  250.                                 new XB.UI.Behaviour.Computed(comment, element, widgetInstanceId, this);
  251.                                 
  252.                                 nodes.push(comment);
  253.                                 this._collection.put(widgetInstanceId, elementId, comment, computed);
  254.                                 
  255.                                 this._logger.trace("Builder::subscribe: " + [widgetInstanceId, elementId, value]);
  256.                                 
  257.                                 break;
  258.                             }
  259.                             default:
  260.                                 nodes = nodes.concat(this._constructStaticElement(element, widgetInstanceId, document));
  261.                         }
  262.                     }
  263.                 break;
  264.             }
  265.         }
  266.         
  267.         return nodes;
  268.     },
  269.     
  270.     _importAttrs: function UIBuilder_importAttrs(attributesMap, document) {
  271.         var result = [];
  272.         for (let i = 0, len = attributesMap.length; i < len; i++) {
  273.             result.push( document.importNode(attributesMap.item(i), false) );
  274.         }
  275.         return result;
  276.     },
  277.     
  278.     _constructTextElement: function UIBuilder_constructTextElement(text, document) {
  279.         if (typeof text != 'string')
  280.             text = text.toString();
  281.         text = sysutils.normalizeString(text);
  282.         if (text.length > 0) {
  283.             var node = document.createTextNode(text);
  284.             return [node];
  285.         } else {
  286.             return [];
  287.         }
  288.     },
  289.     
  290.     _constructStaticElement: function UIBuilder_constructStaticElement(element, widgetInstanceId, document) {
  291.         var role = element.localName;
  292.         var behaviour = XB.UI.Elements[role];
  293.         if (behaviour) {
  294.             var comment = document.createComment("xb.static role: " + role);
  295.             new behaviour(comment, element, widgetInstanceId, this);
  296.             return [comment];
  297.         }
  298.         else
  299.             this._logger.warn("Unknown UI element " + role);
  300.         
  301.         return [];
  302.     },
  303.     
  304.     _constructComputedElement: function UIBuilder_constructComputedElement(widgetInstanceId, elementId, value, document) {
  305.         var nodes = [];
  306.         if (XB._base.runtime.isXML(value)) {
  307.             nodes = this._constructElements(value.childNodes, document, widgetInstanceId);
  308.             nodes = nodes.concat(this._importAttrs(value.attributes, document));
  309.         } else {
  310.             let text = "";
  311.             if (XB._base.runtime.isException(value)) {
  312.                 this._logger.debug("Got exception value: " + value.toString());
  313.             }
  314.             else {
  315.                 try {
  316.                     text = XB._base.runtime.xToString(value);
  317.                 }
  318.                 catch (e) {
  319.                     this._logger.error("Failed converting XValue into a string. " + misc.formatError(e));
  320.                 }
  321.                 nodes = nodes.concat(this._constructTextElement(text, document));
  322.             }
  323.         }
  324.         
  325.         return nodes;
  326.     }
  327. };