source: extensions/AMenuManager/js/amm_links.js @ 8962

Last change on this file since 8962 was 8962, checked in by grum, 13 years ago

release 3.0.0
fix bug:1716, bug:2128, bug:1296, bug:2052, bug:1910, bug:1723, bug:1709, bug:1680, bug:1477, bug:2129
rewrite plugin core, implement ajax interface, optimize JS

  • Property svn:executable set to *
File size: 8.5 KB
Line 
1/**
2 * userLinksManage
3 *
4 * release 1.0.0
5 */
6function userLinksManage (opt, keys, token)
7{
8  var options = {
9          ajaxUrl:'plugins/AMenuManager/amm_ajax.php'
10        },
11      translatedKeys= {
12          g002_ok:'g002_ok',
13          g002_cancel:'g002_cancel',
14          g002_loading: 'g002_loading',
15          g002_editoflink : 'g002_editoflink',
16          g002_createoflink : 'g002_createoflink'
17        },
18      properties = {
19          id:'',
20          token:token
21        },
22
23  /**
24   * load links list
25   */
26  load = function ()
27    {
28      $("#iList table.littlefont").sortable('destroy');
29      $('#iList').html("<br>"+translatedKeys.g002_loading+"<br><img src='./plugins/GrumPluginClasses/icons/processing.gif'>");
30
31      $.ajax(
32        {
33          type: "POST",
34          url: options.ajaxUrl,
35          async: true,
36          data: { ajaxfct:"admin.links.list" },
37          success:
38            function(msg)
39            {
40              $("#iList").html(msg);
41              $('#iListOrderButtons').css("display", 'none');
42
43              $("#iList").sortable(
44                {
45                  connectWith: '.connectedSortable',
46                  axis: "y",
47                  cursor: 'move',
48                  opacity:0.6,
49                  items: 'li',
50                  tolerance:'pointer',
51                  update: function () {  $('#iListOrderButtons').css("display", 'block'); }
52                }
53              );
54            }
55        }
56      );
57    },
58
59  /**
60   * edit or create a new link
61   *
62   * @param String linkId : if empty, assume to create a new link
63   */
64  edit = function (linkId)
65    {
66      properties.id=linkId;
67
68      $('#iDialogEdit')
69        .dialog('option', 'title', (linkId=='')?translatedKeys.g002_createoflink:translatedKeys.g002_editoflink)
70        .dialog("open");
71    },
72
73  /**
74   * remove a link
75   *
76   * @param String linkId : link to remove
77   */
78  remove = function (linkId)
79    {
80      properties.id=linkId;
81
82      $.ajax(
83        {
84          type: "POST",
85          url: options.ajaxUrl,
86          async: true,
87          data: { ajaxfct:"admin.links.delete", id:properties.id, token:properties.token },
88          success:
89            function(msg)
90            {
91              load();
92            }
93        }
94      );
95    },
96
97  /**
98   * check validity of form
99   */
100  checkValidity = function ()
101    {
102      $('.error').removeClass('error');
103      ok=true;
104
105      if($('#iamm_label').inputText('value')=='')
106      {
107        $('#iamm_label').inputText('isValid', false);
108        ok=false;
109      }
110
111      if($('#iamm_url').inputText('value')=='')
112      {
113        $('#iamm_url').inputText('isValid', false);
114        ok=false;
115      }
116
117      return(ok);
118    },
119
120
121  /**
122   * update values of the dialog box
123   *
124   * @param String items : json string ; if empty assume to reset all fields
125   *                       with default values
126   */
127  updateDialog = function (items)
128    {
129      if(items=='')
130      {
131        $('#iamm_label').inputText('value', '');
132        $('#iamm_url').inputText('value', '');
133        $('#iamm_icon').inputList('value', ':first');
134        $('#iamm_mode').inputList('value', ':first');
135        $('#iamm_visible').inputRadio('value', 'y');
136        $('#iamm_access_users').inputCheckbox('value', ':all');
137        $('#iamm_access_groups').inputCheckbox('value', ':all');
138      }
139      else
140      {
141        tmp=$.parseJSON(items);
142
143        $('#iamm_label').inputText('value', tmp.label);
144        $('#iamm_url').inputText('value', tmp.url);
145        $('#iamm_icon').inputList('value', tmp.icon);
146        $('#iamm_mode').inputList('value', tmp.mode);
147        $('#iamm_visible').inputRadio('value', tmp.visible);
148        $('#iamm_access_users').inputCheckbox('value', '', tmp.accessUsers);
149        $('#iamm_access_users').inputCheckbox('value', ':invert');
150        $('#iamm_access_groups').inputCheckbox('value', '', tmp.accessGroups);
151        $('#iamm_access_groups').inputCheckbox('value', ':invert');
152      }
153    },
154
155  /**
156   * update order on server
157   */
158  doUpdateOrder = function ()
159    {
160      var datas={
161            links:[]
162          },
163          order=0;
164
165      $('#iList li.connectedSortable').each(
166        function ()
167        {
168          datas.links.push(
169            {
170              id:$(this).attr('linkId'),
171              order:order
172            }
173          );
174          order++;
175        }
176      );
177
178      $.ajax(
179        {
180          type: "POST",
181          url: options.ajaxUrl,
182          async: true,
183          data: { ajaxfct:"admin.links.order", token:properties.token, datas:datas },
184          success:
185            function(msg)
186            {
187              displayProcessing(false);
188
189              returned=msg.split('!');
190
191              if(returned[0]=='OK')
192              {
193                $('#iListOrderButtons').css("display", 'none');
194              }
195              else
196              {
197                $('#'+returned[0]).addClass('error');
198                alert(returned[1]);
199              }
200            }
201        }
202      );
203    },
204
205  /**
206   * update values on server
207   */
208  doUpdate = function ()
209    {
210      displayProcessing(true);
211
212      // build datas
213      var datas = {
214        label:$('#iamm_label').inputText('value'),
215        url:$('#iamm_url').inputText('value'),
216        icon:$('#iamm_icon').inputList('value'),
217        mode:$('#iamm_mode').inputList('value'),
218        visible:$('#iamm_visible').inputRadio('value'),
219        accessUsers:$('#iamm_access_users').inputCheckbox('value'),
220        accessGroups:$('#iamm_access_groups').inputCheckbox('value')
221      };
222
223      $.ajax(
224        {
225          type: "POST",
226          url: options.ajaxUrl,
227          async: true,
228          data: { ajaxfct:"admin.links.set", id:properties.id, token:properties.token, datas:datas },
229          success:
230            function(msg)
231            {
232              displayProcessing(false);
233
234              if(msg.match(/^[0-9]+$/i)!=null)
235              {
236                // result Ok ! => close the dialog box and reload the list
237                $('#iDialogEdit').dialog("close");
238                load();
239              }
240              else
241              {
242                returned=msg.split('!');
243                $('#'+returned[0]).addClass('error');
244                alert(returned[1]);
245              }
246            }
247        }
248      );
249    },
250
251  /**
252   * display or hide the processing flower
253   */
254  displayProcessing = function (visible)
255    {
256      if(visible)
257      {
258        $('#iBDProcessing').css("display", "block");
259      }
260      else
261      {
262        $('#iBDProcessing').css("display", "none");
263      }
264    },
265
266  /**
267   * initialize the object
268   */
269  init = function ()
270  {
271    var buttons={};
272
273    buttons[translatedKeys.g002_ok]=function()
274      {
275        if(checkValidity()) doUpdate();
276      };
277    buttons[translatedKeys.g002_cancel]=function()
278      {
279        $('#iDialogEdit').dialog("close");
280      };
281
282    $('#iDialogEdit')
283      .dialog(
284        {
285          autoOpen:false,
286          width:600,
287          height:400,
288          modal: true,
289          dialogClass: 'gcBgTabSheet gcBorder',
290          title: '',
291          buttons:buttons
292        }
293      )
294      .bind('dialogopen', function ()
295        {
296          if(properties.id!='')
297          {
298            displayProcessing(true);
299
300            $.ajax(
301              {
302                type: "POST",
303                url: options.ajaxUrl,
304                async: true,
305                data: { ajaxfct:"admin.links.get", id:properties.id },
306                success:
307                  function(msg)
308                  {
309                    updateDialog(msg);
310                    displayProcessing(false);
311                  }
312              }
313            );
314          }
315          else
316          {
317            updateDialog('');
318          }
319        }
320      );
321
322    $('#iamm_label').inputText(
323      {
324        displayChar:50,
325        maxChar:50
326      }
327    );
328
329    $('#iamm_url').inputText(
330      {
331        displayChar:50,
332        maxChar:255
333      }
334    );
335
336    $('#iamm_icon').inputList(
337      {
338        listMaxWidth:250,
339        colsWidth:[22,200],
340        colsCss:['iconColImg','iconColText'],
341        popupMode:'mouseout'
342      }
343    );
344
345    $('#iamm_mode').inputList({popupMode:'mouseout'});
346
347    $('#iamm_visible').inputRadio();
348
349    $('#iamm_access_users').inputCheckbox({returnMode:'notSelected'});
350    $('#iamm_access_groups').inputCheckbox({returnMode:'notSelected'});
351
352    load();
353  };
354
355  $.extend(options, opt);
356  $.extend(translatedKeys, keys);
357
358  this.load = function () { load(); };
359  this.edit = function (linkId) { edit(linkId); };
360  this.remove = function (linkId) { remove(linkId); };
361  this.doUpdateOrder = function () { doUpdateOrder(); };
362
363  init();
364}
365
366
Note: See TracBrowser for help on using the repository browser.