linksdirectwidget.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // jQuery UI style "widget" for managing 1:n links "in-place"
  2. $(function()
  3. {
  4. // the widget definition, where "itop" is the namespace,
  5. // "directlinks" the widget name
  6. $.widget( "itop.directlinks",
  7. {
  8. // default options
  9. options:
  10. {
  11. input_name: '',
  12. class_name: '',
  13. att_code: '',
  14. submit_to: '../pages/ajax.render.php',
  15. submit_parameters: {},
  16. labels: { 'delete': 'Delete',
  17. modify: 'Modify...' ,
  18. creation_title: 'Creation of a new object...' ,
  19. create: 'Create...'
  20. }
  21. },
  22. // the constructor
  23. _create: function()
  24. {
  25. var me = this;
  26. this.id = this.element.attr('id');
  27. this.element
  28. .addClass('itop-directlinks');
  29. this.datatable = this.element.find('table.listResults');
  30. this.deleteBtn = $('<button type="button">' + this.options.labels['delete'] + '</button>');
  31. this.modifyBtn = $('<button type="button">' + this.options.labels['modify'] + '</button>');
  32. this.createBtn = $('<button type="button">' + this.options.labels['create'] + '</button>');
  33. this.indicator = $('<span></span>');
  34. this.inputToBeCreated = $('<input type="hidden" name="'+this.options.input_name+'_tbc" value="{}">');
  35. this.toBeCreated = {};
  36. this.inputToBeDeleted = $('<input type="hidden" name="'+this.options.input_name+'_tbd" value="[]">');
  37. this.toBeDeleted = [];
  38. this.element
  39. .after(this.inputToBeCreated)
  40. .after(this.inputToBeDeleted)
  41. .after('<span style="float:left">&nbsp;&nbsp;&nbsp;<img src="../images/tv-item-last.gif">&nbsp;&nbsp;&nbsp;')
  42. .after(this.indicator).after(this.createBtn).after('&nbsp;&nbsp;&nbsp')
  43. .after(this.modifyBtn).after('&nbsp;&nbsp;&nbsp')
  44. .after(this.deleteBtn);
  45. this.element.find('.selectList'+this.id).bind('change', function() { me._updateButtons(); });
  46. this.deleteBtn.click(function() {
  47. $('.selectList'+me.id+':checked', me.element).each( function() { me._deleteRow($(this)); });
  48. });
  49. this.createBtn.click(function() {
  50. me._createRow();
  51. });
  52. this.modifyBtn.hide(); //hidden for now since it's not yet implemented
  53. this._updateButtons();
  54. },
  55. // called when created, and later when changing options
  56. _refresh: function()
  57. {
  58. this._updateButtons();
  59. },
  60. // events bound via _bind are removed automatically
  61. // revert other modifications here
  62. _destroy: function()
  63. {
  64. this.element
  65. .removeClass('itop-directlinks');
  66. },
  67. // _setOptions is called with a hash of all options that are changing
  68. _setOptions: function()
  69. {
  70. // in 1.9 would use _superApply
  71. this._superApply(arguments);
  72. },
  73. // _setOption is called for each individual option that is changing
  74. _setOption: function( key, value )
  75. {
  76. // in 1.9 would use _super
  77. this._superApply(arguments);
  78. if (key == 'fields') this._refresh();
  79. },
  80. _updateButtons: function()
  81. {
  82. var oChecked = $('.selectList'+this.id+':checked', this.element);
  83. switch(oChecked.length)
  84. {
  85. case 0:
  86. this.deleteBtn.attr('disabled', 'disabled');
  87. this.modifyBtn.attr('disabled', 'disabled');
  88. break;
  89. case 1:
  90. this.deleteBtn.removeAttr('disabled');
  91. this.modifyBtn.removeAttr('disabled');
  92. break;
  93. default:
  94. this.deleteBtn.removeAttr('disabled');
  95. this.modifyBtn.attr('disabled', 'disabled');
  96. break;
  97. }
  98. },
  99. _updateTable: function()
  100. {
  101. var me = this;
  102. this.datatable.trigger("update").trigger("applyWidgets");
  103. this.datatable.tableHover();
  104. this.datatable.find('.selectList'+this.id).bind('change', function() { me._updateButtons(); });
  105. },
  106. _updateDlgSize: function()
  107. {
  108. this.oDlg.dialog('option', { position: { my: "center", at: "center", of: window }});
  109. },
  110. _createRow: function()
  111. {
  112. this.createBtn.attr('disabled', 'disabled');
  113. this.indicator.html('<img src="../images/indicator.gif">');
  114. oParams = this.options.submit_parameters;
  115. oParams.operation = 'createObject';
  116. oParams['class'] = this.options.class_name;
  117. oParams.real_class = '';
  118. oParams.att_code = this.options.att_code;
  119. oParams.iInputId = this.id;
  120. var me = this;
  121. $.post(this.options.submit_to, oParams, function(data){
  122. me.oDlg = $('<div></div>');
  123. $('body').append(me.oDlg);
  124. me.oDlg.html(data);
  125. me.oDlg.find('form').removeAttr('onsubmit').bind('submit', function() { me._onCreateRow(); return false; } );
  126. me.oDlg.find('button.cancel').unbind('click').click( function() { me.oDlg.dialog('close'); } );
  127. me.oDlg.dialog({
  128. title: me.options.labels['creation_title'],
  129. modal: true,
  130. width: 'auto',
  131. height: 'auto',
  132. position: { my: "center", at: "center", of: window },
  133. close: function() { me._onDlgClose(); }
  134. });
  135. me.indicator.html('');
  136. me.createBtn.removeAttr('disabled');
  137. me._updateDlgSize();
  138. });
  139. },
  140. subclassSelected: function()
  141. {
  142. var sRealClass = this.oDlg.find('select[name="class"]').val();
  143. oParams = this.options.submit_parameters;
  144. oParams.operation = 'createObject';
  145. oParams['class'] = this.options.class_name;
  146. oParams.real_class = sRealClass;
  147. oParams.att_code = this.options.att_code;
  148. oParams.iInputId = this.id;
  149. var me = this;
  150. me.oDlg.find('button').attr('disabled', 'disabled');
  151. me.oDlg.find('span.indicator').html('<img src="../images/indicator.gif">');
  152. $.post(this.options.submit_to, oParams, function(data){
  153. me.oDlg.html(data);
  154. me.oDlg.find('form').removeAttr('onsubmit').bind('submit', function() { me._onCreateRow(); return false; } );
  155. me.oDlg.find('button.cancel').unbind('click').click( function() { me.oDlg.dialog('close'); } );
  156. me._updateDlgSize();
  157. });
  158. },
  159. _onCreateRow: function()
  160. {
  161. // Validate the form
  162. var sFormId = this.oDlg.find('form').attr('id');
  163. if (CheckFields(sFormId, true))
  164. {
  165. // Gather the values from the form
  166. oParams = this.options.submit_parameters;
  167. var oValues = {};
  168. this.oDlg.find(':input').each( function() {
  169. if (this.name != '')
  170. {
  171. oParams[this.name] = this.value;
  172. oValues[this.name] = this.value;
  173. }
  174. });
  175. var nextIdx = 0;
  176. for(k in this.toBeCreated)
  177. {
  178. nextIdx++;
  179. }
  180. nextIdx++;
  181. this.toBeCreated[nextIdx] = oValues;
  182. this.inputToBeCreated.val(JSON.stringify(this.toBeCreated));
  183. this.oDlg.dialog('close');
  184. oParams = this.options.submit_parameters;
  185. oParams.operation = 'getLinksetRow';
  186. oParams['class'] = this.options.class_name;
  187. oParams.att_code = this.options.att_code;
  188. oParams.iInputId = this.id;
  189. oParams.tempId = nextIdx;
  190. var me = this;
  191. this.createBtn.attr('disabled', 'disabled');
  192. this.indicator.html('<img src="../images/indicator.gif">');
  193. $.post(this.options.submit_to, oParams, function(data){
  194. me.datatable.find('tbody').append(data);
  195. me._updateTable();
  196. me.indicator.html('');
  197. me.createBtn.removeAttr('disabled');
  198. });
  199. }
  200. },
  201. _onDlgClose: function()
  202. {
  203. this.oDlg.remove();
  204. this.oDlg = null;
  205. },
  206. _deleteRow: function(oCheckbox)
  207. {
  208. var iObjKey = parseInt(oCheckbox.val(), 10); // Number in base 10
  209. if (iObjKey > 0)
  210. {
  211. // Existing objet: add it to the "to be deleted" list
  212. this.toBeDeleted.push(iObjKey);
  213. this.inputToBeDeleted.val(JSON.stringify(this.toBeDeleted));
  214. }
  215. else
  216. {
  217. // Object to be created, just remove it from the "to be created" list
  218. this.toBeCreated[-iObjKey] = undefined;
  219. this.inputToBeCreated.val(JSON.stringify(this.toBeCreated));
  220. }
  221. // Now remove the row from the table
  222. oRow = oCheckbox.closest('tr');
  223. oRow.remove();
  224. this._updateButtons();
  225. this._updateTable();
  226. }
  227. });
  228. });