[Yulup-commits] rev 23827 - in public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content: . editor/widgets

andi at wyona.com andi at wyona.com
Wed Apr 18 00:05:55 CEST 2007


Author: andi
Date: 2007-04-18 00:05:53 +0200 (Wed, 18 Apr 2007)
New Revision: 23827

Modified:
   public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/editor/widgets/widget.js
   public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/neutron.js
   public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/neutronparser10.js
Log:
New widget format (WIP).


Modified: public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/editor/widgets/widget.js
===================================================================
--- public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/editor/widgets/widget.js	2007-04-17 22:05:32 UTC (rev 23826)
+++ public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/editor/widgets/widget.js	2007-04-17 22:05:53 UTC (rev 23827)
@@ -149,57 +149,53 @@
         insertMenu     = document.getElementById("uiYulupEditorEditorContextMenuInsertMenupopup");
 
         for (var i = 0; i < aWidgets.length; i++) {
-            if (this.getWidgetByName(aWidgets[i].attributes["name"])) {
-                // skip duplicate widgets
-                continue;
-            }
+            widget = aWidgets[i];
 
-            widget = new Widget();
+            // TODO: is this.widgets actually used somewhere?
+            this.widgets.push(widget);
 
-            widget.attributes         = aWidgets[i].attributes;
-            widget.fragment           = aWidgets[i].fragment;
-            widget.fragmentAttributes = aWidgets[i].fragmentAttributes;
-            widget.icon               = aWidgets[i].icon;
-            widget.iconURI            = aWidgets[i].iconURI;
+            if (widget instanceof NeutronWidget) {
+                /* DEBUG */ dump("Yulup:widget.js:WidgetManager.addWidget: widget id \"" + widget.id + "\" is of type NeutronWidget\n");
 
-            /* DEBUG */ dump("Yulup:widget.js:WidgetManager.addWidget: widget.icon = \"" + widget.icon + "\"\n");
+                /* DEBUG */ dump("Yulup:widget.js:WidgetManager.addWidget: widget.icon = \"" + widget.icon + "\"\n");
 
-            if (widget.icon) {
-                switch (widget.attributes["type"]) {
-                    case "surround":
-                    case "insert":
-                        // create the widget directory
-                        widgetDir = this.tmpDir.clone();
-                        widgetDir.append(widget.attributes["name"]);
-                        widgetDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
+                if (widget.icon) {
+                    // create the widget directory
+                    widgetDir = this.tmpDir.clone();
+                    widgetDir.append(widget.id);
+                    widgetDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
 
-                        // create the widget icon file
-                        iconFile = widgetDir.clone();
-                        iconFile.append(widget.icon);
-                        iconFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0755);
+                    // create the widget icon file
+                    iconFile = widgetDir.clone();
+                    iconFile.append(widget.icon);
+                    iconFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0755);
 
-                        /* DEBUG */ dump("Yulup:widget.js:WidgetManager.addWidget: tmp icon file = \"" + iconFile.path + "\"\n");
+                    /* DEBUG */ dump("Yulup:widget.js:WidgetManager.addWidget: tmp icon file = \"" + iconFile.path + "\"\n");
 
-                        widget.tmpIconFile = iconFile;
-                        widget.tmpIconURI  = ioService.newFileURI(widget.tmpIconFile);
-                        break;
+                    widget.tmpIconFile = iconFile;
+                    widget.tmpIconURI  = ioService.newFileURI(widget.tmpIconFile);
                 }
+
+                // add command to editor.xul
+                widgetCommand = document.createElement("command");
+                widgetCommand.setAttribute("id", "cmd_yulup_widget_" + widget.id);
+                widgetCommand.setAttribute("disabled", "false");
+                widgetCommand.setAttribute("label", widget.getNameByLang(YulupAppServices.getAppLocale()));
+                widgetCommand.setAttribute("tooltiptext", widget.getDescriptionByLang(YulupAppServices.getAppLocale()));
+                widgetCommand.setAttribute("oncommand", "WidgetHandler.doWidgetCommand(this, \"" + widget.id + "\")");
+                commandSet.appendChild(widgetCommand);
             }
 
-            this.widgets[this.widgets.length] = widget;
+            if (widget instanceof NeutronWidgetGroup) {
+                /* DEBUG */ dump("Yulup:widget.js:WidgetManager.addWidget: widget id \"" + widget.id + "\" is of type NeutronWidgetGroup\n");
+            }
 
-            // add command to editor.xul
-            widgetCommand = document.createElement("command");
-            widgetCommand.setAttribute("id", "cmd_yulup_widget_" + widget.attributes["name"]);
-            widgetCommand.setAttribute("disabled", "false");
-            widgetCommand.setAttribute("label", widget.attributes["name"]);
-            widgetCommand.setAttribute("tooltiptext", widget.attributes["description"]);
-            widgetCommand.setAttribute("oncommand", "WidgetHandler.doWidgetCommand(this, \"" + widget.attributes["name"] + "\")");
-            commandSet.appendChild(widgetCommand);
 
+
+            // TODO: flatten out list of widgets and then re-loop?
             // get top-level element name
-            if (widget.attributes["type"] === "surround") {
-                surroundingElementName = widget.fragment.documentElement;
+            if (widget.supportsActionSurround()) {
+                surroundingElementName = widget.getSurroundActionFragment().documentElement;
 
                 if (surroundingElementName && surroundingElementName.localName) {
                     /* DEBUG */ dump("Yulup:widget.js:WidgetManager.addWidgets: registering surrounding action for element name \"" + widget.fragment.documentElement.localName.toLowerCase() + "\"\n");
@@ -213,34 +209,31 @@
             // add toolbarbutton to editor.xul
             if (widget.icon) {
                 widgetButton = document.createElement("canvasbutton");
-                widgetButton.setAttribute("id", "uiWidget" + widget.attributes["name"]);
+                widgetButton.setAttribute("id", "uiWidget" + widget.id);
                 widgetButton.setAttribute("style", "-moz-box-orient: vertical;");
-                widgetButton.setAttribute("command", "cmd_yulup_widget_" + widget.attributes["name"]);
+                widgetButton.setAttribute("command", "cmd_yulup_widget_" + widget.id);
                 toolbarButtons.appendChild(widgetButton);
             }
 
             // add command to context menu
             menuItem = document.createElement("menuitem");
-            menuItem.setAttribute("command", "cmd_yulup_widget_" + widget.attributes["name"]);
+            menuItem.setAttribute("command", "cmd_yulup_widget_" + widget.id);
 
-            switch (widget.attributes["type"]) {
-                case "surround":
-                    menuItem.setAttribute("autocheck", "false");
-                    menuItem.setAttribute("type", "checkbox");
+            if (widget.supportsActionSurround()) {
+                menuItem.setAttribute("autocheck", "false");
+                menuItem.setAttribute("type", "checkbox");
 
-                    surroundMenu.appendChild(menuItem);
+                surroundMenu.appendChild(menuItem);
 
-                    if (surroundMenu.parentNode.hasAttribute("disabled"))
-                        surroundMenu.parentNode.removeAttribute("disabled");
+                if (surroundMenu.parentNode.hasAttribute("disabled"))
+                    surroundMenu.parentNode.removeAttribute("disabled");
+            }
 
-                    break;
-                case "insert":
-                    insertMenu.appendChild(menuItem);
+            if (widget.supportsActionInsert()) {
+                insertMenu.appendChild(menuItem);
 
-                    if (insertMenu.parentNode.hasAttribute("disabled"))
-                        insertMenu.parentNode.removeAttribute("disabled");
-
-                    break;
+                if (insertMenu.parentNode.hasAttribute("disabled"))
+                    insertMenu.parentNode.removeAttribute("disabled");
             }
         }
     },
@@ -258,17 +251,12 @@
 
         for (var i = 0; i < this.widgets.length; i++) {
             if (this.widgets[i].icon) {
-                switch (this.widgets[i].attributes["type"]) {
-                    case "surround":
-                    case "insert":
-                        contextObj = {
-                            widget: this.widgets[i],
-                            callback: aLoadFinishedCallback
-                        };
+                contextObj = {
+                    widget: this.widgets[i],
+                    callback: aLoadFinishedCallback
+                };
 
-                        NetworkService.httpFetchToFile(this.widgets[i].iconURI.spec, this.widgets[i].tmpIconFile, this.requestFinishedHandler, contextObj, true);
-                        break;
-                }
+                NetworkService.httpFetchToFile(this.widgets[i].iconURI.spec, this.widgets[i].tmpIconFile, this.requestFinishedHandler, contextObj, true);
             } else {
                 aLoadFinishedCallback(null, null, null);
             }
@@ -287,10 +275,10 @@
         /* DEBUG */ dump("Yulup:widget.js:WidgetManager.WidgetManager.installWidget() invoked\n");
 
         // set widget image
-        widgetButton = document.getElementById('uiWidget' + aWidget.attributes["name"]);
+        widgetButton = document.getElementById("uiWidget" + aWidget.id);
 
         if (widgetButton)
-            widgetButton.setAttribute('image', aWidget.tmpIconURI.spec);
+            widgetButton.setAttribute("image", aWidget.tmpIconURI.spec);
     },
 
     requestFinishedHandler: function(aResultFile, aResponseStatusCode, aContext) {
@@ -304,6 +292,7 @@
 
             xmlDoc = new XMLDocument(fileURI);
             xmlDoc.loadDocument();
+
             try {
                 // parse the neutron exception
                 Neutron.response(xmlDoc.documentData);

Modified: public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/neutron.js
===================================================================
--- public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/neutron.js	2007-04-17 22:05:32 UTC (rev 23826)
+++ public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/neutron.js	2007-04-17 22:05:53 UTC (rev 23827)
@@ -613,6 +613,9 @@
 
         // for all fragments, convert to string
         for (var i = 0; i < this.fragments.length; i++) {
+            /* TODO: get rid of this horrible stringification once all the data is moved
+             *       into their own respective datatypes, so they can toString()
+             *       themselves. */
             objString += "Fragment name:        " + this.fragments[i].name + "\n";
             objString += "Edit MIME-Type:       " + this.fragments[i].mimeType + "\n";
             objString += "Edit Open URI:        " + (this.fragments[i].open.uri ? this.fragments[i].open.uri.spec : this.fragments[i].open.uri) + "\n"; + "\n";
@@ -809,26 +812,76 @@
  * @constructor
  * @return {NeutronParser}
  */
-function NeutronParser() {}
+function NeutronParser() {
+    /* DEBUG */ dump("Yulup:neutron.js:NeutronParser() invoked\n");
+}
 
 NeutronParser.prototype = {};
 
 
-function NeutronWidget() {}
+/**
+ * NeutronWidget constructor. Instantiates a new object of
+ * type NeutronWidget.
+ *
+ * Base class for versioned Neutron widgets.
+ *
+ * @constructor
+ * @return {NeutronWidget}
+ */
+function NeutronWidget() {
+    /* DEBUG */ dump("Yulup:neutron.js:NeutronWidget() invoked\n");
 
-NeutronWidget.prototype = {};
+    this.id = Date.now().toString();
+}
 
+NeutronWidget.prototype = {
+    id: null
+};
 
-function NeutronWidgetGroup() {}
 
+/**
+ * NeutronWidgetGroup constructor. Instantiates a new object of
+ * type NeutronWidgetGroup.
+ *
+ * Base class for versioned Neutron widget groups.
+ *
+ * @constructor
+ * @return {NeutronWidgetGroup}
+ */
+function NeutronWidgetGroup() {
+    /* DEBUG */ dump("Yulup:neutron.js:NeutronWidgetGroup() invoked\n");
+}
+
 NeutronWidgetGroup.prototype = {};
 
 
-function NeutronWidgetAction() {}
+/**
+ * NeutronWidgetAction constructor. Instantiates a new object of
+ * type NeutronWidgetAction.
+ *
+ * Base class for versioned Neutron widget actions.
+ *
+ * @constructor
+ * @return {NeutronWidgetAction}
+ */
+function NeutronWidgetAction() {
+    /* DEBUG */ dump("Yulup:neutron.js:NeutronWidgetAction() invoked\n");
+}
 
 NeutronWidgetAction.prototype = {};
 
 
-function NeutronWidgetActionParameter() {}
+/**
+ * NeutronWidgetActionParameter constructor. Instantiates a new object of
+ * type NeutronWidgetActionParameter.
+ *
+ * Base class for versioned Neutron widget action parameters.
+ *
+ * @constructor
+ * @return {NeutronWidgetActionParameter}
+ */
+function NeutronWidgetActionParameter() {
+    /* DEBUG */ dump("Yulup:neutron.js:NeutronWidgetActionParameter() invoked\n");
+}
 
 NeutronWidgetActionParameter.prototype = {};

Modified: public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/neutronparser10.js
===================================================================
--- public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/neutronparser10.js	2007-04-17 22:05:32 UTC (rev 23826)
+++ public/yulup/src/branches/WIDGET_REFACTORING_BRANCH/yulup/src/chrome/content/neutronparser10.js	2007-04-17 22:05:53 UTC (rev 23827)
@@ -41,6 +41,9 @@
 function NeutronParser10(aDocument, aBaseURI) {
     /* DEBUG */ dump("Yulup:neutronparser10.js:NeutronParser10(\"" + aDocument + "\", \"" + aBaseURI + "\") invoked\n");
 
+     // call super constructor
+    NeutronParser.call(this);
+
     this.documentDOM = aDocument;
     this.baseURI     = aBaseURI;
     this.ioService   = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
@@ -560,7 +563,10 @@
 };
 
 
-function Neutron10Widget() {}
+function Neutron10Widget() {
+    // call super constructor
+    NeutronWidget.call(this);
+}
 
 Neutron10Widget.prototype = {
     __proto__: NeutronWidget.prototype,
@@ -573,6 +579,36 @@
     surround: null,
     insert  : null,
 
+    __getByLang: function (aLangMap, aLangID) {
+        var retval      = null;
+        var prefixIndex = null;
+        var langID      = null;
+
+        /* DEBUG */ dump("Yulup:neutronparser10.js:Neutron10Widget.__getByLang(\"" + aLangMap + "\", \"" + aLangID + "\") invoked\n");
+
+        /* DEBUG */ YulupDebug.ASSERT(aLangMap != null);
+        /* DEBUG */ YulupDebug.ASSERT(aLangID  != null);
+
+        retval = aLangMap[aLangID];
+
+        if (retval)
+            return retval;
+
+        // try to cut down the lang ID to its prefix
+        prefixIndex = aLangID.indexOf("-");
+        if (prefixIndex > 0)
+            langID = aLangID.substring(0, prefixIndex);
+
+        retval = aLangMap[langID];
+
+        if (retval)
+            return retval;
+
+        // return first value in map
+        for (retval in aLangMap)
+            return retval;
+    },
+
     set name(aValue) {
         var name = {};
 
@@ -586,7 +622,7 @@
     },
 
     getNameByLang: function (aLangID) {
-        return this.__name[aLangID];
+        return this.__getByLang(this.__name, aLangID);
     },
 
     set description(aValue) {
@@ -602,12 +638,37 @@
     },
 
     getDescriptionByLang: function (aLangID) {
-        return this.__desc[aLangID];
+        return this.__getByLang(this.__desc, aLangID);
+    },
+
+    supportsActionSurround: function () {
+        return (this.surround != null);
+    },
+
+    supportsActionInsert: function () {
+        return (this.insert != null);
+    },
+
+    getSurroundActionFragment: function () {
+        if (!this.surround)
+            return null;
+
+        this.surround.fragment;
+    },
+
+    getInsertActionFragment: function () {
+        if (!this.insert)
+            return null;
+
+        this.insert.fragment;
     }
 };
 
 
 function Neutron10WidgetGroup() {
+    // call super constructor
+    NeutronWidgetGroup.call(this);
+
     this.widgets = new Array();
 }
 
@@ -618,7 +679,10 @@
 };
 
 
-function Neutron10WidgetAction() {}
+function Neutron10WidgetAction() {
+    // call super constructor
+    NeutronWidgetAction.call(this);
+}
 
 Neutron10WidgetAction.prototype = {
     __proto__: NeutronWidgetAction.prototype,
@@ -628,7 +692,10 @@
 };
 
 
-function Neutron10WidgetActionParameter() {}
+function Neutron10WidgetActionParameter() {
+    // call super constructor
+    NeutronWidgetActionParameter.call(this);
+}
 
 Neutron10WidgetActionParameter.prototype = {
     __proto__: NeutronWidgetActionParameter.prototype,
@@ -639,6 +706,36 @@
     xpath: null,
     type : null,
 
+    __getByLang: function (aLangMap, aLangID) {
+        var retval      = null;
+        var prefixIndex = null;
+        var langID      = null;
+
+        /* DEBUG */ dump("Yulup:neutronparser10.js:Neutron10WidgetActionParameter.__getByLang(\"" + aLangMap + "\", \"" + aLangID + "\") invoked\n");
+
+        /* DEBUG */ YulupDebug.ASSERT(aLangMap != null);
+        /* DEBUG */ YulupDebug.ASSERT(aLangID  != null);
+
+        retval = aLangMap[aLangID];
+
+        if (retval)
+            return retval;
+
+        // try to cut down the lang ID to its prefix
+        prefixIndex = aLangID.indexOf("-");
+        if (prefixIndex > 0)
+            langID = aLangID.substring(0, prefixIndex);
+
+        retval = aLangMap[langID];
+
+        if (retval)
+            return retval;
+
+        // return first value in map
+        for (retval in aLangMap)
+            return retval;
+    },
+
     set name(aValue) {
         var name = {};
 
@@ -652,7 +749,7 @@
     },
 
     getNameByLang: function (aLangID) {
-        return this.__name[aLangID];
+        return this.__getByLang(this.__name, aLangID);
     },
 
     set description(aValue) {
@@ -668,6 +765,6 @@
     },
 
     getDescriptionByLang: function (aLangID) {
-        return this.__desc[aLangID];
+        return this.__getByLang(this.__desc, aLangID);
     }
 };




More information about the Yulup-commits mailing list