form.js 5.1 KB

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