From 680bda0873b25b1141cef5f5d2d74269bc192eb6 Mon Sep 17 00:00:00 2001 From: Lucas Rocha Date: Fri, 21 Feb 2014 10:33:59 +0000 Subject: [PATCH] Bug 972306 - Restructure the Home.panels API for clarity (r=margaret) --- mobile/android/modules/Home.jsm | 78 ++++++++++++++++----------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/mobile/android/modules/Home.jsm b/mobile/android/modules/Home.jsm index 290daca0214..1b2fc0d2010 100644 --- a/mobile/android/modules/Home.jsm +++ b/mobile/android/modules/Home.jsm @@ -210,6 +210,12 @@ let HomePanels = (function () { return false; }; + let _assertPanelExists = function(id) { + if (!(id in _panels)) { + throw "Home.panels: Panel doesn't exist: id = " + id; + } + }; + return Object.freeze({ // Valid layouts for a panel. Layout: Object.freeze({ @@ -222,12 +228,6 @@ let HomePanels = (function () { GRID: "grid" }), - // Valid actions for a panel. - Action: Object.freeze({ - INSTALL: "install", - REFRESH: "refresh" - }), - // Valid item types for a panel view. Item: Object.freeze({ ARTICLE: "article", @@ -240,18 +240,16 @@ let HomePanels = (function () { INTENT: "intent" }), - add: function(options) { + register: function(options) { let panel = new Panel(options); - if (!panel.id || !panel.title) { - throw "Home.panels: Can't create a home panel without an id and title!"; + + // Bail if the panel already exists + if (panel.id in _panels) { + throw "Home.panels: Panel already exists: id = " + panel.id; } - let action = options.action; - - // Bail if the panel already exists, except when we're refreshing - // an existing panel instance. - if (panel.id in _panels && action != this.Action.REFRESH) { - throw "Home.panels: Panel already exists: id = " + panel.id; + if (!panel.id || !panel.title) { + throw "Home.panels: Can't create a home panel without an id and title!"; } if (!_valueExists(this.Layout, panel.layout)) { @@ -288,41 +286,39 @@ let HomePanels = (function () { } _panels[panel.id] = panel; - - if (action) { - let messageType; - - switch(action) { - case this.Action.INSTALL: - messageType = "HomePanels:Install"; - break; - - case this.Action.REFRESH: - messageType = "HomePanels:Refresh"; - break; - - default: - throw "Home.panels: Invalid action for panel: panel.id = " + panel.id + ", action = " + action; - } - - sendMessageToJava({ - type: messageType, - panel: _panelToJSON(panel) - }); - } }, - remove: function(id) { - if (!(id in _panels)) { - throw "Home.panels: Panel doesn't exist: id = " + id; - } + unregister: function(id) { + _assertPanelExists(id); delete _panels[id]; + }, + + install: function(id) { + _assertPanelExists(id); + + sendMessageToJava({ + type: "HomePanels:Install", + panel: _panelToJSON(_panels[id]) + }); + }, + + uninstall: function(id) { + _assertPanelExists(id); sendMessageToJava({ type: "HomePanels:Uninstall", id: id }); + }, + + update: function(id) { + _assertPanelExists(id); + + sendMessageToJava({ + type: "HomePanels:Update", + panel: _panelToJSON(_panels[id]) + }); } }); })();