Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[myslice.git] / plugins / sladialog / static / js / sladialog.js
1 /**
2  * MyPlugin:    demonstration plugin
3  * Version:     0.1
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
11  * License:     GPLv3
12  */
13
14 (function($){
15
16     var SlaDialog = Plugin.extend({
17
18         /** XXX to check
19          * @brief Plugin constructor
20          * @param options : an associative array of setting values
21          * @param element : 
22          * @return : a jQuery collection of objects on which the plugin is
23          *     applied, which allows to maintain chainability of calls
24          */
25         init: function(options, element) {
26             // for debugging tools
27             this.classname="SlaDialog";
28             // Call the parent constructor, see FAQ when forgotten
29             this._super(options, element);
30
31             /* Member variables */
32
33             /* Plugin events */
34
35             /* Setup query and record handlers */
36
37             // Explain this will allow query events to be handled
38             // What happens when we don't define some events ?
39             // Some can be less efficient
40             this.listen_query(options.query_uuid);
41             
42             /* GUI setup and event binding */
43             // call function
44
45         },
46
47         find_row: function(key)
48         {
49             // key in third position, column id = 2
50             var KEY_POS = 2;
51
52             var cols = $.grep(this.table.fnSettings().aoData, function(col) {
53                 return (col._aData[KEY_POS] == key);
54             } );
55
56             if (cols.length == 0)
57                 return null;
58             if (cols.length > 1)
59                 throw "Too many same-key rows in ResourceSelected plugin";
60
61             return cols[0];
62         },
63
64         /* PLUGIN EVENTS */
65         // on_show like in querytable
66
67
68         /* GUI EVENTS */
69
70         uncheck: function(urn)
71         {
72             $('#slamodal').on('hidden.bs.modal', function(e){
73                 $('#' + (urn).replace(/"/g,'')).click();
74                 console.log('#' + (data.value).replace(/"/g,''));
75             });
76         },
77
78         // a function to bind events here: click change
79         // how to raise manifold events
80         set_state: function(data, username)
81         {
82             var action;
83             var msg;
84             var button = '';
85             var username = username;
86
87             var uncheck = false;
88
89             switch(data.request) {
90                 case FIELD_REQUEST_ADD_RESET:
91                 case FIELD_REQUEST_REMOVE_RESET:
92                     $('#sla_dialog').hide();
93                     // find line and delete it                    
94                     // row = this.find_row(data.value);
95                     // if (row)
96                     //     this.table.fnDeleteRow(row.nTr);
97                         // $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
98                         // $("#badge-pending").text($("#badge-pending").data('number'));
99                     return;
100                 case FIELD_REQUEST_CHANGE:
101                     action = 'UPDATE';
102                     break;
103                 case FIELD_REQUEST_ADD:
104                     action = 'ADD';
105
106                     if (data.value.toLowerCase().indexOf("iminds") >= 0){
107
108                         $('#sla_dialog').show();
109                         $('#slamodal').modal('show');
110                         
111                         $(document).ready(function() {
112                             $("#accept_sla").click(function(){
113                                 console.log("SLA ACCEPTED");
114                                 console.log("With username: " + username);
115                                 $.post("/sla/agreements/simplecreate", {"template_id":"iMindsServiceTemplate","user":username});
116                                 $('#slamodal').modal('hide');
117                             }); 
118                         });
119
120                         $(document).ready(function() {
121                             $("#dismiss_sla").click(function(){
122                                 console.log("SLA NOT ACCEPTED");
123                                 // FIX ME: This is not a good solution to prevent the checkbox click
124                                 var chkbox = document.getElementById((data.value).replace(/"/g,''));
125                                 if(chkbox.checked){
126                                     chkbox.click();
127                                 }
128                                 $('#slamodal').modal('hide');
129                                 
130                             }); 
131                         });
132
133                     }
134
135                     break;
136                 case FIELD_REQUEST_REMOVE:
137                     action = 'REMOVE';
138                     break;
139             }
140         },
141
142         post_agreement: function()
143         {
144             console.log(this.options.user);
145         },
146
147         /* GUI MANIPULATION */
148
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
152
153         show_hide_button: function() 
154         {
155             // this.id, this.el, this.cl, this.elts
156             // same output as a jquery selector with some guarantees
157         },
158
159         /* TEMPLATES */
160
161         // see in the html template
162         // How to load a template, use of mustache
163
164         /* QUERY HANDLERS */
165
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
169
170         // no prefix
171
172         on_filter_added: function(filter)
173         {
174
175         },
176
177         on_field_state_changed: function(result)
178         {
179             console.log("triggered state_changed: "+result);
180             // this.set_state(result, this.options.username);
181         }, 
182
183         // ... be sure to list all events here
184
185         /* RECORD HANDLERS */
186         on_all_new_record: function(record)
187         {
188             //
189         },
190
191         on_new_record: function(record)
192         {
193
194         },
195
196         /* INTERNAL FUNCTIONS */
197         _dummy: function() {
198             // only convention, not strictly enforced at the moment
199         },
200
201     });
202
203     /* Plugin registration */
204     $.plugin('SlaDialog', SlaDialog);
205
206     // TODO Here use cases for instanciating plugins in different ways like in the pastie.
207
208 })(jQuery);