field_set.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 oRes = this.getField(aFieldsToValidate[i]).triggerHandler('validate', oData);
  201. if (!oRes.is_valid)
  202. {
  203. this.options.is_valid = false;
  204. }
  205. }
  206. return this.options.is_valid;
  207. },
  208. // Debug helper
  209. showOptions: function()
  210. {
  211. return this.options;
  212. },
  213. _loadCssFile: function(url)
  214. {
  215. if (!$('link[href="' + url + '"]').length)
  216. $('<link href="' + url + '" rel="stylesheet">').appendTo('head');
  217. },
  218. _loadJsFile: function(url)
  219. {
  220. if (!$('script[src="' + url + '"]').length)
  221. $.getScript(url);
  222. },
  223. // Place a field for which no container exists
  224. _addField: function(sFieldId)
  225. {
  226. $('<div ' + this.options.field_identifier_attr + '="' + sFieldId + '" data-form-path="' + this.options.form_path + '"></div>').appendTo(this.element);
  227. },
  228. _prepareField: function(sFieldId)
  229. {
  230. var oField = this.options.fields_list[sFieldId];
  231. if(this.getField(oField.id).length === 1)
  232. {
  233. // We replace the node instead of just replacing the inner html so the previous widget is automatically destroyed.
  234. this.getField(oField.id).replaceWith(
  235. $('<div ' + this.options.field_identifier_attr + '="' + oField.id + '" data-form-path="' + this.options.form_path + '"></div>')
  236. );
  237. }
  238. else
  239. {
  240. this._addField(oField.id);
  241. }
  242. var oFieldContainer = this.getField(oField.id);
  243. // HTML
  244. if( (oField.html !== undefined) && (oField.html !== '') )
  245. {
  246. oFieldContainer.html(oField.html);
  247. }
  248. // JS files
  249. if( (oField.js_files !== undefined) && (oField.js_files.length > 0) )
  250. {
  251. for(var j in oField.js_files)
  252. {
  253. this._loadJsFile(oField.js_files[i]);
  254. }
  255. }
  256. // CSS files
  257. if( (oField.css_files !== undefined) && (oField.css_files.length > 0) )
  258. {
  259. for(var j in oField.css_files)
  260. {
  261. this._loadCssFile(oField.css_files[i]);
  262. }
  263. }
  264. // JS inline
  265. if( (oField.js_inline !== undefined) && (oField.js_inline !== '') )
  266. {
  267. this.buildData.script_code += '; '+ oField.js_inline;
  268. }
  269. // CSS inline
  270. if( (oField.css_inline !== undefined) && (oField.css_inline !== '') )
  271. {
  272. this.buildData.style_code += ' '+ oField.css_inline;
  273. }
  274. },
  275. buildForm: function()
  276. {
  277. this.buildData.script_code = '';
  278. this.buildData.style_code = '';
  279. for(var i in this.options.fields_list)
  280. {
  281. var oField = this.options.fields_list[i];
  282. if(oField.id === undefined)
  283. {
  284. console.log('Field set : A field must have at least an id property.');
  285. return false;
  286. }
  287. this._prepareField(oField.id);
  288. }
  289. this.options.script_element.text('$(document).ready(function(){ ' + this.buildData.script_code + ' });');
  290. this.options.style_element.text(this.buildData.style_code);
  291. eval(this.options.script_element.text());
  292. }
  293. });
  294. });