X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plugins%2Fsladialog%2Fstatic%2Fjs%2Fsladialog.js;h=01eb0afd3f87b3628684b8ce3aeb6931bc5ab44d;hb=deee82377a1f72626cede01fdfec5e9d7cc274ce;hp=89d1ab0da12e8bb2ad0add77130c631cf6fb1863;hpb=f56d47e6dcc591f2c6008205c531ece8eefaccf7;p=myslice.git diff --git a/plugins/sladialog/static/js/sladialog.js b/plugins/sladialog/static/js/sladialog.js index 89d1ab0d..01eb0afd 100644 --- a/plugins/sladialog/static/js/sladialog.js +++ b/plugins/sladialog/static/js/sladialog.js @@ -1,13 +1,10 @@ /** - * MyPlugin: demonstration plugin - * Version: 0.1 - * Description: Template for writing new plugins and illustrating the different - * possibilities of the plugin API. - * This file is part of the Manifold project + * SlaDialog + * Description: Plugin to allow SLA acceptance and creation to MySlice portal + * in Fed4FIRE * Requires: js/plugin.js - * URL: http://www.myslice.info - * Author: Jordan Augé - * Copyright: Copyright 2012-2013 UPMC Sorbonne Universités + * Author: Javier García Lloreda + * Copyright: Copyright Atos Spain S.A. * License: GPLv3 */ @@ -15,17 +12,11 @@ var SlaDialog = Plugin.extend({ - /** XXX to check - * @brief Plugin constructor - * @param options : an associative array of setting values - * @param element : - * @return : a jQuery collection of objects on which the plugin is - * applied, which allows to maintain chainability of calls - */ + accepted_slas: {}, + queries: [], + init: function(options, element) { - // for debugging tools - this.classname="SlaDialog"; - // Call the parent constructor, see FAQ when forgotten + this.classname = "SlaDialog"; this._super(options, element); /* Member variables */ @@ -33,154 +24,218 @@ /* Plugin events */ /* Setup query and record handlers */ - - // Explain this will allow query events to be handled - // What happens when we don't define some events ? - // Some can be less efficient this.listen_query(options.query_uuid); /* GUI setup and event binding */ - // call function + // Get testbeds with sla and store them in localStorage + this.get_testbeds_with_sla(); + // call function + //this.button_binding(); }, - find_row: function(key) - { - // key in third position, column id = 2 - var KEY_POS = 2; + get_testbeds_with_sla: function () { + var self = this; + return $.get('/sla/testbeds/', function(data) { + if (typeof(Storage) !== "undefined") { + if (!localStorage.getItem("sla_testbeds")) { + var testbeds = data; + localStorage.setItem("sla_testbeds", testbeds); + } + } + }).done(function(data) { + self.get_sla_templates(data); + }); + }, - var cols = $.grep(this.table.fnSettings().aoData, function(col) { - return (col._aData[KEY_POS] == key); - } ); + get_sla_templates: function (testbeds) { + var self = this; + + testbeds.forEach(function(testbed, index, array) { + + if(testbed=="omf:netmode") { // TODO: Remove + + $.ajax('/sla/agreements/templates/' + testbed) + .always(function(data) { + $(".modal-body #sla_template").html(data.responseText.replace(/<|>/g, "")); + + var content = + ""; + + $("#sla_offers").append(content); + + self.button_binding(); + }); + } + }); + }, - if (cols.length == 0) - return null; - if (cols.length > 1) - throw "Too many same-key rows in ResourceSelected plugin"; + check_template_status: function() { + for (var testbed in this.accepted_slas) { + if (!this.accepted_slas[testbed]) { return false; } + } - return cols[0]; + return true; }, /* PLUGIN EVENTS */ - // on_show like in querytable - /* GUI EVENTS */ - uncheck: function(urn) - { - $('#slamodal').on('hidden.bs.modal', function(e){ - $('#' + (urn).replace(/"/g,'')).click(); - console.log('#' + (data.value).replace(/"/g,'')); + button_binding: function() { + var self = this; + $(".sla-accept-button").click(function() { + // set SLA as accepted and remove warnings + var id = $(this).closest(".row").attr("id"); + self.accepted_slas[id] = true; + var is_ok = self.check_template_status(); + + $(".sla-alert").show(); + $(this).button("complete"); + $(this).prop("disabled", true); + + if (is_ok) { + // remove warnings + $('#' + id).data("urns").forEach(function (urn, index, array) { + data = { + state: STATE_SET, + key : "resource", + op : STATE_SET_REMOVE, + value: urn + } + + manifold.raise_event(self.query_uuid, STATUS_REMOVE_WARNING, data); + }); + } + }); + + $(".sla-alert-close").click(function() { + $(this).closest(".sla-alert").hide(); }); }, - // a function to bind events here: click change - // how to raise manifold events - set_state: function(data, username) - { - var action; - var msg; - var button = ''; - var username = username; - - var uncheck = false; - - switch(data.request) { - case FIELD_REQUEST_ADD_RESET: - case FIELD_REQUEST_REMOVE_RESET: - $('#sla_dialog').hide(); - // find line and delete it - // row = this.find_row(data.value); - // if (row) - // this.table.fnDeleteRow(row.nTr); - // $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 ); - // $("#badge-pending").text($("#badge-pending").data('number')); - return; - case FIELD_REQUEST_CHANGE: - action = 'UPDATE'; - break; - case FIELD_REQUEST_ADD: - action = 'ADD'; - - if (data.value.toLowerCase().indexOf("iminds") >= 0){ - - $('#sla_dialog').show(); - $('#slamodal').modal('show'); - - $(document).ready(function() { - $("#accept_sla").click(function(){ - console.log("SLA ACCEPTED"); - console.log("With username: " + username); - $.post("/sla/agreements/simplecreate", {"template_id":"iMindsServiceTemplate","user":username}); - $('#slamodal').modal('hide'); - }); - }); - - $(document).ready(function() { - $("#dismiss_sla").click(function(){ - console.log("SLA NOT ACCEPTED"); - // FIX ME: This is not a good solution to prevent the checkbox click - var chkbox = document.getElementById((data.value).replace(/"/g,'')); - if(chkbox.checked){ - chkbox.click(); - } - $('#slamodal').modal('hide'); - - }); - }); + create_sla: function(record) { + var self = this; - } + var urns = []; - break; - case FIELD_REQUEST_REMOVE: - action = 'REMOVE'; - break; - } - }, + if (record.resource.length != 0 && typeof record.resource[0] === "object") { - post_agreement: function() - { - console.log(this.options.user); + record.resource.forEach(function(r) { + if ($.inArray(r.component_id, record.resource) == -1) { // if not already selected + urns.push(r.component_id); + } + }); + + var data = { + "SLIVER_INFO_AGGREGATE_URN": record.resource[0].component_manager_id, + "SLIVER_INFO_EXPIRATION": record.lease[0].end_time, // FIXME: only working with leases + "SLIVER_INFO_SLICE_URN": record.slice_urn, + "SLIVER_INFO_CREATOR_URN": record.users[0], + "SLIVER_INFO_URN": urns, + "SLIVER_INFO_SLA_ID": self._getUUID() + }; + + console.log(data); + + $.post("/sla/agreements/create/", data) + .done(function (response) { + console.log("SLA created"); + }); + } }, - /* GUI MANIPULATION */ - - // We advise you to write function to change behaviour of the GUI - // Will use naming helpers to access content _inside_ the plugin - // always refer to these functions in the remaining of the code - - show_hide_button: function() + uncheck: function(urn) { - // this.id, this.el, this.cl, this.elts - // same output as a jquery selector with some guarantees +// $('#slamodal').on('hidden.bs.modal', function(e){ +// $('#' + (urn).replace(/"/g,'')).click(); +// console.log('#' + (data.value).replace(/"/g,'')); +// }); }, - /* TEMPLATES */ - - // see in the html template - // How to load a template, use of mustache - - /* QUERY HANDLERS */ - - // How to make sure the plugin is not desynchronized - // He should manifest its interest in filters, fields or records - // functions triggered only if the proper listen is done - - // no prefix - on_filter_added: function(filter) { }, - on_field_state_changed: function(result) + on_field_state_changed: function(data) { - console.log("triggered state_changed: "+result); + var self = this; + self.query_uuid = self.options.query_uuid; + + var testbeds = localStorage.getItem("sla_testbeds").split(","); + var urn_regexp = /\+(.*?)\+/; // this.set_state(result, this.options.username); - }, - // ... be sure to list all events here + switch(data.state) { + case STATE_SET: + switch(data.op) { + case STATE_SET_IN_PENDING: + if (typeof(data.value) == 'string') { + // data.value = urn + var urn = urn_regexp.exec(data.value)[1]; + var pos = $.inArray(urn, testbeds); + if ( pos != -1) { + var id_ref = testbeds[pos].replace(/\.|:/g, "-"); + $("#" + id_ref).data("urns").push(data.value); + $("#" + id_ref).show(); + self.accepted_slas[id_ref] = false; + //$( "#sla_offers_list" ).append( + // $("
  • ").text("Testbed " + testbeds[pos] + " offers SLA for its resources") + //); + } + } + break; + case STATE_SET_OUT: + // data.value = urn + if (typeof(data.value) == 'string') { + // data.value = urn + var urn = urn_regexp.exec(data.value)[1]; + var pos = $.inArray(urn, testbeds); + if ( pos != -1) { + var id_ref = testbeds[pos].replace(/\.|:/g, "-"); + var array = $("#" + id_ref).data("urns"); + array.splice(array.indexOf(data.value), 1); + + if ($("#" + id_ref).data("urns").length == 0) { + $("#" + id_ref).hide(); + delete self.accepted_slas[id_ref]; + $(".sla-accept-button").button("reset"); + $(".sla-accept-button").prop("disabled", false); + } + //$( "#sla_offers_list" ).append( + // $("
  • ").text("Testbed " + testbeds[pos] + " offers SLA for its resources") + //); + } + } + break; + } + break; + + case STATE_WARNINGS: + // Add resource to SLA + // data.key = urn + // data.value = {1: "SLA acceptance required..."} + // this.change_status(data.key, data.value); + break; + } + }, /* RECORD HANDLERS */ on_all_new_record: function(record) @@ -190,12 +245,20 @@ on_new_record: function(record) { - + this.create_sla(record); }, /* INTERNAL FUNCTIONS */ - _dummy: function() { - // only convention, not strictly enforced at the moment + + _supports_sla: function(resource_urn) { + return $.ajax("/sla/testbeds/"); + }, + + _getUUID: function() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8); + return v.toString(16); + }); }, }); @@ -203,6 +266,4 @@ /* Plugin registration */ $.plugin('SlaDialog', SlaDialog); - // TODO Here use cases for instanciating plugins in different ways like in the pastie. - })(jQuery);