From f5cbbaec62ac712f8eb4046c2f98c49ccae76784 Mon Sep 17 00:00:00 2001 From: "ben%netscape.com" Date: Fri, 20 Oct 2000 11:17:09 +0000 Subject: [PATCH] More ViXEn transaction system love. NOT PART OF BUILD. --- .../resources/content/palette/vxPalette.js | 19 +++-- .../resources/content/palette/vxPalette.xul | 1 + .../resources/content/vfd/vxAggregateTxn.js | 28 ++++--- .../vixen/resources/content/vfd/vxBaseTxn.js | 80 +++++++++++++++++++ .../content/vfd/vxChangeAttributeTxn.js | 32 ++++---- .../content/vfd/vxCreateElementTxn.js | 20 ++--- .../content/vfd/vxVFDTransactionManager.js | 13 +-- extensions/vixen/resources/content/vxUtils.js | 32 +++++--- extensions/vixen/resources/jar.mn | 1 + 9 files changed, 169 insertions(+), 57 deletions(-) create mode 100644 extensions/vixen/resources/content/vfd/vxBaseTxn.js diff --git a/extensions/vixen/resources/content/palette/vxPalette.js b/extensions/vixen/resources/content/palette/vxPalette.js index 4cad90f4a756..0f0cdf75544b 100644 --- a/extensions/vixen/resources/content/palette/vxPalette.js +++ b/extensions/vixen/resources/content/palette/vxPalette.js @@ -81,11 +81,11 @@ var vxPalette = var txns = [radiogroupTxn, radiogroupAttrTxn, radioTxn, radioAttrTxn]; var aggregateTxn = new vxAggregateTxn(txns); aggregateTxn.init(); - + var txmgr = focusedWindow.vxVFD.mTxMgrShell; - txmgr.addListener([radioAttrTxn, radioTxn, radioAttrTxn]); + aggregateTxn.addListener([radiogroupAttrTxn, radioTxn, radioAttrTxn]); txmgr.doTransaction(aggregateTxn); - txmgr.removeListener([radioAttrTxn, radioTxn, radioAttrTxn]); + aggregateTxn.removeListener([radiogroupAttrTxn, radioTxn, radioAttrTxn]); }, incrementElementCount: function (aNodeName) @@ -104,9 +104,8 @@ var vxPalette = // need to check to see if the focused window is a vfd var insertionPoint = focusedWindow.vxVFD.getInsertionPoint(); - var vfdDocument = focusedWindow.vxVFD.getContent(true).document; - + var elementTxn = new vxCreateElementTxn(vfdDocument, aNodeName, insertionPoint.parent, insertionPoint.index); elementTxn.init(); var elementAttrTxn = new vxChangeAttributeTxn(elementTxn.mID, aAttributes, aValues, false); @@ -120,6 +119,16 @@ var vxPalette = txmgr.addListener(elementAttrTxn); txmgr.doTransaction(aggregateTxn); txmgr.removeListener(elementAttrTxn); + }, + + /** + * Implements nsITransactionListener + */ + didUndo: function (aTransactionManager, aTransaction, aInterrupt) + { + // Register the palette as a transaction listener so that the instance + // count for each type of widget can be decremented when a transaction + // is done. } }; diff --git a/extensions/vixen/resources/content/palette/vxPalette.xul b/extensions/vixen/resources/content/palette/vxPalette.xul index a2f1033e7f8c..b3820ef04f4e 100644 --- a/extensions/vixen/resources/content/palette/vxPalette.xul +++ b/extensions/vixen/resources/content/palette/vxPalette.xul @@ -14,6 +14,7 @@ + diff --git a/extensions/vixen/resources/content/vfd/vxAggregateTxn.js b/extensions/vixen/resources/content/vfd/vxAggregateTxn.js index 891d28b00fb1..744f823c6298 100644 --- a/extensions/vixen/resources/content/vfd/vxAggregateTxn.js +++ b/extensions/vixen/resources/content/vfd/vxAggregateTxn.js @@ -36,33 +36,41 @@ function vxAggregateTxn (aTransactionList) this.mTransactionList[aTransactionList[i].mID] = aTransactionList[i]; this.mID = "aggregate-txn::"; + + this.mTransactionListeners = []; } vxAggregateTxn.prototype = { - init: function () - { - this.mID += generateID(); - }, + __proto__: vxBaseTxn.prototype, doTransaction: function () { - _dd("vxAggregateTxn::doTransaction"); - for (var i in this.mTransactionList) + for (var i in this.mTransactionList) { + var irq = { }; + this.notifyListeners("willDo", this.mTransactionList[i], irq); this.mTransactionList[i].doTransaction(); + this.notifyListeners("didDo", this.mTransactionList[i], irq); + } }, undoTransaction: function () { - _dd("vxAggregateTxn::undoTransaction"); - for (var i in this.mTransactionList) + for (var i in this.mTransactionList) { + var irq = { }; + this.notifyListeners("willUndo", this.mTransactionList[i], irq); this.mTransactionList[i].undoTransaction(); + this.notifyListeners("didUndo", this.mTransactionList[i], irq); + } }, redoTransaction: function () { - _dd("vxAggregateTxn::redoTransaction"); - for (var i in this.mTransactionList) + for (var i in this.mTransactionList) { + var irq = { }; + this.notifyListeners("willRedo", this.mTransactionList[i], irq); this.mTransactionList[i].redoTransaction(); + this.notifyListeners("didRedo", this.mTransactionList[i], irq); + } }, get commandString() diff --git a/extensions/vixen/resources/content/vfd/vxBaseTxn.js b/extensions/vixen/resources/content/vfd/vxBaseTxn.js new file mode 100644 index 000000000000..3ec7f810b298 --- /dev/null +++ b/extensions/vixen/resources/content/vfd/vxBaseTxn.js @@ -0,0 +1,80 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 2000 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Ben Goodger (Original Author) + */ + +function vxBaseTxn () +{ +} + +vxBaseTxn.prototype = { + mID: null, + + mListeners: [], + + notifyListeners: function (aEvent, aTransaction, aIRQ) + { + _dd("vxBaseTxn::notifyListeners"); + for (var i = 0; i < this.mListeners.length; i++) { + if (aEvent in this.mListeners[i]) { + _ddf("calling listener for transaction complete", aTransaction.commandString); + this.mListeners[i][aEvent](undefined, aTransaction, aIRQ); + } + } + }, + + addListener: function (aListener) + { + _dd("vxBaseTxn::addListener"); + this.mListeners = this.mListeners.concat(aListener); + _ddf("vxBaseTxn::mListeners", this.mListeners); + }, + + removeListener: function (aListener) + { + _dd("vxBaseTxn::removeListener"); + var listeners = [].concat(aListener); + for (var i = 0; i < this.mListeners.length; i++) { + for (var k = 0; k < listeners.length; k++) { + if (this.mListeners[i] == listeners[k]) { + this.mListeners.splice(i,1); + break; + } + } + } + }, + + init: function () + { + this.mID += this.generateID(); + }, + + /** + * Generate a unique identifier for a transaction + */ + generateID: function () + { + var val = ((new Date()).getUTCMilliseconds())*Math.random()*100000; + _ddf("value", val); + return val; + } +}; + diff --git a/extensions/vixen/resources/content/vfd/vxChangeAttributeTxn.js b/extensions/vixen/resources/content/vfd/vxChangeAttributeTxn.js index 1a762024e8c6..3d8bbf9c8929 100644 --- a/extensions/vixen/resources/content/vfd/vxChangeAttributeTxn.js +++ b/extensions/vixen/resources/content/vfd/vxChangeAttributeTxn.js @@ -57,18 +57,14 @@ function vxChangeAttributeTxn(aElement, aAttribute, aValue, aRemoveFlag) } vxChangeAttributeTxn.prototype = { - init: function () - { - this.mID += generateID(); - }, - + __proto__: vxBaseTxn.prototype, + doTransaction: function () { - if (!this.mElementCreated) + if (!this.mElementCreated) return; for (var i = 0; i < this.mAttributes.length; i++) { - _ddf("changing attribute", this.mAttributes[i]); this.mUndoValues[i] = this.mElement.getAttribute(this.mAttributes[i]); if (!this.mRemoveFlags[i]) this.mElement.setAttribute(this.mAttributes[i], this.mValues[i]); @@ -113,16 +109,18 @@ vxChangeAttributeTxn.prototype = { */ didDo: function (aTransactionManager, aTransaction, aInterrupt) { - _ddf("aTransaction is a", aTransaction.commandString); - if (aTransaction.commandString.indexOf("aggregate-txn") >= 0) { - var createElementTxn = aTransaction.mTransactionList[this.mElementTxnID]; - _ddf("the transaction ahead of us is", createElementTxn.commandString); - _ddf("and its element is", createElementTxn.mLocalName); - if (createElementTxn.commandString.indexOf("create-element") >= 0) { - this.mElement = createElementTxn.mElement; - this.mElementCreated = true; - this.doTransaction(); - } + var prevTxn = null; + if (aTransaction.commandString.indexOf("aggregate-txn") >= 0) + prevTxn = aTransaction.mTransactionList[this.mElementTxnID]; + else + prevTxn = aTransaction; + + if (prevTxn.commandString.indexOf("create-element") >= 0) { + _ddf("did create element of", prevTxn.mLocalName); + this.mElement = prevTxn.mElement; + this.mElementCreated = true; + _ddf("going to actually do this transaction now", this.mAttributes); + this.doTransaction(); } } }; diff --git a/extensions/vixen/resources/content/vfd/vxCreateElementTxn.js b/extensions/vixen/resources/content/vfd/vxCreateElementTxn.js index c19d820c4c51..e199be208a4e 100644 --- a/extensions/vixen/resources/content/vfd/vxCreateElementTxn.js +++ b/extensions/vixen/resources/content/vfd/vxCreateElementTxn.js @@ -45,17 +45,12 @@ function vxCreateElementTxn(aDocument, aLocalName, aParentNode, aChildOffset) } vxCreateElementTxn.prototype = { - init: function () - { - this.mID += generateID(); - }, + __proto__: vxBaseTxn.prototype, doTransaction: function () { - _ddf("creating element", this.mLocalName); if (!this.mElementCreated) { - _dd("aParentNode is an id, bailing early"); - _ddf("localname is", this.mLocalName); + _dd("element we rely on does not yet exist, so bail for now"); return; } @@ -96,9 +91,14 @@ vxCreateElementTxn.prototype = { */ didDo: function (aTransactionManager, aTransaction, aInterrupt) { - var createElementTxn = aTransaction.mTransactionList[this.mElementTxnID]; - if (createElementTxn.commandString.indexOf("create-element") >= 0) { - this.mElement = createElementTxn.mElement; + var prevTxn = null; + if (aTransaction.commandString.indexOf("aggregate-txn") >= 0) + prevTxn = aTransaction.mTransactionList[this.mElementTxnID]; + else + prevTxn = aTransaction; + + if (prevTxn.commandString.indexOf("create-element") >= 0) { + this.mParentNode = prevTxn.mElement; this.mElementCreated = true; this.doTransaction(); } diff --git a/extensions/vixen/resources/content/vfd/vxVFDTransactionManager.js b/extensions/vixen/resources/content/vfd/vxVFDTransactionManager.js index 576ced369655..1be2308b8973 100644 --- a/extensions/vixen/resources/content/vfd/vxVFDTransactionManager.js +++ b/extensions/vixen/resources/content/vfd/vxVFDTransactionManager.js @@ -63,8 +63,8 @@ vxVFDTransactionManager.prototype = // likewise with the RDF Seq. var seqCount = this.mTxnSeq.GetCount(); - for (i = this.mTxnStack.index; i < seqCount; i++) - this.mTxnSeq.RemoveElementAt(i); + for (i = seqCount-1; i >= this.mTxnStack.index; i--) + this.mTxnSeq.RemoveElementAt(i, true); } // append the transaction to our list @@ -76,7 +76,6 @@ vxVFDTransactionManager.prototype = for (i = 0; i < this.mTransactionListeners.length; i++) { interruptRequest = {}; if ("didDo" in this.mTransactionListeners[i]) { - _ddf("transaction listener " + i, this.mTransactionListeners[i].commandString); this.mTransactionListeners[i].didDo(this, aTransaction, interruptRequest); // do something with irq here once we figure out what it does. } @@ -98,7 +97,6 @@ vxVFDTransactionManager.prototype = // undo the transaction if (txn) { - _ddf("going to undo the txn at", this.mTxnStack.index-1); txn.undoTransaction(); this.mTxnStack.index--; } @@ -232,6 +230,12 @@ var vxVFDTransactionDS = return null; }, + GetTargets: function (aSource, aProperty, aTruthValue) + { + var targets = [].concat(this.GetTarget(aSource, aProperty, aTruthValue)); + return new ArrayEnumerator(targets); + }, + mObservers: [], AddObserver: function (aObserver) { @@ -282,7 +286,6 @@ vxVFDTransactionStack.prototype = set index (aValue) { - _ddf("setting index to", aValue); this.mIndex = aValue; return this.mIndex; }, diff --git a/extensions/vixen/resources/content/vxUtils.js b/extensions/vixen/resources/content/vxUtils.js index 8dab0c43b26a..82f47bb3fb63 100644 --- a/extensions/vixen/resources/content/vxUtils.js +++ b/extensions/vixen/resources/content/vxUtils.js @@ -44,16 +44,6 @@ var vxUtils = { }; -/** - * Generate a unique identifier for a transaction - */ -function generateID() -{ - var val = ((new Date()).getUTCMilliseconds())*Math.random()*100000; - _ddf("value", val); - return val; -} - /** * dumps the DOM tree under a given node. */ @@ -64,3 +54,25 @@ function dumpDOM(aNode) dumpDOM(aNode.childNodes[i]); } +/** + * Converts a regular js array into an nsISimpleEnumerator + */ +function ArrayEnumerator(aArray) +{ + this.mArray = aArray; + this.mIndex = 0; +} + +ArrayEnumerator.prototype = { + getNext: function () + { + return this.mArray[this.mIndex++]; + }, + + hasMoreElements: function () + { + if (this.mIndex < this.mArray.length) + return true; + return false; + } +}; diff --git a/extensions/vixen/resources/jar.mn b/extensions/vixen/resources/jar.mn index 5193883bf601..49cbb0ad96cd 100644 --- a/extensions/vixen/resources/jar.mn +++ b/extensions/vixen/resources/jar.mn @@ -18,6 +18,7 @@ vixen.jar: content/vixen/vfd/vxVFDSelectionManager.js (content/vfd/vxVFDSelectionManager.js) content/vixen/vfd/vxVFDTransactionManager.js (content/vfd/vxVFDTransactionManager.js) content/vixen/vfd/vxAggregateTxn.js (content/vfd/vxAggregateTxn.js) + content/vixen/vfd/vxBaseTxn.js (content/vfd/vxBaseTxn.js) skin/modern/vixen/contents.rdf (skin/contents.rdf) skin/modern/vixen/vfdScratch.css (skin/vfdScratch.css) locale/en-US/vixen/contents.rdf (locale/en-US/contents.rdf)