form.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var validator = require('utils/validator');
  20. // move this to models cause some errors
  21. App.Form = Em.View.extend({
  22. /**
  23. * generating fields from fieldsOptions
  24. */
  25. classNames: ["form-horizontal"],
  26. i18nprefix: 'form.',
  27. fields: [],
  28. field: {},
  29. messages: [],
  30. object: false,
  31. result: 0, // save result var (-1 - error; 0 - init; 1 - success)
  32. templateName: require('templates/common/form'),
  33. tagName: 'form',
  34. init: function () {
  35. this._super();
  36. var thisForm = this;
  37. if (!this.fields.length)
  38. $.each(this.fieldsOptions,
  39. function () {
  40. var field = App.FormField.create(this);
  41. field.set('form', thisForm);
  42. thisForm.fields.push(field);
  43. thisForm.set("field." + field.get('name'), field);
  44. });
  45. },
  46. getField: function (name) {
  47. var field = false;
  48. $.each(this.fields, function () {
  49. if (this.get('name') == name) {
  50. return field = this;
  51. }
  52. });
  53. return field;
  54. },
  55. isValid: function () {
  56. var isValid = true;
  57. $.each(this.fields, function () {
  58. this.validate();
  59. if (!this.get('isValid')) {
  60. isValid = false;
  61. console.warn(this.get('name') + " IS INVALID : " + this.get('errorMessage'));
  62. }
  63. })
  64. return isValid;
  65. },
  66. isObjectNew: function () {
  67. var object = this.get('object');
  68. return !(object instanceof DS.Model && object.get('id'));
  69. }.property("object"),
  70. updateValues: function () {
  71. var object = this.get('object');
  72. if (object instanceof Em.Object) {
  73. $.each(this.fields, function () {
  74. this.set('value', this.get('displayType') == 'password' ? '' : object.get(this.get('name')));
  75. });
  76. } else {
  77. this.clearValues();
  78. }
  79. }.observes("object"),
  80. /**
  81. *
  82. */
  83. getValues: function () {
  84. var values = {};
  85. $.each(this.fields, function () {
  86. if (!(this.get('displayType') == 'password') && validator.empty(this.get('value'))) // if this is not empty password field
  87. values[this.get('name')] = this.get('value');
  88. });
  89. return values;
  90. },
  91. clearValues: function () {
  92. $.each(this.fields, function () {
  93. this.set('value', '');
  94. });
  95. },
  96. /**
  97. * need to refactor for integration
  98. * @return {Boolean}
  99. */
  100. save: function () {
  101. var thisForm = this;
  102. var object = this.get('object');
  103. if (!this.get('isObjectNew')) {
  104. $.each(this.getValues(), function (i, v) {
  105. object.set(i, v);
  106. });
  107. } else {
  108. if (this.get('className'))
  109. App.store.createRecord(this.get('className'), this.getValues())
  110. else
  111. console.log("Please define class name for your form " + this.constructor);
  112. }
  113. App.store.commit();
  114. this.set('result', 1);
  115. return true;
  116. },
  117. resultText: function () {
  118. var text = "";
  119. switch (this.get('result')) {
  120. case -1:
  121. text = this.t("form.saveError");
  122. break;
  123. case 1:
  124. text = this.t("form.saveSuccess");
  125. break;
  126. }
  127. return text;
  128. }.property('result'),
  129. saveButtonText: function () {
  130. return Em.I18n.t(this.get('i18nprefix') + (this.get('isObjectNew') ? "create" : "save"));
  131. }.property('isObjectNew')
  132. // not recommended
  133. // cancelButtonText:function () {
  134. // return Em.I18n.t(this.get('i18nprefix') + 'cancel').property();
  135. // }
  136. });
  137. App.FormField = Em.Object.extend({ // try to realize this as view
  138. name: '',
  139. displayName: '',
  140. // defaultValue:'', NOT REALIZED YET
  141. description: '',
  142. disabled: false,
  143. displayType: 'string', // string, digits, number, directories, textarea, checkbox
  144. disableRequiredOnPresent: false,
  145. errorMessage: '',
  146. form: false,
  147. isRequired: true, // by default a config property is required
  148. unit: '',
  149. value: '',
  150. isValid: function () {
  151. return this.get('errorMessage') === '';
  152. }.property('errorMessage'),
  153. viewClass: function () {
  154. var options = {};
  155. var element = Em.TextField;
  156. switch (this.get('displayType')) {
  157. case 'checkbox':
  158. element = Em.Checkbox;
  159. options.checkedBinding = "value";
  160. break;
  161. case 'select':
  162. element = Em.Select;
  163. options.content = this.get('values');
  164. options.valueBinding = "value";
  165. options.optionValuePath = "content.value";
  166. options.optionLabelPath = "content.label";
  167. break;
  168. case 'password':
  169. options['type'] = 'password';
  170. break;
  171. case 'textarea':
  172. element = Em.TextArea;
  173. break;
  174. }
  175. return element.extend(options);
  176. }.property('displayType'),
  177. validate: function () {
  178. var digitsRegex = /^\d+$/;
  179. var numberRegex = /^[-,+]?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
  180. var value = this.get('value');
  181. var isError = false;
  182. this.set('errorMessage', '');
  183. if (this.get('isRequired') && (typeof value === 'string' && value.trim().length === 0)) {
  184. this.set('errorMessage', 'This is required');
  185. isError = true;
  186. }
  187. if (typeof value === 'string' && value.trim().length === 0) { // this is not to validate empty field.
  188. isError = true;
  189. }
  190. if (!isError) {
  191. switch (this.get('validator')) {
  192. case 'ipaddress':
  193. if (!validator.isIpAddress(value) && !validator.isDomainName(value)) {
  194. isError = true;
  195. this.set('errorMessage', Em.I18n.t("form.validator.invalidIp"));
  196. }
  197. break;
  198. case 'passwordRetype':
  199. var form = this.get('form');
  200. var passwordField = form.getField('password');
  201. if (passwordField.get('isValid')
  202. && (passwordField.get('value') != this.get('value'))
  203. && passwordField.get('value') && this.get('value')
  204. ) {
  205. this.set('errorMessage', "Passwords are different");
  206. isError = true;
  207. }
  208. break;
  209. default:
  210. break;
  211. }
  212. switch (this.get('displayType')) {
  213. case 'digits':
  214. if (!digitsRegex.test(value)) {
  215. this.set('errorMessage', 'Must contain digits only');
  216. isError = true;
  217. }
  218. break;
  219. case 'number':
  220. if (!numberRegex.test(value)) {
  221. this.set('errorMessage', 'Must be a valid number');
  222. isError = true;
  223. }
  224. break;
  225. case 'directories':
  226. break;
  227. case 'custom':
  228. break;
  229. case 'password':
  230. break;
  231. }
  232. }
  233. if (!isError) {
  234. this.set('errorMessage', '');
  235. }
  236. }.observes('value')
  237. });