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 / fil21A2585F0D73A2DCED1C5DE16339BCF3 < prev    next >
Text File  |  2010-07-12  |  9KB  |  308 lines

  1. XB.UI.Behaviour = Base.extend({
  2.     name: "generic",
  3.     nodeName: "box",
  4.     
  5.     constructor: function(comment, element, widgetInstanceId, builder) {
  6.         this.comment = comment;
  7.         this.element = element;
  8.         this.widgetInstanceId = widgetInstanceId;
  9.         this.builder = builder;
  10.         
  11.         this.logger = XB._base.application.core.Log4Moz.repository.getLogger(XB._base.loggersRoot + ".UI.Behaviour");
  12.         
  13.         this.namespaceURI = XB.UI.consts.STR_XUL_NS;
  14.         
  15.         if (this.comment)
  16.             this.comment.behaviour = this;
  17.         
  18.         this.attribute = { __proto__: null };
  19.         
  20.         this.events = [];
  21.         
  22.         this.on(this.comment, "DOMNodeInserted", this.onInserted, false, this);
  23.         this.on(this.comment, "DOMNodeRemoved", this.onRemoved, false, this);
  24.     },
  25.     
  26.     destructor: function() {
  27.         
  28.     },
  29.     
  30.     readAttributes: function() {
  31.         var attributes = this.element.attributes;
  32.         for (var i = attributes.length; i--;) {
  33.             var attribute = attributes[i];
  34.             this.attribute[attribute.name] = attribute.value;
  35.         }
  36.     },
  37.     
  38.     applyAttributes: function() {
  39.         var attribute = this.attribute;
  40.         for (var name in attribute) {
  41.             this.onAttribute({name: name, value: attribute[name]});
  42.         }
  43.     },
  44.     
  45.     setAttribute: function(name, value, inherited) {
  46.         inherited = inherited || false;
  47.         if (inherited) {
  48.             if (this.attribute[name] != undefined)
  49.                 return;
  50.         }
  51.         else
  52.             this.attribute[name] = value;
  53.         
  54.         if (this.inheritAttributes && this.inheritAttributes[name])
  55.             this.inheritAttribute(name, value);
  56.         else
  57.             if (!inherited || (this.attribute[name] == undefined))
  58.                 this.onAttribute({name: name, value: value, inherited: inherited});
  59.     },
  60.     
  61.     getAttribute: function(name) {
  62.         return this.attribute[name];
  63.     },
  64.     
  65.     inheritanceAttributesEx: function(heir, attributes) {
  66.         function intersect(a, b) {
  67.             var c = {},
  68.                 i;
  69.             for (i in a)
  70.                 if (b[i])
  71.                     c[i] = 1;
  72.             return c;
  73.         }
  74.         
  75.         if (!this.inheritAttributes)
  76.             return attributes;
  77.         attributes = intersect(attributes, this.inheritAttributes);
  78.         
  79.         var empty = true;
  80.         for (let name in attributes) {
  81.             empty = false;
  82.             break;
  83.         }
  84.         if (empty)
  85.             return;
  86.         
  87.         for (let name in attributes)
  88.             if (this.attribute[name] != undefined) {
  89.                 heir.setAttribute(name, this.attribute[name], true);
  90.                 delete attributes[name];
  91.             }
  92.         
  93.         if (this.parent)
  94.             attributes = this.parent.inheritanceAttributesEx(heir, attributes);
  95.         
  96.         return attributes;
  97.     },
  98.     
  99.     inheritanceAttributes: function() {
  100.         try {
  101.             if (this.succeedAttributes && this.parent)
  102.                 this.parent.inheritanceAttributesEx(this, this.succeedAttributes);
  103.         } catch(e) {
  104.             this.logger.error(e);
  105.         }
  106.     },
  107.     
  108.     inheritAttribute: function(name, value) {
  109.         var heirs = this.heirs();
  110.         for each (var heir in heirs)
  111.             if (
  112.                 (heir.succeedAttributes && heir.succeedAttributes[name]) ||
  113.                 (heir.inheritAttributes && heir.inheritAttributes[name])
  114.             )
  115.                 heir.setAttribute(name, value, true);
  116.     },
  117.     
  118.     onAttribute: function() {
  119.     },
  120.     
  121.     getWidgetContainer: function() {
  122.         if (this.node && this.node.parentNode && this.node.parentNode.isXBWidget)
  123.             return this.node.parentNode;
  124.         
  125.         if (this.parent)
  126.             return this.parent.getWidgetContainer();
  127.         
  128.         return null;
  129.     },
  130.     
  131.     children: function() {
  132.         var children = [],
  133.             child = this.node.firstChild;
  134.         while(child) {
  135.             if ((child.nodeType == child.COMMENT_NODE) && child.behaviour)
  136.                 children.push(child.behaviour);
  137.             child = child.nextSibling;
  138.         }
  139.         return children;
  140.     },
  141.     
  142.     index: function() {
  143.         return this.parent.children().indexOf(this);
  144.     },
  145.     
  146.     heirs: function() {
  147.         return this.children();
  148.     },
  149.     
  150.     onInserted: function(event) {
  151.         if (event.eventPhase != event.AT_TARGET)
  152.             return;
  153.         if (this.deaf)
  154.             return;
  155.         
  156.         this.readAttributes();
  157.         this.create();
  158.         this.append();
  159.         this.applyAttributes();
  160.         this.inheritanceAttributes();
  161.         this.build();
  162.     },
  163.     
  164.     onRemoved: function(event) {
  165.         try {
  166.             if (event.eventPhase != event.AT_TARGET)
  167.                 return;
  168.             if (this.deaf)
  169.                 return;
  170.             
  171.             this.destroy();
  172.         }
  173.         catch (e) {
  174.             this.logger.error(XB._base.application.core.Lib.misc.formatError(e));
  175.             this.logger.debug(e.stack);
  176.         }
  177.     },
  178.     
  179.     create: function() {
  180.         this.logger.trace("Creating node " + this.nodeName);
  181.         this.document = this.comment.ownerDocument;
  182.         this.parent = this.comment.parentNode.behaviour;
  183.         
  184.         this.node = this.document.createElementNS(this.namespaceURI, this.nodeName);
  185.         this.node.setAttribute("xb-name", this.name);
  186.         this.node.behaviour = this;
  187.     },
  188.     
  189.     append: function() {
  190.         this.comment.parentNode.insertBefore(this.node, this.comment);
  191.     },
  192.     
  193.     buildFake: function() {
  194.         this.fake = this.document.createElementNS(XB.UI.consts.STR_XUL_NS, "box");
  195.         this.builder.build(this.element, this.fake, this.widgetInstanceId);
  196.     },
  197.     
  198.     build: function() {
  199.         this.builder.build(this.element, this.node, this.widgetInstanceId);
  200.     },
  201.     
  202.     destroy: function() {
  203.         this.removeEventListeners();
  204.         
  205.         this.builder.cleanNode(this.fake);
  206.         this.builder.cleanNode(this.node);
  207.         
  208.         this.builder.removeNode(this.comment);
  209.         this.builder.removeNode(this.node);
  210.         
  211.         if (this.comment)
  212.             delete this.comment.behaviour;
  213.         if (this.node)
  214.             delete this.node.behaviour;
  215.         
  216.         delete this.node;
  217.         delete this.comment;
  218.     },
  219.     
  220.     transform: function(to) {
  221.         var oldNode = this.node;
  222.         this.nodeName = to.toString();
  223.         this.create();
  224.         oldNode.parentNode.insertBefore(this.node, oldNode);
  225.         
  226.         while (oldNode.childNodes.length > 0) {
  227.             var node = oldNode.childNodes[0];
  228.             if (node.behaviour)
  229.                 node.behaviour.deaf = true;
  230.             this.node.appendChild(node);
  231.             if (node.behaviour)
  232.                 node.behaviour.deaf = false;
  233.         }
  234.         oldNode.parentNode.removeChild(oldNode);
  235.     },
  236.     
  237.     removeEventListeners: function() {
  238.         for (var i = this.events.length; i--;)
  239.             this.events[i].disable();
  240.         
  241.         this.events = [];
  242.     },
  243.     
  244.     on: function(node, type, listener, capture, context) {
  245.         if (!node)
  246.             return;
  247.         
  248.         this.events.push(new XB.UI.EventListener(node, type, listener, capture, context));
  249.     }
  250. });
  251.  
  252. XB.UI.Behaviour.IWithAction = XB.UI.Behaviour.extend({
  253.     create: function() {
  254.         this.base();
  255.         this.on(this.node, "command", this.onAction, false, this);
  256.         this.on(this.node, "click", this.onAction, false, this);
  257.     },
  258.     
  259.     onAction: function(event) {
  260.         if (event.type == "click") {
  261.             if (!(event.button == 1 && (this._url || this._action)))
  262.                 return;
  263.         }
  264.         
  265.         event.stopPropagation();
  266.         event.preventDefault();
  267.         
  268.         let eventInfo = {
  269.             type: event.type,
  270.             keys: {
  271.                 shift: event.shiftKey,
  272.                 meta: event.metaKey,
  273.                 ctrl: event.ctrlKey
  274.             },
  275.             mouse: {
  276.                 button: event.button
  277.             }
  278.         };
  279.         
  280.         if (this._action)
  281.             this._action.run(eventInfo);
  282.         
  283.         if (this._url)
  284.             this._url.navigate(eventInfo);
  285.     },
  286.     
  287.     addAction: function (action) {
  288.         if ((action == null) && (typeof action.run !== "function"))
  289.             throw new TypeError("Wrong action behaviour interface");
  290.         this._action = action;
  291.     },
  292.     
  293.     removeAction: function (action) {
  294.         if (this._action == action)
  295.             this._action = null;
  296.     },
  297.     
  298.     addURL: function (url) {
  299.         if ((url == null) && (typeof url.navigate !== "function"))
  300.             throw new TypeError("Wrong url behaviour interface");
  301.         this._url = url;
  302.     },
  303.     
  304.     removeURL: function (url) {
  305.         if (this._url == url)
  306.             this._url = null;
  307.     }
  308. });