diff options
Diffstat (limited to 'bandwagon/content/ui/collectionsPaneController.js')
-rw-r--r-- | bandwagon/content/ui/collectionsPaneController.js | 838 |
1 files changed, 838 insertions, 0 deletions
diff --git a/bandwagon/content/ui/collectionsPaneController.js b/bandwagon/content/ui/collectionsPaneController.js new file mode 100644 index 0000000..017e4fe --- /dev/null +++ b/bandwagon/content/ui/collectionsPaneController.js @@ -0,0 +1,838 @@ +/* ***** 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 bandwagon. + * + * The Initial Developer of the Original Code is + * Mozilla Corporation. + * Portions created by the Initial Developer are Copyright (C) 2008 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): David McNamara + * + * 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 ***** */ + +Bandwagon.Controller.CollectionsPane = new function() +{ + this.initialized = false; + this.initializedBindings = false; + + this.preferredCollection = null; + + this.elemBandwagonCollections = null; + this.elemBandwagonAddons = null; + this.elemBandwagonButtonViewSite = null; + //this.elemBandwagonButtonUpdate = null; + this.elemBandwagonButtonRemove = null; + this.elemBandwagonCollectionTitle = null; + this.elemBandwagonCollectionDescription = null; + this.elemBandwagonCollectionsNotification = null; + this.elemBandwagonCollectionDeck = null; + this.elemBandwagonCollectionHeader = null; + this.stringBundle = null; + + //this.previewNotificationVal = "bandwagon-collection-preview"; +} + +Bandwagon.Controller.CollectionsPane.init = function() +{ + if (Bandwagon.Controller.CollectionsPane.initialized == true) return; + + Bandwagon.Logger.debug("Initializing Bandwagon.Controller.CollectionsPane"); + + this.elemBandwagonCollections = document.getElementById("bandwagon-collections-list"); + this.elemBandwagonAddons = document.getElementById("bandwagon-addons-list"); + this.elemBandwagonButtonViewSite = document.getElementById("bandwagon-button-viewsite"); + //this.elemBandwagonButtonUpdate = document.getElementById("bandwagon-button-update"); + this.elemBandwagonButtonRemove = document.getElementById("bandwagon-button-remove"); + this.elemBandwagonExtensionsDeck = document.getElementById("bandwagon-extensions-deck"); + this.elemBandwagonCollectionTitle = document.getElementById("bandwagon-collection-title"); + this.elemBandwagonCollectionDescription = document.getElementById("bandwagon-collection-description"); + this.elemBandwagonCollectionsNotification = document.getElementById("bandwagon-collections-notification"); + this.elemBandwagonCollectionDeck = document.getElementById("bandwagon-collection-deck"); + this.elemBandwagonCollectionHeader = document.getElementById("bandwagon-collection-header"); + + Bandwagon.Controller.CollectionsPane._repopulateCollectionsList(); + Bandwagon.Controller.CollectionsPane.invalidate(); + + this.elemBandwagonCollections.addEventListener("select", Bandwagon.Controller.CollectionsPane.doShowCollection, true); + this.elemBandwagonAddons.addEventListener("select", Bandwagon.Controller.CollectionsPane.doExpandAddon, true); + + bandwagonService.registerCollectionUpdateObserver(Bandwagon.Controller.CollectionsPane.collectionUpdateObserver); + bandwagonService.registerCollectionListChangeObserver(Bandwagon.Controller.CollectionsPane.collectionListChangeObserver); + + Bandwagon.Preferences.addObserver(Bandwagon.Controller.CollectionsPane.prefObserver); + Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService).addObserver(Bandwagon.Controller.CollectionsPane.prefObserver, "nsPref:changed", false); +} + +/** + * Triggered when the user navigates to the collections pane from a different view in the extensions manager + */ +Bandwagon.Controller.CollectionsPane.onViewSelect = function() +{ + Bandwagon.Logger.debug("in Bandwagon.Controller.CollectionsPane.onViewSelect()"); + + if (Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem != null) + { + // make sure the expanded collection item is scrolled into view + var elemsAddonExpanded = Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.getElementsByTagName("bandwagonAddonExpanded"); + + if (elemsAddonExpanded && elemsAddonExpanded[0]) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.ensureElementIsVisible(elemsAddonExpanded[0]); + } + } +} + +Bandwagon.Controller.CollectionsPane.bindingsReady = function() +{ + // the bindings are ready, do any final initialization + + if (Bandwagon.Controller.CollectionsPane.initializedBindings == false) + { + Bandwagon.Controller.CollectionsPane._selectPreferredCollection(); + Bandwagon.Controller.CollectionsPane.initializedBindings = true; + } + + Bandwagon.Controller.CollectionsPane.initialized = true; +} + +Bandwagon.Controller.CollectionsPane.uninit = function() +{ + if (Bandwagon.Controller.CollectionsPane.initialized != true) return; + + Bandwagon.Logger.debug("Uninitializing Bandwagon.Controller.CollectionsPane"); + + Bandwagon.Controller.CollectionsPane.initialized = false; + + bandwagonService.unregisterCollectionUpdateObserver(Bandwagon.Controller.CollectionsPane.collectionUpdateObserver); + bandwagonService.unregisterCollectionListChangeObserver(Bandwagon.Controller.CollectionsPane.collectionListChangeObserver); + + Bandwagon.Preferences.removeObserver(Bandwagon.Controller.CollectionsPane.prefObserver); + + // now is a good time to save collections to storage + if (Bandwagon.COMMIT_NOW) + bandwagonService.commitAll(); +} + +/** + * Updates the interface disabled-ness based on the state of the selected collection, etc. + */ +Bandwagon.Controller.CollectionsPane.invalidate = function() +{ + if (Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem == null) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonButtonViewSite.disabled = true; + //Bandwagon.Controller.CollectionsPane.elemBandwagonButtonUpdate.disabled = true; + Bandwagon.Controller.CollectionsPane.elemBandwagonButtonRemove.disabled = true; + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionHeader.collapsed = true; + + Bandwagon.Controller.CollectionsPane._repopulateAddonsList(null); + + Bandwagon.Controller.CollectionsPane._invalidateExtensionsDeck(); + } + else + { + Bandwagon.Controller.CollectionsPane.elemBandwagonButtonViewSite.disabled = false; + //Bandwagon.Controller.CollectionsPane.elemBandwagonButtonUpdate.disabled = false; + Bandwagon.Controller.CollectionsPane.elemBandwagonButtonRemove.disabled = false; + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionHeader.collapsed = false; + + var collection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.collection; + + if (collection.status == collection.STATUS_LOADING) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDeck.selectedIndex = 1; + } + else if (collection.status == collection.STATUS_LOADERROR) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDeck.selectedIndex = 3; + } + else if (collection.hasAddon() == 0) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDeck.selectedIndex = 2; + } + else + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDeck.selectedIndex = 0; + } + } +} + +Bandwagon.Controller.CollectionsPane._invalidateExtensionsDeck = function() +{ + var elemsBandwagonCollection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.getElementsByTagName("bandwagonCollection"); + + if (elemsBandwagonCollection.length == 0) + { + if (!bandwagonService.isAMOAuthenticated()) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonExtensionsDeck.selectedIndex = 2; + } + else + { + Bandwagon.Controller.CollectionsPane.elemBandwagonExtensionsDeck.selectedIndex = 1; + } + } + else + { + Bandwagon.Controller.CollectionsPane.elemBandwagonExtensionsDeck.selectedIndex = 0; + } +} + +Bandwagon.Controller.CollectionsPane.collectionListChangeObserver = function() +{ + Bandwagon.Logger.debug("In Bandwagon.Controller.CollectionsPane.collectionListChangeObserver()"); + + var prevCollection = null; + + if (Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem) + { + prevCollection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.collection; + } + + Bandwagon.Controller.CollectionsPane._repopulateCollectionsList(); + Bandwagon.Controller.CollectionsPane._invalidateExtensionsDeck(); + + if (prevCollection != null) + { + Bandwagon.Controller.CollectionsPane._selectCollection(prevCollection); + } +} + +Bandwagon.Controller.CollectionsPane.collectionUpdateObserver = function(collection) +{ + if (collection == null) + { + Bandwagon.Logger.debug("In Bandwagon.Controller.CollectionsPane.collectionUpdateObserver() with collection <null>"); + } + else + { + Bandwagon.Logger.debug("In Bandwagon.Controller.CollectionsPane.collectionUpdateObserver() with collection '" + collection.toString() + "'"); + + // update the unread count for this collection + + const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; + var elemsBandwagonCollection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.getElementsByTagNameNS(XULNS, "bandwagonCollection"); + + for (var i=0; i<elemsBandwagonCollection.length; i++) + { + if (elemsBandwagonCollection[i].collection.equals(collection)) + { + elemsBandwagonCollection[i].unread = collection.getUnreadAddons().length; + break; + } + } + + // if this collection is currently selected, update the view + + if (Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem && collection.equals(Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.collection)) + { + Bandwagon.Controller.CollectionsPane._repopulateAddonsList(collection); + + /* OBSOLETE + // if this is a just arrived collection preview, show the notification box + if (collection.preview) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionsNotification.notificationsHidden = false; + setTimeout(Bandwagon.Controller.CollectionsPane._createPreviewNotification, 200); + } + */ + } + } + + Bandwagon.Controller.CollectionsPane.invalidate(); +} + +Bandwagon.Controller.CollectionsPane.doUpdateAll = function(event) +{ + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDeck.selectedIndex = 1; + + bandwagonService.forceCheckAllForUpdatesAndUpdateCollectionsList(); +} + +Bandwagon.Controller.CollectionsPane.doSubscribe = function(event) +{ + Bandwagon.Controller.CollectionsPane._openLocalizedURL(Bandwagon.COLLECTIONSPANE_DO_SUBSCRIBE_URL); +} + +Bandwagon.Controller.CollectionsPane.doLogin = function(event) +{ + Bandwagon.Controller.CollectionsPane._openLocalizedURL(Bandwagon.COLLECTIONSPANE_DO_LOGIN_URL); +} + + +Bandwagon.Controller.CollectionsPane.doSettings = function(event) +{ + var prefSvc = Components.classes["@mozilla.org/preferences-service;1"]. + getService(Components.interfaces.nsIPrefService); + var prefServiceCache = prefSvc.getBranch(null); + var instantApply = prefServiceCache.getBoolPref("browser.preferences.instantApply"); + var flags = "chrome,titlebar,toolbar,centerscreen" + (instantApply ? ",dialog=no" : ",modal"); + + var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] + .getService(Components.interfaces.nsIWindowMediator); + var win = wm.getMostRecentWindow("Bandwagon:Settings"); + + if (win) + { + win.focus(); + } + else + { + window.openDialog("chrome://bandwagon/content/ui/settings.xul", + "bandwagonsettings", + flags); + } +} + +Bandwagon.Controller.CollectionsPane.doViewSite = function(event) +{ + // TODO use url from collection + Bandwagon.Controller.CollectionsPane._openURL("http://www.google.com/search?hl=en&q=bandwagon+view+site&btnG=Search"); +} + +Bandwagon.Controller.CollectionsPane.doUpdate = function(event) +{ + if (Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem == null) + return; + + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDeck.selectedIndex = 1; + + var collection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.collection; + + if (collection == null) + return; + + bandwagonService.forceCheckForUpdates(collection); +} + +Bandwagon.Controller.CollectionsPane.doRemove = function(event) +{ + // TODO + alert('TBD'); +} + +/** OBSOLETE +Bandwagon.Controller.CollectionsPane.doRemove = function(event) +{ + if (Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem == null) + return; + + var collection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.collection; + + if (collection == null) + return; + + bandwagonService.deleteCollection(collection); + + if (!collection.preview) + bandwagonService.unsubscribe(collection); +} +*/ + +Bandwagon.Controller.CollectionsPane.doShowCollection = function() +{ + var collection = null; + + var selectedElemBandwagonCollection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem; + + if (selectedElemBandwagonCollection != null) + { + collection = selectedElemBandwagonCollection.collection; + } + + // if collection == null, then display will be cleared + + // misc. ui + + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionTitle.setAttribute("value", (collection?(collection.name?collection.name:collection.resourceURL):"")); + + if (collection && collection.description != "") + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDescription.setAttribute("value", collection.description); + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDescription.removeAttribute("collsaped"); + } + else + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDescription.setAttribute("collapsed", true); + } + + /** OBSOLETE + // collection preview notification box + if (collection && collection.preview && collection.status != collection.STATUS_LOADING) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionsNotification.notificationsHidden = false; + + // this needs to be in a seperate thread + setTimeout(Bandwagon.Controller.CollectionsPane._createPreviewNotification, 500); + } + else + */ + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionsNotification.notificationsHidden = true; + } + + // show items + + Bandwagon.Controller.CollectionsPane._repopulateAddonsList(collection); + + // invalidate + + Bandwagon.Controller.CollectionsPane.invalidate(); + + // show the loading dialog if needed + + if (collection && collection.status == collection.STATUS_LOADING) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionDeck.selectedIndex = 1; + } + + // set all items in this collection to be "read" + // if we've just opened this dialog, don't update the read count + + if (collection) + { + collection.setAllRead(); + + if (Bandwagon.Controller.CollectionsPane.initialized) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.unread = 0; + } + } +} + +Bandwagon.Controller.CollectionsPane.doExpandAddon = function(event) +{ + if (event) + event.preventDefault(); + + if (Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.selectedItem && Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.selectedItem.nodeName == "bandwagonAddonExpanded") + { + return; + } + + var elemsAddonExpanded = Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.getElementsByTagName("bandwagonAddonExpanded"); + + for (var i=0; i<elemsAddonExpanded.length; i++) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.removeChild(elemsAddonExpanded[i]); + } + + var elemsAddon = Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.childNodes; + + for (var i=0; i<elemsAddon.length; i++) + { + elemsAddon[i].collapsed = false; + } + + var selectedElemBandwagonAddon = Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.selectedItem; + + if (selectedElemBandwagonAddon != null && selectedElemBandwagonAddon.addon != null) + { + selectedElemBandwagonAddon.read = true; + + var addon = selectedElemBandwagonAddon.addon; + addon.read = true; + + Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.unread = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.collection.getUnreadAddons().length; + + // collapse this, show the expanded binding + + const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; + var elemBandwagonAddonExpanded = document.createElementNS(XULNS, "bandwagonAddonExpanded"); + + elemBandwagonAddonExpanded.addon = addon; + + try + { + Bandwagon.Controller.CollectionsPane.elemBandwagonAddonExpanded.setAddon(addon); + } catch (e) {} + + Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.insertBefore(elemBandwagonAddonExpanded, selectedElemBandwagonAddon); + + selectedElemBandwagonAddon.collapsed = true; + } +} + +Bandwagon.Controller.CollectionsPane.doMoreInfo = function(event) +{ + if (event) + event.preventDefault(); + + if (Bandwagon.Controller.CollectionsPane.elemBandwagonAddons == null) + return; + + Bandwagon.Controller.CollectionsPane._openURL(Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.selectedItem.addon.learnmore); +} + +Bandwagon.Controller.CollectionsPane.doAddToFirefox = function() +{ + if (Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.selectedItem == null) + return; + + var addon = Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.selectedItem.addon; + + if (!isXPInstallEnabled()) + return; + + if (addon.eula && addon.eula != "") + { + var eula = { + name: addon.name, + text: addon.eula, + accepted: false + }; + + window.openDialog("chrome://mozapps/content/extensions/eula.xul", "_blank", + "chrome,dialog,modal,centerscreen,resizable=no", eula); + + if (!eula.accepted) + return; + } + + var installer = addon.getInstaller(Bandwagon.Util.getHostEnvironmentInfo().os); + + if (!installer) + { + Bandwagon.Logger.warn("No compatible os targets found."); + return; + } + + var params = []; + params[addon.name] = installer; + + // TODO do some user collectionback here? + + var callback = function(url, status) + { + Bandwagon.Logger.info("Finished installing '" + url + "'; status = " + status); + + // TODO some user collectionback here? + } + + InstallTrigger.install(params, callback); +} + +Bandwagon.Controller.CollectionsPane.doUpgradeToFirefoxN = function(version) +{ + Bandwagon.Logger.info("in Bandwagon.Controller.CollectionsPane.doUpgradeToFirefoxN() with version = " + version); + + Bandwagon.Controller.CollectionsPane._openURL("http://www.mozilla.com/en-US/firefox/all.html"); +} + +Bandwagon.Controller.CollectionsPane.doDownloadFirefoxNBeta = function(version) +{ + Bandwagon.Logger.info("in Bandwagon.Controller.CollectionsPane.doDownloadFirefoxNBeta() with version = " + version); + + Bandwagon.Controller.CollectionsPane._openURL("http://www.mozilla.com/en-US/firefox/all-beta.html"); +} + +/** OBSOLETE +Bandwagon.Controller.CollectionsPane.doShowCollectionPreview = function(collectionURL) +{ + // this is executed when the browser overlay controller opens the EM window to subscribe to a new collection. + // i.e. after a custom bandwagonSubscribe is detected and handled + + Bandwagon.Logger.debug("In Bandwagon.Controller.CollectionsPane.doShowCollectionPreview() with collectionURL = " + collectionURL); + + // check if we already have this collection + + if (bandwagonService.collections[collectionURL]) + { + var collection = bandwagonService.collections[collectionURL]; + Bandwagon.Controller.CollectionsPane.preferredCollection = collection; + //Bandwagon.Controller.CollectionsPane._selectCollection(collection); + setTimeout(function() { Bandwagon.Controller.CollectionsPane._selectCollection(collection); }, 500); + return; + } + + // add and select the collection + + var collection = bandwagonService.addPreviewCollection(collectionURL); + //collection.status = collection.STATUS_LOADING; + + Bandwagon.Controller.CollectionsPane.preferredCollection = collection; + setTimeout(function() { Bandwagon.Controller.CollectionsPane._selectCollection(collection); }, 500); +} +*/ + +/** OBSOLETE +Bandwagon.Controller.CollectionsPane._createPreviewNotification = function() +{ + if (!Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionsNotification.getNotificationWithValue(Bandwagon.Controller.CollectionsPane.previewNotificationVal)) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionsNotification.appendNotification + ( + Bandwagon.Controller.ExtensionsOverlay.stringBundle.getString("collectionspane.this.is.only.a.preview"), + Bandwagon.Controller.CollectionsPane.previewNotificationVal, + null, + Bandwagon.Controller.CollectionsPane.elemBandwagonCollectionsNotification.PRIORITY_WARNING_MEDIUM, + [ + { + accessKey: Bandwagon.Controller.ExtensionsOverlay.stringBundle.getString("collectionspane.dont.subscribe.accesskey"), + callback: Bandwagon.Controller.CollectionsPane.doCollectionPreviewDontSubscribe, + label: Bandwagon.Controller.ExtensionsOverlay.stringBundle.getString("collectionspane.dont.subscribe"), + popup: null + }, + { + accessKey: Bandwagon.Controller.ExtensionsOverlay.stringBundle.getString("collectionspane.subscribe.accesskey"), + callback: Bandwagon.Controller.CollectionsPane.doCollectionPreviewSubscribe, + label: Bandwagon.Controller.ExtensionsOverlay.stringBundle.getString("collectionspane.subscribe"), + popup: null + } + ] + ); + } +} +*/ + +/** OBSOLETE +Bandwagon.Controller.CollectionsPane.doCollectionPreviewDontSubscribe = function() +{ + Bandwagon.Logger.debug("in Bandwagon.Controller.CollectionsPane.doCollectionPreviewDontSubscribe()"); + + Bandwagon.Controller.CollectionsPane.preferredCollection = null; + + Bandwagon.Controller.CollectionsPane.doRemove(); +} +*/ + +/** OBSOLETE +Bandwagon.Controller.CollectionsPane.doCollectionPreviewSubscribe = function() +{ + Bandwagon.Logger.debug("in Bandwagon.Controller.CollectionsPane.doCollectionPreviewSubscribe()"); + + if (Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem == null) + return; + + var collection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.collection; + + if (collection == null) + return; + + bandwagonService.subscribe(collection); + + if (Bandwagon.COMMIT_NOW) + bandwagonService.commit(collection); + + Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem.preview = false; + Bandwagon.Controller.CollectionsPane.doShowCollection(); +} +*/ + +/** + * Refreshes the collection pane + */ +Bandwagon.Controller.CollectionsPane.refresh = function() +{ + var selectedElemBandwagonCollection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectedItem; + + if (selectedElemBandwagonCollection != null) + { + collection = selectedElemBandwagonCollection.collection; + Bandwagon.Controller.CollectionsPane._repopulateAddonsList(collection); + } + + Bandwagon.Controller.CollectionsPane.invalidate(); +} + +Bandwagon.Controller.CollectionsPane.prefObserver = +{ + observe: function(subject, topic, data) + { + if (topic != "nsPref:changed") + return; + + if (data.match(/addonsperpage/)) + { + Bandwagon.Controller.CollectionsPane.refresh(); + } + } +} + +/** + * Function to select a collection ui programmatically based on its collection object. + */ +Bandwagon.Controller.CollectionsPane._selectCollection = function(collection) +{ + // select the collection and show (collection == null clears the selection) + + if (collection == null) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.clearSelection(); + Bandwagon.Controller.CollectionsPane.doShowCollection(); + return; + } + + // select the richlistitem + + const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; + + var elemsBandwagonCollection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.getElementsByTagNameNS(XULNS, "bandwagonCollection"); + var elemBandwagonCollection = null; + + for (var i=0; i<elemsBandwagonCollection.length; i++) + { + if (elemsBandwagonCollection[i].collection.equals(collection)) + { + elemBandwagonCollection = elemsBandwagonCollection[i]; + break; + } + } + + if (elemBandwagonCollection == null) + { + Bandwagon.Logger.warn("could not find a richlistitem to select"); + return; + } + + Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.selectItem(elemBandwagonCollection); + Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.ensureElementIsVisible(elemBandwagonCollection); + Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.focus(); +} + +Bandwagon.Controller.CollectionsPane._selectPreferredCollection = function() +{ + // select a collection - last selected or the first one + var elemsBandwagonCollection = Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.getElementsByTagName("bandwagonCollection"); + + if (Bandwagon.Controller.CollectionsPane.preferredCollection != null && bandwagonService.collections[Bandwagon.Controller.CollectionsPane.preferredCollection.resourceURL] != null) + { + Bandwagon.Logger.debug("selecting preferred collection (defined)"); + Bandwagon.Controller.CollectionsPane._selectCollection(Bandwagon.Controller.CollectionsPane.preferredCollection); + } + else if (elemsBandwagonCollection.length > 0) + { + Bandwagon.Logger.debug("selecting preferred collection (the first in the list)"); + Bandwagon.Controller.CollectionsPane._selectCollection(elemsBandwagonCollection[0].collection); + } + else + { + Bandwagon.Logger.debug("preferred collection is none"); + Bandwagon.Controller.CollectionsPane._selectCollection(null); + } +} + +Bandwagon.Controller.CollectionsPane._repopulateCollectionsList = function() +{ + //Bandwagon.Logger.debug("Bandwagon.Controller.CollectionsPane: about to repopulate the collections list"); + + // first clear the list + + while (Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.hasChildNodes()) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.removeChild(Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.firstChild); + } + + // repopulate with collections + + for (var id in bandwagonService.collections) + { + var collection = bandwagonService.collections[id]; + + if (collection == null) + return; + + const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; + var elemBandwagonCollection = document.createElementNS(XULNS, "bandwagonCollection"); + elemBandwagonCollection.collection = collection; + elemBandwagonCollection.controller = Bandwagon.Controller.CollectionsPane; + + Bandwagon.Controller.CollectionsPane.elemBandwagonCollections.appendChild(elemBandwagonCollection); + } +} + +Bandwagon.Controller.CollectionsPane._repopulateAddonsList = function(collection) +{ + // first clear the list + + while (Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.hasChildNodes()) + { + Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.removeChild(Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.firstChild); + } + + Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.clearSelection(); + + if (collection == null) + return; + + Bandwagon.Logger.debug("Bandwagon.Controller.CollectionsPane: repopulating collection '" + collection.resourceURL + "'"); + + // sort by addon.dateAdded + + var addonsSorted = collection.getSortedAddons(); + + // repopulate with collection items + + var addonsPerPage = bandwagonService.getAddonsPerPage(collection); + const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; + + for (var i=0; (i<addonsSorted.length&&i<addonsPerPage); i++) + { + var addon = collection.addons[addonsSorted[i].guid]; + + if (addon == null) + continue; + + var elemBandwagonAddon = document.createElementNS(XULNS, "bandwagonAddon"); + elemBandwagonAddon.addon = addon; + + Bandwagon.Controller.CollectionsPane.elemBandwagonAddons.appendChild(elemBandwagonAddon); + } +} + +Bandwagon.Controller.CollectionsPane._openURL = function(url) +{ + var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] + .getService(Components.interfaces.nsIWindowMediator); + var mainWindow = wm.getMostRecentWindow("navigator:browser"); + + if (mainWindow) + { + var tab = mainWindow.getBrowser().addTab(url); + mainWindow.getBrowser().selectedTab = tab; + mainWindow.focus(); + } + else + { + window.open(url); + } +} + +Bandwagon.Controller.CollectionsPane._openLocalizedURL = function(url) +{ + var locale = Bandwagon.Util.getBrowserLocale(); + + if (locale && locale != "") + url = url.replace(/en-US/, locale, "g"); + + Bandwagon.Controller.CollectionsPane._openURL(url); +} + +// when this window closes, we do any uninit stuff + +window.addEventListener("unload", Bandwagon.Controller.CollectionsPane.uninit, true); + |