2 * MyPlugin: demonstration plugin
4 * Description: Template for writing new plugins and illustrating the different
5 * possibilities of the plugin API.
6 * This file is part of the Manifold project
7 * Requires: js/plugin.js
8 * URL: http://www.myslice.info
9 * Author: Jordan Augé <jordan.auge@lip6.fr>
10 * Copyright: Copyright 2012-2013 UPMC Sorbonne Universités
16 var SlaDialog = Plugin.extend({
21 * @brief Plugin constructor
22 * @param options : an associative array of setting values
24 * @return : a jQuery collection of objects on which the plugin is
25 * applied, which allows to maintain chainability of calls
27 init: function(options, element) {
28 // for debugging tools
29 this.classname="SlaDialog";
30 // Call the parent constructor, see FAQ when forgotten
31 this._super(options, element);
33 /* Member variables */
37 /* Setup query and record handlers */
39 // Explain this will allow query events to be handled
40 // What happens when we don't define some events ?
41 // Some can be less efficient
42 this.listen_query(options.query_uuid);
44 /* GUI setup and event binding */
46 this.button_binding();
50 find_row: function(key)
52 // key in third position, column id = 2
55 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
56 return (col._aData[KEY_POS] == key);
62 throw "Too many same-key rows in ResourceSelected plugin";
67 check_template_status: function() {
68 for (var testbed in this.accepted_slas) {
69 if (!this.accepted_slas[testbed]) { return false; }
76 // on_show like in querytable
81 button_binding: function() {
83 $(".sla-accept-button").click(function() {
84 // set SLA as accepted and remove warnings
85 var id = $(this).closest(".row").attr("id");
86 self.accepted_slas[id] = true;
87 var is_ok = self.check_template_status();
91 // var warnings = manifold.query_store.get_record_state(resource_query.query_uuid, resource_key, STATE_WARNINGS);
96 create_sla: function(record) {
101 record.resource.forEach(function(r) {
102 if ($.inArray(r.component_id, record.resource) == -1) { // if not already selected
103 urns.push(r.component_id);
107 if (record.resource.length != 0 && typeof record.resource[0] !== "undefined") {
109 "SLIVER_INFO_AGGREGATE_URN": record.resource[0].component_manager_id,
110 "SLIVER_INFO_EXPIRATION": record.lease[0].end_time, // FIXME: only working with leases
111 "SLIVER_INFO_SLICE_URN": record.slice_urn,
112 "SLIVER_INFO_CREATOR_URN": record.users[0],
113 "SLIVER_INFO_URN": urns,
114 "SLIVER_INFO_SLA_ID": self._getUUID()
119 $.post("/sla/agreements/create/", data)
120 .done(function (response) {
121 console.log("SLA created");
126 uncheck: function(urn)
128 // $('#slamodal').on('hidden.bs.modal', function(e){
129 // $('#' + (urn).replace(/"/g,'')).click();
130 // console.log('#' + (data.value).replace(/"/g,''));
134 // a function to bind events here: click change
135 // how to raise manifold events
136 set_state: function(data, username)
141 post_agreement: function()
143 console.log(this.options.user);
146 /* GUI MANIPULATION */
148 // We advise you to write function to change behaviour of the GUI
149 // Will use naming helpers to access content _inside_ the plugin
150 // always refer to these functions in the remaining of the code
152 show_hide_button: function()
154 // this.id, this.el, this.cl, this.elts
155 // same output as a jquery selector with some guarantees
160 // see in the html template
161 // How to load a template, use of mustache
165 // How to make sure the plugin is not desynchronized
166 // He should manifest its interest in filters, fields or records
167 // functions triggered only if the proper listen is done
171 on_filter_added: function(filter)
176 on_field_state_changed: function(data)
179 // this.set_state(result, this.options.username);
184 case STATE_SET_IN_PENDING:
185 if (typeof(data.value) == 'string') {
187 this._supports_sla(data.value)
188 .done( function(testbeds) {
189 var urn_regexp = /\+(.*?)\+/;
190 var urn = urn_regexp.exec(data.value)[1];
191 var pos = $.inArray(urn, testbeds);
193 var id_ref = testbeds[pos].replace(/\.|:/g, "-");
194 $("#" + id_ref).data("urns").push(data.value);
195 $("#" + id_ref).show();
196 self.accepted_slas[id_ref] = false;
197 //$( "#sla_offers_list" ).append(
198 // $("<li>").text("Testbed " + testbeds[pos] + " offers SLA for its resources")
206 if (typeof(data.value) == 'string') {
208 this._supports_sla(data.value)
209 .done( function(testbeds) {
210 var urn_regexp = /\+(.*?)\+/;
211 var urn = urn_regexp.exec(data.value)[1];
212 var pos = $.inArray(urn, testbeds);
214 var id_ref = testbeds[pos].replace(/\.|:/g, "-");
215 var array = $("#" + id_ref).data("urns");
216 array.splice(array.indexOf(data.value), 1);
218 if ($("#" + id_ref).data("urns").length == 0) {
219 $("#" + id_ref).hide();
220 delete self.accepted_slas[id_ref];
222 //$( "#sla_offers_list" ).append(
223 // $("<li>").text("Testbed " + testbeds[pos] + " offers SLA for its resources")
233 // Add resource to SLA
235 // data.value = {1: "SLA acceptance required..."}
236 // this.change_status(data.key, data.value);
241 // ... be sure to list all events here
243 /* RECORD HANDLERS */
244 on_all_new_record: function(record)
249 on_new_record: function(record)
251 this.create_sla(record);
254 /* INTERNAL FUNCTIONS */
256 // only convention, not strictly enforced at the moment
259 _supports_sla: function(resource_urn) {
260 return $.ajax("/sla/testbeds/");
265 _getUUID: function() {
266 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
267 var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
268 return v.toString(16);
274 /* Plugin registration */
275 $.plugin('SlaDialog', SlaDialog);
277 // TODO Here use cases for instanciating plugins in different ways like in the pastie.