<< | к задаче | главная | печатать | обсудить(0 сообщений) >>
Задача: qForms, библиотека типичного функционала валидации/построения/связки html-форм
Исходник: field.js библиотечный файл :: qForms api-139 [javascript, code #154, hits: 8713, рейтинг: 3/7,4.88(2414)] +
автор: - [добавлен: 28.05.2006] управление:
  1. // define Field makeContainer(); prototype
  2. function _Field_makeContainer(bindTo){
  3. lstContainers = (arguments.length == 0) ? this.name : this.name + "," + arguments[0];
  4. this.container = true;
  5. this.defaultValue = this.getValue();
  6. this.lastValue = this.defaultValue;
  7. this.dummyContainer = false;
  8. this.boundContainers = _listToArray(lstContainers.toLowerCase());
  9. var thisKey = this.qForm._name + "_" + this.name.toLowerCase();
  10.  
  11. // copy objects from the select box into the container object
  12. qFormAPI.containers[thisKey] = new Object();
  13. for( var i=0; i < this.obj.options.length; i++ ){
  14. qFormAPI.containers[thisKey][this.obj.options[i].value] = this.obj.options[i].text;
  15. }
  16. }
  17. Field.prototype.makeContainer = _Field_makeContainer;
  18.  
  19. // define Field resetLast(); prototype
  20. function _Field_resetLast(){
  21. this.setValue(this.lastValue, null, false);
  22. return true;
  23. }
  24. Field.prototype.resetLast = _Field_resetLast;
  25.  
  26. // define Field toUpperCase(); prototype
  27. function _Field_toUpperCase(){
  28. this.setValue(this.getValue().toUpperCase(), null, false);
  29. return true;
  30. }
  31. Field.prototype.toUpperCase = _Field_toUpperCase;
  32.  
  33. // define Field toLowerCase(); prototype
  34. function _Field_toLowerCase(){
  35. this.setValue(this.getValue().toLowerCase(), null, false);
  36. return true;
  37. }
  38. Field.prototype.toLowerCase = _Field_toLowerCase;
  39.  
  40. // define Field ltrim(); prototype
  41. function _Field_ltrim(){
  42. this.setValue(_ltrim(this.getValue()), null, false);
  43. return true;
  44. }
  45. Field.prototype.ltrim = _Field_ltrim;
  46.  
  47. // define Field rtrim(); prototype
  48. function _Field_rtrim(){
  49. this.setValue(_rtrim(this.getValue()), null, false);
  50. return true;
  51. }
  52. Field.prototype.rtrim = _Field_rtrim;
  53.  
  54. // define Field trim(); prototype
  55. function _Field_trim(){
  56. this.setValue(_trim(this.getValue()), null, false);
  57. return true;
  58. }
  59. Field.prototype.trim = _Field_trim;
  60.  
  61. // define Field compare(); prototype
  62. function _Field_compare(field){
  63. if( this.getValue() == this.qForm[field].getValue() ){
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. return true;
  69. }
  70. Field.prototype.compare = _Field_compare;
  71.  
  72. // define Field mirrorTo(); prototype
  73. function _Field_mirrorTo(objName){
  74. // test to see if the object is qForm object
  75. isQForm = ( objName.indexOf(".") > -1 ) ? !eval("!objName.substring(0,objName.indexOf('.'))") : false;
  76.  
  77. // if it's a qForm object, then set the value of the field to the current field when updated
  78. if( isQForm ){
  79. var strCommand = objName + ".setValue(" + this.pointer + ".getValue()" + ", null, false);";
  80. // otherwise, set the local variable
  81. } else {
  82. var strCommand = objName + " = " + this.pointer + ".getValue();";
  83. }
  84.  
  85. // add an onblur event so that when the field is updated, the requested field
  86. // is updated with the value
  87. this.addEvent(_getEventType(this.type), strCommand, false);
  88. }
  89. Field.prototype.mirrorTo = _Field_mirrorTo;
  90.  
  91. // define Field createDependencyTo(); prototype
  92. function _Field_createDependencyTo(field, condition){
  93. var condition = (arguments.length > 1) ? "\"" + arguments[1] + "\"" : null;
  94. var otherField = this.qForm._pointer + "['" + field + "']";
  95. if( !eval(otherField) ) return alert("The " + field + " field does not exist. The dependency \nto " + this.name + " can not be created.");
  96. // add an onblur event so that when the field is updated, the requested field
  97. // is updated with the value
  98. if( this.qForm[field]._queue.dependencies.length == 0 ) this.qForm[field].addEvent(_getEventType(this.qForm[field].type), otherField + ".enforceDependency();", false);
  99. this.qForm[field]._queue.dependencies[this.qForm[field]._queue.dependencies.length] = otherField + ".isDependent('" + this.name + "', " + condition + ");";
  100. return true;
  101. }
  102. Field.prototype.createDependencyTo = _Field_createDependencyTo;
  103.  
  104. // *this is an internal method that should only be used by the API*
  105. // define Field isDependent(); prototype
  106. function _Field_isDependent(field, condition){
  107. var condition = _param(arguments[1], null);
  108. this.value = this.getValue();
  109.  
  110. // if the current field is empty or not equal to the specified value, then the
  111. // dependent field is not required, otherwise the dependency is enforced
  112. if( condition == null ){
  113. var result = (this.isNotEmpty() || this.required);
  114. } else {
  115. // if there's a space in the condition, assume you're to evaluate the string
  116. if( condition.indexOf("this.") > -1 || condition == "true" || condition == "false" ){
  117. var result = eval(condition);
  118. // otherwise, you're doing a simple value compare
  119. } else {
  120. var result = (this.value == condition);
  121. }
  122. }
  123. // return both the field and the result
  124. var o = null;
  125. o = new Object();
  126. o.field = field;
  127. o.result = result;
  128. return o;
  129. }
  130. Field.prototype.isDependent = _Field_isDependent;
  131.  
  132. // *this is an internal method that should only be used by the API*
  133. // define Field enforceDependency(); prototype
  134. function _Field_enforceDependency(e){
  135. var lstExcludeFields = _param(arguments[0], ",");
  136. var lstFieldsChecked = ",";
  137. var lstFieldsRequired = ",";
  138. // loop through all the dependency and run each one
  139. for( var i=0; i < this._queue.dependencies.length; i++ ){
  140. var s = eval(this._queue.dependencies[i]);
  141. // keep a unique list of field checked
  142. if( lstFieldsChecked.indexOf("," + s.field + ",") == -1 ) lstFieldsChecked += s.field + ",";
  143. // keep a unique list of fields that now should be required
  144. if( s.result && lstFieldsRequired.indexOf("," + s.field + ",") == -1 ) lstFieldsRequired += s.field + ",";
  145. }
  146. // create an array of the field checked
  147. aryFieldsChecked = lstFieldsChecked.split(",");
  148. // loop through the array skipping the first and last elements
  149. for( var j=1; j < aryFieldsChecked.length-1; j++ ){
  150. // determine if the field is required
  151. var result = (lstFieldsRequired.indexOf("," + aryFieldsChecked[j] + ",") > -1);
  152. // update it's status
  153. this.qForm[aryFieldsChecked[j]].required = result;
  154. // now go check the dependencies for the field whose required status was changed
  155. // if the dependency rules for the field have already been run, then don't run
  156. // them again
  157. if( lstExcludeFields.indexOf("," + aryFieldsChecked[j] + ",") == -1 ) setTimeout(this.qForm._pointer + "." + aryFieldsChecked[j] + ".enforceDependency('" + lstExcludeFields + this.name + ",')", 1);
  158. }
  159. }
  160. Field.prototype.enforceDependency = _Field_enforceDependency;
  161.  
  162.  
  163. // define Field location(); prototype
  164. function _Field_location(target, key){
  165. var target = _param(arguments[0], "self");
  166. var key = _param(arguments[1]);
  167. // if the current field is disabled or locked, then kill the method
  168. if( this.isLocked() || this.isDisabled() ) return this.setValue(key, null, false);
  169.  
  170. var value = this.getValue();
  171. this.setValue(key, null, false);
  172. // if the value isn't equal to the key, then don't relocate the user
  173. if( value != key ) eval(target + ".location = value");
  174.  
  175. return true;
  176. }
  177. Field.prototype.location = _Field_location;
  178.  
  179. // define Field format(); prototype
  180. function _Field_format(mask, type){
  181. var type = _param(arguments[1], "numeric").toLowerCase();
  182. this.validate = true;
  183. this.validateFormat(mask, type);
  184. }
  185. Field.prototype.format = _Field_format;
  186.  
  187.  
  188. // define Field populate(); prototype
  189. function _Field_populate(struct, reset, sort, prefix){
  190. // if the current field is disabled or locked, then kill the method
  191. if( this.isLocked() || this.isDisabled() ) return false;
  192.  
  193. var reset = _param(arguments[1], true, "boolean");
  194. var sort = _param(arguments[2], false, "boolean");
  195. var prefix = _param(arguments[3], null, "object");
  196.  
  197. if( this.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes.");
  198.  
  199. // clear the select box
  200. if( reset ) this.obj.length = 0;
  201.  
  202. // if prefixing options
  203. if( !!prefix ) for( key in prefix ) this.obj.options[this.obj.length] = new Option(prefix[key], key);
  204.  
  205. // populate the select box
  206. for( key in struct ) this.obj.options[this.obj.length] = new Option(struct[key], key);
  207.  
  208. // if the user wishes to sort the options in the select box
  209. if( sort ) _sortOptions(this.obj);
  210. return true;
  211. }
  212. Field.prototype.populate = _Field_populate;
  213.  
  214. // define Field transferTo(); prototype
  215. function _Field_transferTo(field, sort, type, selectItems, reset){
  216. // if the current field is disabled or locked, then kill the method
  217. if( this.isLocked() || this.isDisabled() ) return false;
  218. var sort = _param(arguments[1], true, "boolean");
  219. var type = _param(arguments[2], "selected");
  220. var selectItems = _param(arguments[3], true, "boolean");
  221. var reset = _param(arguments[4], false, "boolean");
  222.  
  223. _transferOptions(this.obj, this.qForm[field].obj, sort, type, selectItems, reset);
  224. return true;
  225. }
  226. Field.prototype.transferTo = _Field_transferTo;
  227.  
  228. // define Field transferFrom(); prototype
  229. function _Field_transferFrom(field, sort, type, selectItems, reset){
  230. // if the current field is disabled or locked, then kill the method
  231. if( this.isLocked() || this.isDisabled() ) return false;
  232. var sort = _param(arguments[1], true, "boolean");
  233. var type = _param(arguments[2], "selected");
  234. var selectItems = _param(arguments[3], true, "boolean");
  235. var reset = _param(arguments[4], false, "boolean");
  236.  
  237. _transferOptions(this.qForm[field].obj, this.obj, sort, type, selectItems, reset);
  238. return true;
  239. }
  240. Field.prototype.transferFrom = _Field_transferFrom;
  241.  
  242. // define Field moveUp(); prototype
  243. function _Field_moveUp(){
  244. // if the current field is disabled or locked, then kill the method
  245. if( this.isLocked() || this.isDisabled() || this.type.substring(0,6) != "select" ) return false;
  246.  
  247. var oOptions = this.obj.options;
  248. // rearrange
  249. for( var i=1; i < oOptions.length; i++ ){
  250. // swap options
  251. if( oOptions[i].selected ){
  252. _swapOptions(oOptions[i], oOptions[i-1]);
  253. }
  254. }
  255. return true;
  256. }
  257. Field.prototype.moveUp = _Field_moveUp;
  258.  
  259. // define Field moveDown(); prototype
  260. function _Field_moveDown(){
  261. // if the current field is disabled or locked, then kill the method
  262. if( this.isLocked() || this.isDisabled() || this.type.substring(0,6) != "select" ) return false;
  263.  
  264. var oOptions = this.obj.options;
  265. // rearrange
  266. for( var i=oOptions.length-2; i > -1; i-- ){
  267. // swap options
  268. if( oOptions[i].selected ){
  269. _swapOptions(oOptions[i+1], oOptions[i]);
  270. }
  271. }
  272. return true;
  273. }
  274. Field.prototype.moveDown = _Field_moveDown;
  275.  
  276.  
/******************************************************************************
qForm JSAPI: Cookie Library

Author: Dan G. Switzer, II
Build: 109
******************************************************************************/

+добавить реализацию
 
каталог | задачи | паттерны | исходники | стат | форумы | карта сайта | контакты | ссылки 
© 2000-2020 CodeLAB Group
  Все права защищены
Страница сгенерирована за 0.00516 секунд
Количество запросов к БД: 7, gzip: 8.4kb/67.1kb(88%)