field_set.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //iTop Form handler
  2. ;
  3. $(function()
  4. {
  5. // the widget definition, where 'itop' is the namespace,
  6. // 'form_handler' the widget name
  7. $.widget( 'itop.field_set',
  8. {
  9. // default options
  10. options:
  11. {
  12. field_identifier_attr: 'data-field-id', // convention: fields are rendered into a div and are identified by this attribute
  13. fields_list: null,
  14. fields_impacts: {},
  15. touched_fields: [],
  16. is_valid: true,
  17. form_path: '',
  18. script_element: null,
  19. style_element: null
  20. },
  21. buildData:
  22. {
  23. script_code: '',
  24. style_code: ''
  25. },
  26. // the constructor
  27. _create: function()
  28. {
  29. var me = this;
  30. this.element
  31. .addClass('field_set');
  32. this.element
  33. .bind('field_change', function(event, data){
  34. console.log('field_set: field_change');
  35. me._onFieldChange(event, data);
  36. })
  37. .bind('update_form', function(event, data){
  38. console.log('field_set: update_form');
  39. me._onUpdateForm(event, data);
  40. })
  41. .bind('get_current_values', function(event, data){
  42. console.log('field_set: get_current_values');
  43. return me._onGetCurrentValues(event, data);
  44. })
  45. .bind('validate', function(event, data){
  46. if (data === undefined)
  47. {
  48. data = {};
  49. }
  50. console.log('field_set: validate');
  51. return me._onValidate(event, data);
  52. });
  53. // Creating DOM elements if not using user's specifics
  54. if(this.options.script_element === null)
  55. {
  56. this.options.script_element = $('<script type="text/javascript"></script>');
  57. this.element.after(this.options.script_element);
  58. }
  59. if(this.options.style_element === null)
  60. {
  61. this.options.style_element = $('<style></style>');
  62. this.element.before(this.options.style_element);
  63. }
  64. // Building the form
  65. if(this.options.fields_list !== null)
  66. {
  67. this.buildForm();
  68. }
  69. },
  70. // called when created, and later when changing options
  71. _refresh: function()
  72. {
  73. },
  74. // events bound via _bind are removed automatically
  75. // revert other modifications here
  76. _destroy: function()
  77. {
  78. this.element
  79. .removeClass('field_set');
  80. },
  81. // _setOptions is called with a hash of all options that are changing
  82. // always refresh when changing options
  83. _setOptions: function()
  84. {
  85. this._superApply(arguments);
  86. },
  87. // _setOption is called for each individual option that is changing
  88. _setOption: function( key, value )
  89. {
  90. this._super( key, value );
  91. },
  92. _getField: function (sFieldId)
  93. {
  94. return this.element.find('[' + this.options.field_identifier_attr + '="'+sFieldId+'"][data-form-path="'+this.options.form_path+'"]');
  95. },
  96. _onGetCurrentValues: function(event, data)
  97. {
  98. event.stopPropagation();
  99. var result = {};
  100. for(var i in this.options.fields_list)
  101. {
  102. var field = this.options.fields_list[i];
  103. if(this._getField(field.id).hasClass('form_field'))
  104. {
  105. result[field.id] = this._getField(field.id).triggerHandler('get_current_value');
  106. }
  107. else
  108. {
  109. console.log('Field set : Cannot retrieve current value from field [' + this.options.field_identifier_attr + '="'+field.id+'"] as it seems to have no itop.form_field widget attached.');
  110. }
  111. }
  112. return result;
  113. },
  114. _getRequestedFields: function(sourceFieldName)
  115. {
  116. var fieldsName = [];
  117. if(this.options.fields_impacts[sourceFieldName] !== undefined)
  118. {
  119. for(var i in this.options.fields_impacts[sourceFieldName])
  120. {
  121. fieldsName.push(this.options.fields_impacts[sourceFieldName][i]);
  122. }
  123. }
  124. return fieldsName;
  125. },
  126. _onFieldChange: function(event, data)
  127. {
  128. event.stopPropagation();
  129. // Set field as touched so we know that we have to do checks on it later
  130. if(this.options.touched_fields.indexOf(data.name) < 0)
  131. {
  132. this.options.touched_fields.push(data.name);
  133. }
  134. // Validate the field
  135. var oRes = this._getField(data.name).triggerHandler('validate', {touched_fields_only: true});
  136. if (!oRes.is_valid)
  137. {
  138. this.options.is_valid = false;
  139. }
  140. var requestedFields = this._getRequestedFields(data.name);
  141. if(requestedFields.length > 0)
  142. {
  143. this.element.trigger('update_fields', {form_path: this.options.form_path, requested_fields: requestedFields});
  144. }
  145. },
  146. _onUpdateForm: function(event, data)
  147. {
  148. event.stopPropagation();
  149. this.buildData.script_code = '';
  150. this.buildData.style_code = '';
  151. for (var i in data.updated_fields)
  152. {
  153. var updated_field = data.updated_fields[i];
  154. this.options.fields_list[updated_field.id] = updated_field;
  155. this._prepareField(updated_field.id);
  156. }
  157. // Adding code to the dom
  158. this.options.script_element.append('\n\n// Appended by update at ' + Date() + '\n' + this.buildData.script_code);
  159. this.options.style_element.append('\n\n// Appended by update at ' + Date() + '\n' + this.buildData.style_code);
  160. // Evaluating script code as adding it to dom did not executed it (only script from update !)
  161. eval(this.buildData.script_code);
  162. },
  163. _onValidate: function(event, data)
  164. {
  165. event.stopPropagation();
  166. this.options.is_valid = true;
  167. var aFieldsToValidate = [];
  168. if ((data.touched_fields_only !== undefined) && (data.touched_fields_only === true))
  169. {
  170. aFieldsToValidate = this.options.touched_fields;
  171. }
  172. else
  173. {
  174. // Requires IE9+ Object.keys(this.options.fields_list);
  175. for (var sFieldId in this.options.fields_list)
  176. {
  177. aFieldsToValidate.push(sFieldId);
  178. }
  179. }
  180. for(var i in aFieldsToValidate)
  181. {
  182. var oRes = this._getField(aFieldsToValidate[i]).triggerHandler('validate', data);
  183. if (!oRes.is_valid)
  184. {
  185. this.options.is_valid = false;
  186. }
  187. }
  188. return this.options.is_valid;
  189. },
  190. showOptions: function() // Debug helper
  191. {
  192. console.log(this.options);
  193. return this.options;
  194. },
  195. _loadCssFile: function(url)
  196. {
  197. if (!$('link[href="'+url+'"]').length)
  198. $('<link href="'+url+'" rel="stylesheet">').appendTo('head');
  199. },
  200. _loadJsFile: function(url)
  201. {
  202. if (!$('script[src="'+url+'"]').length)
  203. $.getScript(url);
  204. },
  205. // Place a field for which no container exists
  206. _addField: function(field_id)
  207. {
  208. $('<div ' + this.options.field_identifier_attr + '="'+field_id+'" data-form-path="' + this.options.form_path + '"></div>').appendTo(this.element);
  209. },
  210. _prepareField: function(field_id)
  211. {
  212. var field = this.options.fields_list[field_id];
  213. if(this._getField(field.id).length === 1)
  214. {
  215. // We replace the node instead of just replacing the inner html so the previous widget is automatically destroyed.
  216. this._getField(field.id).replaceWith( $('<div ' + this.options.field_identifier_attr + '="'+field.id+'" data-form-path="' + this.options.form_path + '"></div>') );
  217. }
  218. else
  219. {
  220. this._addField(field.id);
  221. }
  222. var field_container = this._getField(field.id);
  223. // HTML
  224. if( (field.html !== undefined) && (field.html !== '') )
  225. {
  226. field_container.html(field.html);
  227. }
  228. // JS files
  229. if( (field.js_files !== undefined) && (field.js_files.length > 0) )
  230. {
  231. for(var j in field.js_files)
  232. {
  233. this._loadJsFile(field.js_files[i]);
  234. }
  235. }
  236. // CSS files
  237. if( (field.css_files !== undefined) && (field.css_files.length > 0) )
  238. {
  239. for(var j in field.css_files)
  240. {
  241. this._loadCssFile(field.css_files[i]);
  242. }
  243. }
  244. // JS inline
  245. if( (field.js_inline !== undefined) && (field.js_inline !== '') )
  246. {
  247. this.buildData.script_code += '; '+ field.js_inline;
  248. }
  249. // CSS inline
  250. if( (field.css_inline !== undefined) && (field.css_inline !== '') )
  251. {
  252. this.buildData.style_code += ' '+ field.css_inline;
  253. }
  254. },
  255. buildForm: function()
  256. {
  257. this.buildData.script_code = '';
  258. this.buildData.style_code = '';
  259. for(var i in this.options.fields_list)
  260. {
  261. var field = this.options.fields_list[i];
  262. if(field.id === undefined)
  263. {
  264. console.log('Field set : An field must have at least an id property.');
  265. return false;
  266. }
  267. this._prepareField(field.id);
  268. }
  269. this.options.script_element.text('$(document).ready(function(){ '+this.buildData.script_code+' });');
  270. this.options.style_element.text(this.buildData.style_code);
  271. eval(this.options.script_element.text());
  272. }
  273. });
  274. });