linksdirectwidget.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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: GetAbsoluteUrlAppRoot()+'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. // call the original destroy method since we overwrote it
  67. $.Widget.prototype.destroy.call( this );
  68. },
  69. // _setOptions is called with a hash of all options that are changing
  70. _setOptions: function()
  71. {
  72. // in 1.9 would use _superApply
  73. $.Widget.prototype._setOptions.apply( this, arguments );
  74. },
  75. // _setOption is called for each individual option that is changing
  76. _setOption: function( key, value )
  77. {
  78. // in 1.9 would use _super
  79. $.Widget.prototype._setOption.call( this, key, value );
  80. if (key == 'fields') this._refresh();
  81. },
  82. _updateButtons: function()
  83. {
  84. var oChecked = $('.selectList'+this.id+':checked', this.element);
  85. switch(oChecked.length)
  86. {
  87. case 0:
  88. this.deleteBtn.attr('disabled', 'disabled');
  89. this.modifyBtn.attr('disabled', 'disabled');
  90. break;
  91. case 1:
  92. this.deleteBtn.removeAttr('disabled');
  93. this.modifyBtn.removeAttr('disabled');
  94. break;
  95. default:
  96. this.deleteBtn.removeAttr('disabled');
  97. this.modifyBtn.attr('disabled', 'disabled');
  98. break;
  99. }
  100. },
  101. _updateTable: function()
  102. {
  103. var me = this;
  104. this.datatable.trigger("update").trigger("applyWidgets");
  105. this.datatable.tableHover();
  106. this.datatable.find('.selectList'+this.id).bind('change', function() { me._updateButtons(); });
  107. },
  108. _updateDlgSize: function()
  109. {
  110. this.oDlg.dialog('option', { position: { my: "center", at: "center", of: window }});
  111. },
  112. _createRow: function()
  113. {
  114. this.createBtn.attr('disabled', 'disabled');
  115. this.indicator.html('<img src="../images/indicator.gif">');
  116. oParams = this.options.submit_parameters;
  117. oParams.operation = 'createObject';
  118. oParams['class'] = this.options.class_name;
  119. oParams.real_class = '';
  120. oParams.att_code = this.options.att_code;
  121. oParams.iInputId = this.id;
  122. var me = this;
  123. $.post(this.options.submit_to, oParams, function(data){
  124. me.oDlg = $('<div></div>');
  125. $('body').append(me.oDlg);
  126. me.oDlg.html(data);
  127. me.oDlg.find('form').removeAttr('onsubmit').bind('submit', function() { me._onCreateRow(); return false; } );
  128. me.oDlg.find('button.cancel').unbind('click').click( function() { me.oDlg.dialog('close'); } );
  129. me.oDlg.dialog({
  130. title: me.options.labels['creation_title'],
  131. modal: true,
  132. width: 'auto',
  133. height: 'auto',
  134. position: { my: "center", at: "center", of: window },
  135. close: function() { me._onDlgClose(); }
  136. });
  137. me.indicator.html('');
  138. me.createBtn.removeAttr('disabled');
  139. me._updateDlgSize();
  140. });
  141. },
  142. subclassSelected: function()
  143. {
  144. var sRealClass = this.oDlg.find('select[name="class"]').val();
  145. oParams = this.options.submit_parameters;
  146. oParams.operation = 'createObject';
  147. oParams['class'] = this.options.class_name;
  148. oParams.real_class = sRealClass;
  149. oParams.att_code = this.options.att_code;
  150. oParams.iInputId = this.id;
  151. var me = this;
  152. me.oDlg.find('button').attr('disabled', 'disabled');
  153. me.oDlg.find('span.indicator').html('<img src="../images/indicator.gif">');
  154. $.post(this.options.submit_to, oParams, function(data){
  155. me.oDlg.html(data);
  156. me.oDlg.find('form').removeAttr('onsubmit').bind('submit', function() { me._onCreateRow(); return false; } );
  157. me.oDlg.find('button.cancel').unbind('click').click( function() { me.oDlg.dialog('close'); } );
  158. me._updateDlgSize();
  159. });
  160. },
  161. _onCreateRow: function()
  162. {
  163. // Validate the form
  164. var sFormId = this.oDlg.find('form').attr('id');
  165. if (CheckFields(sFormId, true))
  166. {
  167. // Gather the values from the form
  168. oParams = this.options.submit_parameters;
  169. var oValues = {};
  170. this.oDlg.find(':input').each( function() {
  171. if (this.name != '')
  172. {
  173. oParams[this.name] = this.value;
  174. oValues[this.name] = this.value;
  175. }
  176. });
  177. var nextIdx = 0;
  178. for(k in this.toBeCreated)
  179. {
  180. nextIdx++;
  181. }
  182. nextIdx++;
  183. this.toBeCreated[nextIdx] = oValues;
  184. this.inputToBeCreated.val(JSON.stringify(this.toBeCreated));
  185. this.oDlg.dialog('close');
  186. oParams = this.options.submit_parameters;
  187. oParams.operation = 'getLinksetRow';
  188. oParams['class'] = this.options.class_name;
  189. oParams.att_code = this.options.att_code;
  190. oParams.iInputId = this.id;
  191. oParams.tempId = nextIdx;
  192. var me = this;
  193. this.createBtn.attr('disabled', 'disabled');
  194. this.indicator.html('<img src="../images/indicator.gif">');
  195. $.post(this.options.submit_to, oParams, function(data){
  196. me.datatable.find('tbody').append(data);
  197. me._updateTable();
  198. me.indicator.html('');
  199. me.createBtn.removeAttr('disabled');
  200. });
  201. }
  202. },
  203. _onDlgClose: function()
  204. {
  205. this.oDlg.remove();
  206. this.oDlg = null;
  207. },
  208. _deleteRow: function(oCheckbox)
  209. {
  210. var iObjKey = parseInt(oCheckbox.val(), 10); // Number in base 10
  211. if (iObjKey > 0)
  212. {
  213. // Existing objet: add it to the "to be deleted" list
  214. this.toBeDeleted.push(iObjKey);
  215. this.inputToBeDeleted.val(JSON.stringify(this.toBeDeleted));
  216. }
  217. else
  218. {
  219. // Object to be created, just remove it from the "to be created" list
  220. this.toBeCreated[-iObjKey] = undefined;
  221. this.inputToBeCreated.val(JSON.stringify(this.toBeCreated));
  222. }
  223. // Now remove the row from the table
  224. oRow = oCheckbox.closest('tr');
  225. oRow.remove();
  226. this._updateButtons();
  227. this._updateTable();
  228. }
  229. });
  230. });