form.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }
  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. /**
  78. * reset values to default of every field in the form
  79. */
  80. clearValues: function () {
  81. this.get('fields').forEach(function (field) {
  82. var value = (field.get('defaultValue') === undefined) ? '' : field.get('defaultValue');
  83. field.set('value', value);
  84. }, this);
  85. },
  86. visibleFields:Em.computed.filterBy('fields', 'isHiddenField', false),
  87. resultText:function () {
  88. var text = "";
  89. switch (this.get('result')) {
  90. case -1:
  91. text = this.t("form.saveError");
  92. break;
  93. case 1:
  94. text = this.t("form.saveSuccess");
  95. break;
  96. }
  97. return text;
  98. }.property('result')
  99. });
  100. App.FormField = Em.Object.extend({ // try to realize this as view
  101. name:'',
  102. displayName:'',
  103. // defaultValue:'', NOT REALIZED YET
  104. description:'',
  105. disabled:false,
  106. displayType:'string', // string, digits, number, directories, textarea, checkbox
  107. disableRequiredOnPresent:false,
  108. errorMessage:'',
  109. warnMessage:'',
  110. form:false,
  111. isRequired:true, // by default a config property is required
  112. unit:'',
  113. value:'',
  114. isValid:Em.computed.equal('errorMessage', ''),
  115. viewClass:function () {
  116. var options = {};
  117. var element = Em.TextField;
  118. switch (this.get('displayType')) {
  119. case 'checkbox':
  120. element = Em.Checkbox;
  121. options.checkedBinding = "value";
  122. break;
  123. case 'select':
  124. element = Em.Select;
  125. options.content = this.get('values');
  126. options.valueBinding = "value";
  127. options.optionValuePath = "content.value";
  128. options.optionLabelPath = "content.label";
  129. break;
  130. case 'password':
  131. options['type'] = 'password';
  132. break;
  133. case 'textarea':
  134. element = Em.TextArea;
  135. break;
  136. case 'hidden':
  137. options.type = "hidden";
  138. break;
  139. }
  140. return element.extend(options);
  141. }.property('displayType'),
  142. validate:function () {
  143. var value = this.get('value');
  144. var isError = false;
  145. this.set('errorMessage', '');
  146. if (this.get('isRequired') && (typeof value === 'string' && value.trim().length === 0)) {
  147. this.set('errorMessage', 'This is required');
  148. isError = true;
  149. }
  150. if (typeof value === 'string' && value.trim().length === 0) { // this is not to validate empty field.
  151. isError = true;
  152. }
  153. if (!isError) {
  154. this.set('errorMessage', '');
  155. }
  156. },
  157. isHiddenField: Em.computed.equal('displayType', 'hidden')
  158. });