From dcfe845dacf96b1ea694d7f59ad92d9ad998016f Mon Sep 17 00:00:00 2001 From: "gavin%gavinsharp.com" Date: Thu, 10 Aug 2006 00:30:12 +0000 Subject: [PATCH] Bug 205872: domNode viewer should use one dialog for setting new/editing attributes, patch by Shawn Wilsher , r=timeless, sr=neil --- .../content/viewers/domNode/domNode.js | 112 +++++++++------ .../content/viewers/domNode/domNodeDialog.js | 135 ++++++++++++++++++ .../content/viewers/domNode/domNodeDialog.xul | 127 ++++++++++++++++ .../locale/en-US/inspector.properties | 2 - .../locale/en-US/viewers/domNode.dtd | 21 +++ 5 files changed, 352 insertions(+), 45 deletions(-) create mode 100644 extensions/inspector/resources/content/viewers/domNode/domNodeDialog.js create mode 100644 extensions/inspector/resources/content/viewers/domNode/domNodeDialog.xul diff --git a/extensions/inspector/resources/content/viewers/domNode/domNode.js b/extensions/inspector/resources/content/viewers/domNode/domNode.js index bc65c00150d..28a50e5eee3 100644 --- a/extensions/inspector/resources/content/viewers/domNode/domNode.js +++ b/extensions/inspector/resources/content/viewers/domNode/domNode.js @@ -51,7 +51,6 @@ var gPromptService; //////////// global constants //////////////////// const kDOMViewCID = "@mozilla.org/inspector/dom-view;1"; -const kPromptServiceCID = "@mozilla.org/embedcomp/prompt-service;1"; ////////////////////////////////////////////////// @@ -215,7 +214,8 @@ DOMNodeViewer.prototype = case "cmdEditDelete": return this.selectedAttribute != null; case "cmdEditEdit": - return this.mAttrTree.view.selection.count == 1; + return this.mAttrTree.currentIndex >= 0 && + this.mAttrTree.view.selection.count == 1; case "cmdEditNodeValue": // this function can be fired before the subject is set if (this.subject) { @@ -376,43 +376,38 @@ cmdEditInsert.prototype = promptFor: function() { - if (!gPromptService) { - gPromptService = XPCU.getService(kPromptServiceCID, "nsIPromptService"); - } - - var attrName = { value: "" }; - var attrValue = { value: "" }; - var dummy = { value: false }; - var bundle = viewer.pane.panelset.stringBundle; - var msg = bundle.getString("enterAttrName.message"); var title = bundle.getString("newAttribute.title"); + var doc = viewer.subject.ownerDocument; + var out = { name: null, value: null, namespaceURI: null, accepted: false }; - if (gPromptService.prompt(window, title, msg, attrName, null, dummy)) { - msg = bundle.getString("enterAttrValue.message"); - if (gPromptService.prompt(window, title, msg, attrValue, null, dummy)) { - this.subject = viewer.subject; - this.subject.setAttribute(attrName.value, attrValue.value); - this.attr = this.subject.getAttributeNode(attrName.value); - return false; - } - } - return true; + window.openDialog("chrome://inspector/content/viewers/domNode/domNodeDialog.xul", + "insert", "chrome,modal,centerscreen", out, title, doc); + + this.subject = viewer.subject; + if (out.accepted) + this.subject.setAttributeNS(out.namespaceURI, out.name, out.value); + + this.attr = this.subject.getAttributeNode(out.name); + return false; }, doCommand: function() { - if (this.attr) - this.subject.setAttribute(this.attr.nodeName, this.attr.nodeValue); - else + if (!this.attr) return this.promptFor(); + + this.subject.setAttributeNS(this.attr.namespaceURI, + this.attr.nodeName, + this.attr.nodeValue); return false; }, undoCommand: function() { if (this.attr && this.subject == viewer.subject) - this.subject.removeAttribute(this.attr.nodeName, this.attr.nodeValue); + this.subject.removeAttributeNS(this.attr.namepsaceURI, + this.attr.localName); } }; @@ -455,28 +450,44 @@ cmdEditEdit.prototype = attr: null, previousValue: null, newValue: null, + previousNamespaceURI: null, + newNamespaceURI: null, subject: null, promptFor: function() { var attr = viewer.selectedAttribute.node; if (attr) { - if (!gPromptService) { - gPromptService = XPCU.getService(kPromptServiceCID, "nsIPromptService"); - } - - var attrValue = { value: attr.nodeValue }; - var dummy = { value: false }; - var bundle = viewer.pane.panelset.stringBundle; - var msg = bundle.getString("enterAttrValue.message"); var title = bundle.getString("editAttribute.title"); + var doc = attr.ownerDocument; + var out = { + name: attr.nodeName, + value: attr.nodeValue, + namespaceURI: attr.namespaceURI, + accepted: false + }; - if (gPromptService.prompt(window, title, msg, attrValue, null, dummy)) { - this.subject = viewer.subject; - this.newValue = attrValue.value; - this.previousValue = attr.nodeValue; - this.subject.setAttribute(attr.nodeName, attrValue.value); + window.openDialog("chrome://inspector/content/viewers/domNode/domNodeDialog.xul", + "edit", "chrome,modal,centerscreen", out, title, doc); + + if (out.accepted) { + this.subject = viewer.subject; + this.newValue = out.value; + this.newNamespaceURI = out.namespaceURI; + this.previousValue = attr.nodeValue; + this.previousNamespaceURI = attr.namespaceURI; + if (this.previousNamespaceURI == this.newNamespaceURI) { + this.subject.setAttributeNS(this.previousNamespaceURI, + attr.nodeName, + out.value); + } else { + this.subject.removeAttributeNS(this.previousNamespaceURI, + attr.localName); + this.subject.setAttributeNS(out.namespaceURI, + attr.nodeName, + out.value); + } this.attr = this.subject.getAttributeNode(attr.nodeName); return false; } @@ -486,17 +497,32 @@ cmdEditEdit.prototype = doCommand: function() { - if (this.attr) - this.subject.setAttribute(this.attr.nodeName, this.newValue); - else + if (!this.attr) return this.promptFor(); + + this.subject.removeAttributeNS(this.previousNamespaceURI, + this.attr.localName); + this.subject.setAttributeNS(this.newNamespaceURI, + this.attr.nodeName, + this.newValue); return false; }, undoCommand: function() { - if (this.attr) - this.subject.setAttribute(this.attr.nodeName, this.previousValue); + if (this.attr) { + if (this.previousNamespaceURI == this.newNamespaceURI) { + this.subject.setAttributeNS(this.previousNamespaceURI, + this.attr.nodeName, + this.previousValue); + } else { + this.subject.removeAttributeNS(this.newNamespaceURI, + this.attr.localName); + this.subject.setAttributeNS(this.previousNamespaceURI, + this.attr.nodeName, + this.previousValue); + } + } } }; diff --git a/extensions/inspector/resources/content/viewers/domNode/domNodeDialog.js b/extensions/inspector/resources/content/viewers/domNode/domNodeDialog.js new file mode 100644 index 00000000000..d8baed3fcf0 --- /dev/null +++ b/extensions/inspector/resources/content/viewers/domNode/domNodeDialog.js @@ -0,0 +1,135 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code for the DOM Inspector. + * + * The Initial Developer of the Original Code is + * Shawn Wilsher + * + * Portions created by the Initial Developer are Copyright (C) 2006 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +/////////////////////////////////////////////////////////////////////////////// +//// Global Variables + +var dialog; + +/////////////////////////////////////////////////////////////////////////////// +//// Initialization/Destruction + +window.addEventListener("load", DomNodeDialog_initialize, false); + +function DomNodeDialog_initialize() +{ + dialog = new DomNodeDialog(); + dialog.initialize(); +} + +/////////////////////////////////////////////////////////////////////////////// +//// class DomNodeDialog + +function DomNodeDialog() +{ + this.mData = window.arguments[0]; + this.mTitle = window.arguments[1]; + this.mDoc = window.arguments[2]; + + this.nodeName = document.getElementById("tx_nodeName"); + this.nodeValue = document.getElementById("tx_nodeValue"); + this.namespace = document.getElementById("tx_namespace"); + this.menulist = document.getElementById("ml_namespace"); +} + +DomNodeDialog.prototype = +{ + /** + * This function initializes the content of the dialog. + */ + initialize: function initialize() + { + document.title = this.mTitle; + var menuitems = this.menulist.firstChild.childNodes; + var defaultNS = document.getElementById("mi_namespace"); + var customNS = document.getElementById("mi_custom"); + var accept = document.documentElement.getButton("accept"); + + accept.disabled = this.mData.name == null; + this.nodeName.value = this.mData.name || ""; + this.nodeName.disabled = this.mData.name != null; + this.nodeValue.value = this.mData.value || ""; + this.menulist.disabled = !this.enableNamespaces(); + defaultNS.value = this.mDoc.documentElement.namespaceURI; + customNS.value = this.mData.namespaceURI; + this.menulist.value = this.mData.namespaceURI; + + this.toggleNamespace(); + }, + + /** + * The function that is called on accept. Sets data. + */ + accept: function accept() + { + this.mData.name = this.nodeName.value; + this.mData.value = this.nodeValue.value; + this.mData.namespaceURI = this.namespace.value; + this.mData.accepted = true; + return true; + }, + + /** + * toggleNamespace toggles the namespace textbox based on the namespace menu. + */ + toggleNamespace: function toggleNamespace() + { + dialog.namespace.disabled = dialog.menulist.selectedItem.id != "mi_custom"; + dialog.namespace.value = dialog.menulist.value; + }, + + /** + * enableNamespaces determines if the document accepts namespaces or not + * + * @return True if the document can have namespaced attributes, false + * otherwise. + */ + enableNamespaces: function enableNamespaces() + { + return this.mDoc.contentType != "text/html"; + }, + + /** + * toggleAccept enables/disables the Accept button when there is/isn't an + * attribute name. + */ + toggleAccept: function toggleAccept() + { + document.documentElement.getButton("accept").disabled = + dialog.nodeName.value == ""; + } +}; diff --git a/extensions/inspector/resources/content/viewers/domNode/domNodeDialog.xul b/extensions/inspector/resources/content/viewers/domNode/domNodeDialog.xul new file mode 100644 index 00000000000..1e31b9c714a --- /dev/null +++ b/extensions/inspector/resources/content/viewers/domNode/domNodeDialog.xul @@ -0,0 +1,127 @@ + + + + + + +