diff --git a/src/sunstone/public/app/app.js b/src/sunstone/public/app/app.js index 110ffe1388..c1371e81db 100644 --- a/src/sunstone/public/app/app.js +++ b/src/sunstone/public/app/app.js @@ -9,11 +9,20 @@ define(function(require) { require('tabs/datastores-tab') ]; + var _commonDialogs = [ + require('utils/dialogs/confirm'), + require('utils/dialogs/confirm-with-select') + ] + + Sunstone.addDialogs(_commonDialogs); + $.each(_tabs, function(index, tab) { Sunstone.addMainTab(tab); }); $(document).ready(function() { Sunstone.insertTabs(); + Sunstone.insertDialog(require('utils/dialogs/confirm')) + Sunstone.insertDialog(require('utils/dialogs/confirm-with-select')) }); }); diff --git a/src/sunstone/public/app/sunstone.js b/src/sunstone/public/app/sunstone.js index c7bf508a1a..64d7a9753e 100644 --- a/src/sunstone/public/app/sunstone.js +++ b/src/sunstone/public/app/sunstone.js @@ -2,15 +2,19 @@ define(function(require) { require('jquery'); require('foundation.reveal'); require('foundation.tab'); + require('foundation.dropdown'); var Config = require('sunstone-config'); var Locale = require('utils/locale'); var Notifier = require('utils/notifier'); var TOP_INTERVAL = 10000; //ms + var CONFIRM_DIALOG_ID = require('utils/dialogs/confirm/dialogId'); + var CONFIRM_WITH_SELECT_DIALOG_ID = require('utils/dialogs/confirm-with-select/dialogId'); var SunstoneCfg = { "actions" : {}, + "dialogs" : {}, "tabs" : {}, "form_panels" : {} }; @@ -21,32 +25,45 @@ define(function(require) { SunstoneCfg["tabs"][_tabId] = tabObj; var actions = tabObj.actions; - if (actions) { - $.each(actions, function(actionName, action){ - SunstoneCfg["actions"][actionName] = action; - }) + if (actions) { + _addActions(actions) } var panels = tabObj.panels; if (panels) { - var indexedPanels = {} - $.each(panels, function(index, panel){ - indexedPanels[panel.panelId] = panel - }) - SunstoneCfg["tabs"][_tabId]['panels'] = indexedPanels; + _addPanels(_tabId, panels) } var dialogs = tabObj.dialogs; if (dialogs) { - var indexedDialogs = {} - $.each(dialogs, function(index, dialog){ - indexedDialogs[dialog.dialogId] = dialog - }) - SunstoneCfg["tabs"][_tabId]['dialogs'] = indexedDialogs; + _addDialogs(dialogs) } } } + var _addActions = function(actions) { + $.each(actions, function(actionName, action) { + SunstoneCfg["actions"][actionName] = action; + }) + return false; + } + + var _addDialogs = function(dialogs) { + $.each(dialogs, function(index, dialog) { + SunstoneCfg['dialogs'][dialog.dialogId] = dialog + }) + return false; + } + + var _addPanels = function(tabId, panels) { + var indexedPanels = {} + $.each(panels, function(index, panel) { + indexedPanels[panel.panelId] = panel + }) + SunstoneCfg["tabs"][tabId]['panels'] = indexedPanels; + return false; + } + //Inserts all main tabs in the DOM var _insertTabs = function() { for (tabName in SunstoneCfg["tabs"]) { @@ -327,7 +344,7 @@ define(function(require) { //Listen for .action_buttons //An action buttons runs a predefined action. If it has type //"multiple" it runs that action on the elements of a datatable. - $('.action_button').on("click", function() { + $(document).on("click", '.action_button', function() { var error = 0; var value = $(this).val() if ($.isEmptyObject(value)) { @@ -356,14 +373,34 @@ define(function(require) { if (!error && !$(this).hasClass("refresh")) { //proceed to close confirm dialog in //case it was open - $('div#confirm_dialog').foundation('reveal', 'close'); + _hideDialog(CONFIRM_DIALOG_ID); }; return false; }); + //Listen .confirm_buttons. These buttons show a confirmation dialog + //before running the action. + $(document).on("click", '.confirm_button', function() { + $('#' + CONFIRM_DIALOG_ID).data('buttonAction', $(this).attr('href')); + $('#' + CONFIRM_DIALOG_ID).data('buttonTab', $(this).parents('.tab').attr('id')); + _showDialog(CONFIRM_DIALOG_ID); + return false; + }); + + //Listen .confirm_buttons. These buttons show a confirmation dialog + //with a select box before running the action. + $(document).on("click", '.confirm_with_select_button', function() { + $('#' + CONFIRM_WITH_SELECT_DIALOG_ID).data('buttonAction', $(this).attr('href')); + $('#' + CONFIRM_WITH_SELECT_DIALOG_ID).data('buttonTab', $(this).parents('.tab').attr('id')); + _showDialog(CONFIRM_WITH_SELECT_DIALOG_ID); + return false; + }); + + $(document).foundation('reflow', 'dropdown'); + // Button to return to the list view from the detailed view - $("a[href='back']").on("click", function(e) { + $(document).on("click", "a[href='back']", function(e) { $(".navigation-active-li a", $("#navigation")).click(); e.preventDefault(); }); @@ -528,45 +565,45 @@ define(function(require) { $(document).foundation('reflow', 'tab'); } - var _insertDialog = function(tabName, dialog) { + var _insertDialog = function(dialog) { var dialogElement = $(dialog.html()).appendTo('div#dialogs'); dialog.setup(dialogElement); dialogElement.foundation('reveal', 'reflow'); dialogElement.on('opened.fndtn.reveal', function () { - dialog.onShow(dialogElement); + dialog.onShow(dialog.dialogId); }); - dialogElement.on('click', '.resetDialog', function(){ - _resetDialog(tabName, dialog.dialogId); - _showDialog(tabName, dialog.dialogId); + dialogElement.on('click', '.resetDialog', function() { + _resetDialog(dialog.dialogId); + _showDialog(dialog.dialogId); }) return dialogElement; } - var _showDialog = function(tabName, dialogId) { - var dialog = SunstoneCfg['tabs'][tabName]['dialogs'][dialogId]; + var _showDialog = function(dialogId) { + var dialog = SunstoneCfg['dialogs'][dialogId]; var dialogElement = $('#' + dialog.dialogId); if (dialogElement.length == 0) { - dialogElement = _insertDialog(tabName, dialog); + dialogElement = _insertDialog(dialog); } dialogElement.foundation('reveal', 'open'); return false; } - var _hideDialog = function(tabName, dialogId) { - var dialog = SunstoneCfg['tabs'][tabName]['dialogs'][dialogId]; + var _hideDialog = function(dialogId) { + var dialog = SunstoneCfg['dialogs'][dialogId]; var dialogElement = $('#' + dialog.dialogId); dialogElement.foundation('reveal', 'close') } - var _resetDialog = function(tabName, dialogId) { - var dialog = SunstoneCfg['tabs'][tabName]['dialogs'][dialogId]; + var _resetDialog = function(dialogId) { + var dialog = SunstoneCfg['dialogs'][dialogId]; var dialogElement = $('#' + dialog.dialogId); dialogElement.remove(); - dialogElement = _insertDialog(tabName, dialog); + dialogElement = _insertDialog(dialog); return false; } @@ -764,11 +801,17 @@ define(function(require) { return $(".resource-id", context).text(); } + var _getAction = function(actionId) { + return SunstoneCfg["actions"][actionId]; + } + var Sunstone = { "addMainTab": _addMainTab, + "addDialogs": _addDialogs, "insertTabs": _insertTabs, "insertPanels": _insertPanels, + "insertDialog": _insertDialog, 'showTab': _showTab, "showElement" : _showElement, @@ -780,7 +823,9 @@ define(function(require) { "rightListVisible": _rightListVisible, "rightInfoResourceId": _rightInfoResourceId, - "runAction" : _runAction + "runAction" : _runAction, + "getAction": _getAction, + "getButton": _getButton } return Sunstone; diff --git a/src/sunstone/public/app/tabs/datastores-tab/actions.js b/src/sunstone/public/app/tabs/datastores-tab/actions.js index 2704eba426..c64287e627 100644 --- a/src/sunstone/public/app/tabs/datastores-tab/actions.js +++ b/src/sunstone/public/app/tabs/datastores-tab/actions.js @@ -15,8 +15,8 @@ define(function(require) { type: "create", call : OpenNebulaDatastore.create, callback : function(request, response) { - Sunstone.hideDialog(TAB_ID, CREATE_DIALOG_ID); - Sunstone.resetDialog(TAB_ID, CREATE_DIALOG_ID); + Sunstone.hideDialog(CREATE_DIALOG_ID); + Sunstone.resetDialog(CREATE_DIALOG_ID); DatastoresDataTable.addElement(request, response); Notifier.notifyCustom(Locale.tr("Datastore created"), " ID: " + response.DATASTORE.ID, false); }, @@ -26,7 +26,7 @@ define(function(require) { "Datastore.create_dialog" : { type: "custom", call: function() { - Sunstone.showDialog(TAB_ID, CREATE_DIALOG_ID); + Sunstone.showDialog(CREATE_DIALOG_ID); } }, diff --git a/src/sunstone/public/app/tabs/zones-tab/actions.js b/src/sunstone/public/app/tabs/zones-tab/actions.js index 4347c42641..8cec345b40 100644 --- a/src/sunstone/public/app/tabs/zones-tab/actions.js +++ b/src/sunstone/public/app/tabs/zones-tab/actions.js @@ -13,8 +13,8 @@ define(function(require) { type: "create", call: OpenNebulaZone.create, callback: function(request, response) { - Sunstone.hideDialog(TAB_ID, CREATE_DIALOG_ID); - Sunstone.resetDialog(TAB_ID, CREATE_DIALOG_ID); + Sunstone.hideDialog(CREATE_DIALOG_ID); + Sunstone.resetDialog(CREATE_DIALOG_ID); ZonesDataTable.addElement(request, response); }, error: Notifier.onError, @@ -24,7 +24,7 @@ define(function(require) { "Zone.create_dialog" : { type: "custom", call: function() { - Sunstone.showDialog(TAB_ID, CREATE_DIALOG_ID); + Sunstone.showDialog(CREATE_DIALOG_ID); } }, diff --git a/src/sunstone/public/app/utils/dialogs/confirm-with-select.js b/src/sunstone/public/app/utils/dialogs/confirm-with-select.js new file mode 100644 index 0000000000..1b89ecdd46 --- /dev/null +++ b/src/sunstone/public/app/utils/dialogs/confirm-with-select.js @@ -0,0 +1,88 @@ +define(function(require) { + var TemplateHTML = require('hbs!./confirm-with-select/html'); + var Sunstone = require('sunstone'); + var Locale = require('utils/locale'); + var Notifier = require('utils/notifier'); + var DIALOG_ID = require('./confirm-with-select/dialogId'); + + var ResourceSelect = require('utils/resource-select'); + + var _html = function() { + return TemplateHTML({dialogId: DIALOG_ID}); + } + + var _setup = function(dialog) { + //when we proceed with a "confirm with select" we need to + //find out if we are running an action with a parametre on a datatable + //items or if its just an action + $('#confirm_with_select_proceed', dialog).click(function() { + var action = $(this).val(); + var action = Sunstone.getActions[action]; + var param = $('.resource_list_select', dialog).val(); + + if (!param.length) { + notifyError("You must select a value"); + return false; + }; + + if (!action) { + notifyError("Action " + action + " not defined."); + return false; + }; + + var error; + switch (action.type){ + case "multiple": + error = Sunstone.runAction(action, action.elements(), param); + break; + default: + error = Sunstone.runAction(action, param); + break; + } + + if (!error) { + dialog.foundation('reveal', 'close'); + } + + return false; + }); + + return false; + } + + var _onShow = function(dialog) { + var actionId = dialog.data('buttonAction'); + var tabId = dialog.data('buttonTab'); + var button = Sunstone.getButton(tabId, action); + + var tip = Locale.tr("You have to confirm this action"); + if (button.tip == undefined) { + tip = button.tip + } + + if (button.custom_select) { + $('div#confirm_select', dialog).html(button.custom_select); + } else { + ResourceSelect.insert('#confirm_select', dialog, button.select, null, true); + } + + $('#confirm_with_select_tip', dialog).text(tip); + $('#confirm_with_select_proceed', dialog).val(action); + + var action = Sunstone.getAction(action); + var elements = action.elements(); + if (elements) { + var str = action.split('.'); + $(".confirm_action", dialog).html(str[1] + ' ' + str[0] + ': ' + elements.join(', ')) + } + + return false; + } + + return { + 'dialogId': DIALOG_ID, + 'html': _html, + 'setup': _setup, + 'onShow': _onShow + } +}); diff --git a/src/sunstone/public/app/utils/dialogs/confirm-with-select/dialogId.js b/src/sunstone/public/app/utils/dialogs/confirm-with-select/dialogId.js new file mode 100644 index 0000000000..54726fcbaf --- /dev/null +++ b/src/sunstone/public/app/utils/dialogs/confirm-with-select/dialogId.js @@ -0,0 +1,3 @@ +define(function(require){ + return 'confirmWithSelectDialog'; +}) \ No newline at end of file diff --git a/src/sunstone/public/app/utils/dialogs/confirm-with-select/html.hbs b/src/sunstone/public/app/utils/dialogs/confirm-with-select/html.hbs new file mode 100644 index 0000000000..143a6384b8 --- /dev/null +++ b/src/sunstone/public/app/utils/dialogs/confirm-with-select/html.hbs @@ -0,0 +1,25 @@ +
+
+

+ {{tr "Confirm"}} +
+   + +

+
+
+
+
{{tr "You need to select something."}}
+
+
+
+
+
+
+
+
+ +
+ × +
+
\ No newline at end of file diff --git a/src/sunstone/public/app/utils/dialogs/confirm.js b/src/sunstone/public/app/utils/dialogs/confirm.js new file mode 100644 index 0000000000..4ee327f30c --- /dev/null +++ b/src/sunstone/public/app/utils/dialogs/confirm.js @@ -0,0 +1,46 @@ +define(function(require) { + var TemplateHTML = require('hbs!./confirm/html'); + var Sunstone = require('sunstone'); + var Locale = require('utils/locale'); + var DIALOG_ID = require('./confirm/dialogId'); + + var _html = function() { + return TemplateHTML({dialogId: DIALOG_ID}); + } + + var _setup = function(dialog) { + // Submit action is configured in sunstone.js since it's an action_button + return false; + } + + var _onShow = function(dialogId) { + var dialog = $('#' + dialogId); + var actionId = dialog.data('buttonAction'); + var tabId = dialog.data('buttonTab'); + var button = Sunstone.getButton(tabId, actionId); + + var tip = Locale.tr("You have to confirm this action"); + if (button.tip) { + tip = button.tip + } + + $('#confirm_proceed', dialog).val(actionId); + $('#confirm_tip', dialog).text(tip); + + var action = Sunstone.getAction(actionId); + var elements = action.elements(); + if (elements) { + var str = actionId.split('.'); + $(".confirm_action", dialog).html(str[1] + ' ' + str[0] + ': ' + elements.join(', ')) + } + + return false; + } + + return { + 'dialogId': DIALOG_ID, + 'html': _html, + 'setup': _setup, + 'onShow': _onShow + } +}); diff --git a/src/sunstone/public/app/utils/dialogs/confirm/dialogId.js b/src/sunstone/public/app/utils/dialogs/confirm/dialogId.js new file mode 100644 index 0000000000..9f53100fc9 --- /dev/null +++ b/src/sunstone/public/app/utils/dialogs/confirm/dialogId.js @@ -0,0 +1,3 @@ +define(function(require){ + return 'confirmDialog'; +}) \ No newline at end of file diff --git a/src/sunstone/public/app/utils/dialogs/confirm/html.hbs b/src/sunstone/public/app/utils/dialogs/confirm/html.hbs new file mode 100644 index 0000000000..45d1244e5b --- /dev/null +++ b/src/sunstone/public/app/utils/dialogs/confirm/html.hbs @@ -0,0 +1,20 @@ +
+
+

+ {{tr "Confirm"}} +
+   + +

+
+
+
{{tr "You have to confirm this action."}}
+
+
{{tr "Do you want to proceed?"}}
+
+
+ +
+ × +
+
\ No newline at end of file diff --git a/src/sunstone/public/app/utils/tab-datatable.js b/src/sunstone/public/app/utils/tab-datatable.js index 2ac6a6b7cf..f23c5db54f 100644 --- a/src/sunstone/public/app/utils/tab-datatable.js +++ b/src/sunstone/public/app/utils/tab-datatable.js @@ -269,6 +269,24 @@ define(function(require) { $('tr input.check_item:visible', dataTable).replaceWith(SPINNER); } + //returns an array of ids of selected elements in a dataTable + var _getSelectedNodes = function(dataTable, forceDataTable){ + var selected_nodes = []; + if (dataTable){ + var tab = dataTable.parents(".tab") + if (Sunstone.rightInfoVisible(tab) && !forceDataTable) { + selected_nodes.push(Sunstone.rightInfoResourceId(tab)); + } else { + //Which rows of the datatable are checked? + var nodes = $('tbody input.check_item:checked',dataTable); + $.each(nodes,function(){ + selected_nodes.push($(this).val()); + }); + } + }; + return selected_nodes; + } + return { 'infoListener': _infoListener, 'addElement': _addElement, @@ -280,6 +298,7 @@ define(function(require) { 'updateView': _updateView, 'updateSingleElement': _updateSingleElement, 'getElementData': _getElementData, - 'waitingNodes': _waitingNodes + 'waitingNodes': _waitingNodes, + 'getSelectedNodes': _getSelectedNodes } })