field_set.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Copyright (C) 2010-2016 Combodo SARL
  2. //
  3. // This file is part of iTop.
  4. //
  5. // iTop is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // iTop is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  17. //iTop Field set
  18. //Used by itop.form_handler and itop.subform_field to list their fields
  19. ;
  20. $(function()
  21. {
  22. // the widget definition, where 'itop' is the namespace,
  23. // 'field_set' the widget name
  24. $.widget( 'itop.field_set',
  25. {
  26. // default options
  27. options:
  28. {
  29. field_identifier_attr: 'data-field-id', // convention: fields are rendered into a div and are identified by this attribute
  30. fields_list: null,
  31. fields_impacts: {},
  32. touched_fields: [],
  33. is_valid: true,
  34. form_path: '',
  35. script_element: null,
  36. style_element: null
  37. },
  38. buildData:
  39. {
  40. script_code: '',
  41. style_code: ''
  42. },
  43. // the constructor
  44. _create: function()
  45. {
  46. var me = this;
  47. this.element
  48. .addClass('field_set');
  49. this.element
  50. .bind('field_change', function(oEvent, oData){
  51. //console.log('field_set: field_change');
  52. me._onFieldChange(oEvent, oData);
  53. })
  54. .bind('update_form', function(oEvent, oData){
  55. //console.log('field_set: update_form');
  56. me._onUpdateForm(oEvent, oData);
  57. })
  58. .bind('get_current_values', function(oEvent, oData){
  59. //console.log('field_set: get_current_values');
  60. return me._onGetCurrentValues(oEvent, oData);
  61. })
  62. .bind('validate', function(oEvent, oData){
  63. if (oData === undefined)
  64. {
  65. oData = {};
  66. }
  67. //console.log('field_set: validate');
  68. return me._onValidate(oEvent, oData);
  69. });
  70. // Creating DOM elements if not using user's specifics
  71. if(this.options.script_element === null)
  72. {
  73. this.options.script_element = $('<script type="text/javascript"></script>');
  74. this.element.after(this.options.script_element);
  75. }
  76. if(this.options.style_element === null)
  77. {
  78. this.options.style_element = $('<style></style>');
  79. this.element.before(this.options.style_element);
  80. }
  81. // Building the form
  82. if(this.options.fields_list !== null)
  83. {
  84. this.buildForm();
  85. }
  86. },
  87. // called when created, and later when changing options
  88. _refresh: function()
  89. {
  90. },
  91. // events bound via _bind are removed automatically
  92. // revert other modifications here
  93. _destroy: function()
  94. {
  95. this.element
  96. .removeClass('field_set');
  97. },
  98. // _setOptions is called with a hash of all options that are changing
  99. // always refresh when changing options
  100. _setOptions: function()
  101. {
  102. this._superApply(arguments);
  103. },
  104. // _setOption is called for each individual option that is changing
  105. _setOption: function( key, value )
  106. {
  107. this._super( key, value );
  108. },
  109. getField: function (sFieldId)
  110. {
  111. return this.element.find('[' + this.options.field_identifier_attr + '="' + sFieldId + '"][data-form-path="' + this.options.form_path + '"]');
  112. },
  113. _onGetCurrentValues: function(oEvent, oData)
  114. {
  115. oEvent.stopPropagation();
  116. var oResult = {};
  117. for(var i in this.options.fields_list)
  118. {
  119. var oField = this.options.fields_list[i];
  120. if(this.getField(oField.id).hasClass('form_field'))
  121. {
  122. oResult[oField.id] = this.getField(oField.id).triggerHandler('get_current_value');
  123. }
  124. else
  125. {
  126. console.log('Field set : Cannot retrieve current value from field [' + this.options.field_identifier_attr + '="' + oField.id + '"][data-form-path="' + this.options.form_path + '"] as it seems to have no itop.form_field widget attached.');
  127. }
  128. }
  129. return oResult;
  130. },
  131. _getRequestedFields: function(sSourceFieldName)
  132. {
  133. var aFieldsName = [];
  134. if(this.options.fields_impacts[sSourceFieldName] !== undefined)
  135. {
  136. for(var i in this.options.fields_impacts[sSourceFieldName])
  137. {
  138. aFieldsName.push(this.options.fields_impacts[sSourceFieldName][i]);
  139. }
  140. }
  141. return aFieldsName;
  142. },
  143. _onFieldChange: function(oEvent, oData)
  144. {
  145. oEvent.stopPropagation();
  146. // Set field as touched so we know that we have to do checks on it later
  147. if(this.options.touched_fields.indexOf(oData.name) < 0)
  148. {
  149. this.options.touched_fields.push(oData.name);
  150. }
  151. // Validate the field
  152. var oResult = this.getField(oData.name).triggerHandler('validate', {touched_fields_only: true});
  153. if (!oResult.is_valid)
  154. {
  155. this.options.is_valid = false;
  156. }
  157. var oRequestedFields = this._getRequestedFields(oData.name);
  158. if(oRequestedFields.length > 0)
  159. {
  160. this.element.trigger('update_fields', {form_path: this.options.form_path, requested_fields: oRequestedFields});
  161. }
  162. },
  163. _onUpdateForm: function(oEvent, oData)
  164. {
  165. oEvent.stopPropagation();
  166. this.buildData.script_code = '';
  167. this.buildData.style_code = '';
  168. for (var i in oData.updated_fields)
  169. {
  170. var oUpdatedField = oData.updated_fields[i];
  171. this.options.fields_list[oUpdatedField.id] = oUpdatedField;
  172. this._prepareField(oUpdatedField.id);
  173. }
  174. // Adding code to the dom
  175. // Note : We use text() instead of append(), otherwise the code will be interpreted as DOM tags (text + <img /> + ...) and can break some browsers
  176. this.options.script_element.text( this.options.script_element.text() + '\n\n// Appended by update on ' + Date() + '\n' + this.buildData.script_code);
  177. this.options.style_element.text( this.options.style_element.text() + '\n\n// Appended by update on ' + Date() + '\n' + this.buildData.style_code);
  178. // Evaluating script code as adding it to dom did not executed it (only script from update !)
  179. eval(this.buildData.script_code);
  180. },
  181. _onValidate: function(oEvent, oData)
  182. {
  183. oEvent.stopPropagation();
  184. this.options.is_valid = true;
  185. var aFieldsToValidate = [];
  186. if ((oData.touched_fields_only !== undefined) && (oData.touched_fields_only === true))
  187. {
  188. aFieldsToValidate = this.options.touched_fields;
  189. }
  190. else
  191. {
  192. // TODO : Requires IE9+ Object.keys(this.options.fields_list);
  193. for (var sFieldId in this.options.fields_list)
  194. {
  195. aFieldsToValidate.push(sFieldId);
  196. }
  197. }
  198. for(var i in aFieldsToValidate)
  199. {
  200. var oField = this.getField(aFieldsToValidate[i]);
  201. // Checking if the field still exists as it could have been from a dynamic subform (Typically with custom fields)
  202. if(oField.length > 0 && oField.hasClass('form_field'))
  203. {
  204. var oRes = oField.triggerHandler('validate', oData);
  205. if (!oRes.is_valid)
  206. {
  207. this.options.is_valid = false;
  208. }
  209. }
  210. }
  211. return this.options.is_valid;
  212. },
  213. // Debug helper
  214. showOptions: function()
  215. {
  216. return this.options;
  217. },
  218. _loadCssFile: function(url)
  219. {
  220. if (!$('link[href="' + url + '"]').length)
  221. $('<link href="' + url + '" rel="stylesheet">').appendTo('head');
  222. },
  223. _loadJsFile: function(url)
  224. {
  225. if (!$('script[src="' + url + '"]').length)
  226. $.getScript(url);
  227. },
  228. // Place a field for which no container exists
  229. _addField: function(sFieldId)
  230. {
  231. $('<div ' + this.options.field_identifier_attr + '="' + sFieldId + '" data-form-path="' + this.options.form_path + '"></div>').appendTo(this.element);
  232. },
  233. _prepareField: function(sFieldId)
  234. {
  235. var oField = this.options.fields_list[sFieldId];
  236. if(this.getField(oField.id).length === 1)
  237. {
  238. // We replace the node instead of just replacing the inner html so the previous widget is automatically destroyed.
  239. this.getField(oField.id).replaceWith(
  240. $('<div ' + this.options.field_identifier_attr + '="' + oField.id + '" data-form-path="' + this.options.form_path + '"></div>')
  241. );
  242. }
  243. else
  244. {
  245. this._addField(oField.id);
  246. }
  247. var oFieldContainer = this.getField(oField.id);
  248. // HTML
  249. if( (oField.html !== undefined) && (oField.html !== '') )
  250. {
  251. oFieldContainer.html(oField.html);
  252. }
  253. // JS files
  254. if( (oField.js_files !== undefined) && (oField.js_files.length > 0) )
  255. {
  256. for(var i in oField.js_files)
  257. {
  258. this._loadJsFile(oField.js_files[i]);
  259. }
  260. }
  261. // CSS files
  262. if( (oField.css_files !== undefined) && (oField.css_files.length > 0) )
  263. {
  264. for(var i in oField.css_files)
  265. {
  266. this._loadCssFile(oField.css_files[i]);
  267. }
  268. }
  269. // JS inline
  270. if( (oField.js_inline !== undefined) && (oField.js_inline !== '') )
  271. {
  272. this.buildData.script_code += '; '+ oField.js_inline;
  273. }
  274. // CSS inline
  275. if( (oField.css_inline !== undefined) && (oField.css_inline !== '') )
  276. {
  277. this.buildData.style_code += ' '+ oField.css_inline;
  278. }
  279. },
  280. buildForm: function()
  281. {
  282. this.buildData.script_code = '';
  283. this.buildData.style_code = '';
  284. for(var i in this.options.fields_list)
  285. {
  286. var oField = this.options.fields_list[i];
  287. if(oField.id === undefined)
  288. {
  289. console.log('Field set : A field must have at least an id property.');
  290. return false;
  291. }
  292. this._prepareField(oField.id);
  293. }
  294. this.options.script_element.text('$(document).ready(function(){ ' + this.buildData.script_code + ' });');
  295. this.options.style_element.text(this.buildData.style_code);
  296. eval(this.options.script_element.text());
  297. }
  298. });
  299. });