form.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. var thisForm = this;
  36. if (!this.fields.length) {
  37. this.fieldsOptions.forEach(
  38. function (options) {
  39. var field = App.FormField.create(options);
  40. field.set('form', thisForm);
  41. thisForm.fields.push(field);
  42. thisForm.set("field." + field.get('name'), field);
  43. }
  44. );
  45. }
  46. this._super();
  47. },
  48. getField:function (name) {
  49. var field = false;
  50. $.each(this.fields, function () {
  51. if (this.get('name') == name) {
  52. return field = this;
  53. }
  54. });
  55. return field;
  56. },
  57. isValid:function () {
  58. var isValid = true;
  59. $.each(this.fields, function () {
  60. this.validate();
  61. if (!this.get('isValid')) {
  62. isValid = false;
  63. console.warn(this.get('name') + " IS INVALID : " + this.get('errorMessage'));
  64. }
  65. })
  66. return isValid;
  67. },
  68. isObjectNew:function () {
  69. var object = this.get('object');
  70. if(object instanceof App.User){
  71. return false;
  72. }
  73. return !(object instanceof DS.Model && object.get('id'));
  74. }.property("object"),
  75. updateValues:function () {
  76. var object = this.get('object');
  77. if (object instanceof Em.Object) {
  78. $.each(this.fields, function () {
  79. this.set('value', (this.get('displayType') == 'password') ? '' : object.get(this.get('name')));
  80. });
  81. } else {
  82. this.clearValues();
  83. }
  84. }.observes("object"),
  85. clearValues:function () {
  86. $.each(this.fields, function () {
  87. this.set('value', '');
  88. });
  89. },
  90. /**
  91. * need to refactor for integration
  92. * @return {Boolean}
  93. */
  94. save:function () {
  95. var object = this.get('object');
  96. var formValues = {};
  97. $.each(this.get('fields'), function () {
  98. formValues[this.get('name')] = this.get('value');
  99. });
  100. if (!this.get('isObjectNew')) {
  101. $.each(formValues, function (k, v) {
  102. object.set(k, v);
  103. });
  104. }
  105. else {
  106. if (this.get('className')) {
  107. App.store.createRecord(this.get('className'), formValues);
  108. }
  109. else {
  110. console.log("Please define class name for your form " + this.constructor);
  111. }
  112. }
  113. //App.store.commit();
  114. this.set('result', 1);
  115. return true;
  116. },
  117. visibleFields:function () {
  118. var fields = this.get('fields');
  119. var visible = [];
  120. fields.forEach(function (field) {
  121. if (!field.get('isHiddenField')) {
  122. visible.push(field);
  123. }
  124. });
  125. return visible;
  126. }.property('fields'),
  127. resultText:function () {
  128. var text = "";
  129. switch (this.get('result')) {
  130. case -1:
  131. text = this.t("form.saveError");
  132. break;
  133. case 1:
  134. text = this.t("form.saveSuccess");
  135. break;
  136. }
  137. return text;
  138. }.property('result'),
  139. saveButtonText:function () {
  140. return Em.I18n.t(this.get('i18nprefix') + (this.get('isObjectNew') ? "create" : "save"));
  141. }.property('isObjectNew')
  142. // not recommended
  143. // cancelButtonText:function () {
  144. // return Em.I18n.t(this.get('i18nprefix') + 'cancel').property();
  145. // }
  146. });
  147. App.FormField = Em.Object.extend({ // try to realize this as view
  148. name:'',
  149. displayName:'',
  150. // defaultValue:'', NOT REALIZED YET
  151. description:'',
  152. disabled:false,
  153. displayType:'string', // string, digits, number, directories, textarea, checkbox
  154. disableRequiredOnPresent:false,
  155. errorMessage:'',
  156. form:false,
  157. isRequired:true, // by default a config property is required
  158. unit:'',
  159. value:'',
  160. observeValue:function () {
  161. if (this.get('displayType') == 'hidden')
  162. console.warn(" FORM FIELD VALUE: ", this.get('value'));
  163. }.observes('value'),
  164. isValid:function () {
  165. return this.get('errorMessage') === '';
  166. }.property('errorMessage'),
  167. viewClass:function () {
  168. var options = {};
  169. var element = Em.TextField;
  170. switch (this.get('displayType')) {
  171. case 'checkbox':
  172. element = Em.Checkbox;
  173. options.checkedBinding = "value";
  174. break;
  175. case 'select':
  176. element = Em.Select;
  177. options.content = this.get('values');
  178. options.valueBinding = "value";
  179. options.optionValuePath = "content.value";
  180. options.optionLabelPath = "content.label";
  181. break;
  182. case 'password':
  183. options['type'] = 'password';
  184. break;
  185. case 'textarea':
  186. element = Em.TextArea;
  187. break;
  188. case 'hidden':
  189. options.type = "hidden";
  190. break;
  191. }
  192. return element.extend(options);
  193. }.property('displayType'),
  194. validate:function () {
  195. var digitsRegex = /^\d+$/;
  196. var numberRegex = /^[-,+]?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
  197. var value = this.get('value');
  198. var isError = false;
  199. this.set('errorMessage', '');
  200. if (this.get('isRequired') && (typeof value === 'string' && value.trim().length === 0)) {
  201. this.set('errorMessage', 'This is required');
  202. isError = true;
  203. }
  204. if (typeof value === 'string' && value.trim().length === 0) { // this is not to validate empty field.
  205. isError = true;
  206. }
  207. if (!isError) {
  208. switch (this.get('validator')) {
  209. case 'ipaddress':
  210. if (!validator.isIpAddress(value) && !validator.isDomainName(value)) {
  211. isError = true;
  212. this.set('errorMessage', Em.I18n.t("form.validator.invalidIp"));
  213. }
  214. break;
  215. case 'passwordRetype':
  216. var form = this.get('form');
  217. var passwordField = form.getField('password');
  218. if (passwordField.get('isValid')
  219. && (passwordField.get('value') != this.get('value'))
  220. && passwordField.get('value') && this.get('value')
  221. ) {
  222. this.set('errorMessage', "Passwords are different");
  223. isError = true;
  224. }
  225. break;
  226. default:
  227. break;
  228. }
  229. switch (this.get('displayType')) {
  230. case 'digits':
  231. if (!digitsRegex.test(value)) {
  232. this.set('errorMessage', 'Must contain digits only');
  233. isError = true;
  234. }
  235. break;
  236. case 'number':
  237. if (!numberRegex.test(value)) {
  238. this.set('errorMessage', 'Must be a valid number');
  239. isError = true;
  240. }
  241. break;
  242. case 'directories':
  243. break;
  244. case 'custom':
  245. break;
  246. case 'password':
  247. break;
  248. }
  249. }
  250. if (!isError) {
  251. this.set('errorMessage', '');
  252. }
  253. },
  254. isHiddenField:function () {
  255. return this.get('displayType') == 'hidden';
  256. }.property('type')
  257. });