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 if (record.resource.length != 0 && typeof record.resource[0] === "object") {
103 record.resource.forEach(function(r) {
104 if ($.inArray(r.component_id, record.resource) == -1) { // if not already selected
105 urns.push(r.component_id);
110 "SLIVER_INFO_AGGREGATE_URN": record.resource[0].component_manager_id,
111 "SLIVER_INFO_EXPIRATION": record.lease[0].end_time, // FIXME: only working with leases
112 "SLIVER_INFO_SLICE_URN": record.slice_urn,
113 "SLIVER_INFO_CREATOR_URN": record.users[0],
114 "SLIVER_INFO_URN": urns,
115 "SLIVER_INFO_SLA_ID": self._getUUID()
120 $.post("/sla/agreements/create/", data)
121 .done(function (response) {
122 console.log("SLA created");
127 uncheck: function(urn)
129 // $('#slamodal').on('hidden.bs.modal', function(e){
130 // $('#' + (urn).replace(/"/g,'')).click();
131 // console.log('#' + (data.value).replace(/"/g,''));
135 // a function to bind events here: click change
136 // how to raise manifold events
137 set_state: function(data, username)
142 post_agreement: function()
144 console.log(this.options.user);
147 /* GUI MANIPULATION */
149 // We advise you to write function to change behaviour of the GUI
150 // Will use naming helpers to access content _inside_ the plugin
151 // always refer to these functions in the remaining of the code
153 show_hide_button: function()
155 // this.id, this.el, this.cl, this.elts
156 // same output as a jquery selector with some guarantees
161 // see in the html template
162 // How to load a template, use of mustache
166 // How to make sure the plugin is not desynchronized
167 // He should manifest its interest in filters, fields or records
168 // functions triggered only if the proper listen is done
172 on_filter_added: function(filter)
177 on_field_state_changed: function(data)
180 // this.set_state(result, this.options.username);
185 case STATE_SET_IN_PENDING:
186 if (typeof(data.value) == 'string') {
188 this._supports_sla(data.value)
189 .done( function(testbeds) {
190 var urn_regexp = /\+(.*?)\+/;
191 var urn = urn_regexp.exec(data.value)[1];
192 var pos = $.inArray(urn, testbeds);
194 var id_ref = testbeds[pos].replace(/\.|:/g, "-");
195 $("#" + id_ref).data("urns").push(data.value);
196 $("#" + id_ref).show();
197 self.accepted_slas[id_ref] = false;
198 //$( "#sla_offers_list" ).append(
199 // $("<li>").text("Testbed " + testbeds[pos] + " offers SLA for its resources")
207 if (typeof(data.value) == 'string') {
209 this._supports_sla(data.value)
210 .done( function(testbeds) {
211 var urn_regexp = /\+(.*?)\+/;
212 var urn = urn_regexp.exec(data.value)[1];
213 var pos = $.inArray(urn, testbeds);
215 var id_ref = testbeds[pos].replace(/\.|:/g, "-");
216 var array = $("#" + id_ref).data("urns");
217 array.splice(array.indexOf(data.value), 1);
219 if ($("#" + id_ref).data("urns").length == 0) {
220 $("#" + id_ref).hide();
221 delete self.accepted_slas[id_ref];
223 //$( "#sla_offers_list" ).append(
224 // $("<li>").text("Testbed " + testbeds[pos] + " offers SLA for its resources")
234 // Add resource to SLA
236 // data.value = {1: "SLA acceptance required..."}
237 // this.change_status(data.key, data.value);
242 // ... be sure to list all events here
244 /* RECORD HANDLERS */
245 on_all_new_record: function(record)
250 on_new_record: function(record)
252 this.create_sla(record);
255 /* INTERNAL FUNCTIONS */
257 // only convention, not strictly enforced at the moment
260 _supports_sla: function(resource_urn) {
261 return $.ajax("/sla/testbeds/");
266 _getUUID: function() {
267 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
268 var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
269 return v.toString(16);
275 /* Plugin registration */
276 $.plugin('SlaDialog', SlaDialog);
278 // TODO Here use cases for instanciating plugins in different ways like in the pastie.