form.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // move this to models cause some errors
  20. App.Form = Em.View.extend({
  21. /**
  22. * generating fields from fieldsOptions
  23. */
  24. classNames:["form-horizontal"],
  25. i18nprefix:'form.',
  26. fields:[],
  27. field:{},
  28. messages:[],
  29. object:false,
  30. result:0, // save result var (-1 - error; 0 - init; 1 - success)
  31. templateName:require('templates/common/form'),
  32. tagName:'form',
  33. init:function () {
  34. var thisForm = this;
  35. if (!this.fields.length) {
  36. this.fieldsOptions.forEach(
  37. function (options) {
  38. var field = App.FormField.create(options);
  39. field.set('form', thisForm);
  40. thisForm.fields.push(field);
  41. thisForm.set("field." + field.get('name'), field);
  42. }
  43. );
  44. }
  45. this._super();
  46. },
  47. getField:function (name) {
  48. var field = false;
  49. $.each(this.fields, function () {
  50. if (this.get('name') == name) {
  51. return field = this;
  52. }
  53. });
  54. return field;
  55. },
  56. isValid:function () {
  57. var isValid = true;
  58. $.each(this.fields, function () {
  59. this.validate();
  60. if (!this.get('isValid')) {
  61. isValid = false;
  62. console.warn(this.get('name') + " IS INVALID : " + this.get('errorMessage'));
  63. }
  64. })
  65. return isValid;
  66. },
  67. updateValues:function () {
  68. var object = this.get('object');
  69. if (object instanceof Em.Object) {
  70. $.each(this.fields, function () {
  71. this.set('value', (this.get('displayType') == 'password') ? '' : object.get(this.get('name')));
  72. });
  73. } else {
  74. this.clearValues();
  75. }
  76. }.observes("object"),
  77. clearValues:function () {
  78. $.each(this.fields, function () {
  79. this.set('value', '');
  80. });
  81. },
  82. visibleFields:function () {
  83. var fields = this.get('fields');
  84. var visible = [];
  85. fields.forEach(function (field) {
  86. if (!field.get('isHiddenField')) {
  87. visible.push(field);
  88. }
  89. });
  90. return visible;
  91. }.property('fields'),
  92. resultText:function () {
  93. var text = "";
  94. switch (this.get('result')) {
  95. case -1:
  96. text = this.t("form.saveError");
  97. break;
  98. case 1:
  99. text = this.t("form.saveSuccess");
  100. break;
  101. }
  102. return text;
  103. }.property('result')
  104. });
  105. App.FormField = Em.Object.extend({ // try to realize this as view
  106. name:'',
  107. displayName:'',
  108. // defaultValue:'', NOT REALIZED YET
  109. description:'',
  110. disabled:false,
  111. displayType:'string', // string, digits, number, directories, textarea, checkbox
  112. disableRequiredOnPresent:false,
  113. errorMessage:'',
  114. form:false,
  115. isRequired:true, // by default a config property is required
  116. unit:'',
  117. value:'',
  118. observeValue:function () {
  119. if (this.get('displayType') == 'hidden')
  120. console.warn(" FORM FIELD VALUE: ", this.get('value'));
  121. }.observes('value'),
  122. isValid:function () {
  123. return this.get('errorMessage') === '';
  124. }.property('errorMessage'),
  125. viewClass:function () {
  126. var options = {};
  127. var element = Em.TextField;
  128. switch (this.get('displayType')) {
  129. case 'checkbox':
  130. element = Em.Checkbox;
  131. options.checkedBinding = "value";
  132. break;
  133. case 'select':
  134. element = Em.Select;
  135. options.content = this.get('values');
  136. options.valueBinding = "value";
  137. options.optionValuePath = "content.value";
  138. options.optionLabelPath = "content.label";
  139. break;
  140. case 'password':
  141. options['type'] = 'password';
  142. break;
  143. case 'textarea':
  144. element = Em.TextArea;
  145. break;
  146. case 'hidden':
  147. options.type = "hidden";
  148. break;
  149. }
  150. return element.extend(options);
  151. }.property('displayType'),
  152. validate:function () {
  153. var value = this.get('value');
  154. var isError = false;
  155. this.set('errorMessage', '');
  156. if (this.get('isRequired') && (typeof value === 'string' && value.trim().length === 0)) {
  157. this.set('errorMessage', 'This is required');
  158. isError = true;
  159. }
  160. if (typeof value === 'string' && value.trim().length === 0) { // this is not to validate empty field.
  161. isError = true;
  162. }
  163. if (!isError) {
  164. if(this.get('validator') === 'passwordRetype'){
  165. var form = this.get('form');
  166. var passwordField = form.getField('password');
  167. if (passwordField.get('isValid')
  168. && (passwordField.get('value') != this.get('value'))
  169. && passwordField.get('value') && this.get('value')
  170. ) {
  171. this.set('errorMessage', "Passwords are different");
  172. isError = true;
  173. }
  174. }
  175. }
  176. if (!isError) {
  177. this.set('errorMessage', '');
  178. }
  179. },
  180. isHiddenField:function () {
  181. return this.get('displayType') == 'hidden';
  182. }.property('type')
  183. });