Guest User

Untitled

a guest
Jun 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. CxOpenListOptionFactory = {
  2. make: function(src)
  3. {
  4. text = src.value;
  5. if(src.attributes['optval'])
  6. {
  7. value = src.attributes['optval'].value;
  8. opt = new Option(text, value);
  9. return opt;
  10. }
  11. return;
  12. },
  13. makeTag: function(src) {
  14. tag = src.value.toLowerCase();
  15. if(tag){
  16. opt = new Option(src.value.toLowerCase());
  17. return opt;
  18. }
  19. return;
  20. }
  21. }
  22.  
  23. CxOpenSelect = function(className, srcId, destId, jsonUrl, config, optionFactory)
  24. {
  25. this.src = document.getElementById(srcId);
  26. this.dest = document.getElementById(destId);
  27. this.optionFactory = optionFactory;
  28.  
  29. instance = this;
  30.  
  31. this.getForm = function(el)
  32. {
  33. if ("form" != el.tagName.toLowerCase())
  34. {
  35. return this.getForm(el.parentNode);
  36. }
  37. return el;
  38. }
  39.  
  40. this.add = function()
  41. {
  42. var opt = this.optionFactory(this.src);
  43.  
  44. // exit if option is null
  45. if(!opt) return;
  46.  
  47. this.src.value = '';
  48.  
  49. //try{ delete this.src.attributes['optval'];} catch(e) {}
  50. delete this.src.attributes.optval;
  51.  
  52. // exit if option already exists in list
  53. for (var i = 0; i < this.dest.options.length; i++)
  54. {
  55. if (this.dest.options[i].value == opt.value) return;
  56. }
  57.  
  58. this.dest.options[this.dest.options.length] = opt;
  59. }
  60.  
  61. this.remove = function()
  62. {
  63.  
  64. for (var i = 0; i < this.dest.options.length; i++)
  65. {
  66. if (this.dest.options[i].selected)
  67. {
  68. this.dest.options[i] = null;
  69. --i;
  70. }
  71. }
  72. }
  73.  
  74. this.submit = function()
  75. {
  76. for (var j = 0; j < this.dest.options.length; j++)
  77. {
  78. this.dest.options[j].selected = true;
  79. }
  80. return true;
  81. }
  82.  
  83.  
  84. jQuery('#'+srcId)
  85. .autocomplete(jsonUrl, jQuery.extend({}, {
  86. dataType: 'json',
  87. parse: function(data) {
  88. var parsed = [];
  89. for (key in data) {
  90. parsed[parsed.length] = { data: [ data[key], key ], value: data[key], result: data[key] };
  91. }
  92. return parsed;
  93. }
  94. }, config))
  95. .bind("keypress",
  96. {instance: instance},
  97. function(e) {
  98. // check return key
  99. if (e.keyCode == 13)
  100. {
  101. e.data.instance.add();
  102. return false;
  103. }
  104. })
  105. .bind("change",
  106. function(e) {
  107. jQuery(this).removeAttr('optval');
  108. }
  109. )
  110. .result(
  111. function(event, data) {
  112. jQuery(this).attr('optval', data[1]);
  113. }
  114. );
  115.  
  116. jQuery('#' + srcId).parents('.' + className).find('.' + className + "_add")
  117. .bind("click",
  118. {handler: this},
  119. function(e) {
  120. e.data.handler.add();
  121. });
  122.  
  123. jQuery('#' + srcId).parents('.' + className).find('.' + className + "_remove")
  124. .bind("click",
  125. {handler: this},
  126. function(e) {
  127. e.data.handler.remove();
  128. });
  129.  
  130. jQuery(this.dest.form)
  131. .bind("submit",
  132. {handler: this},
  133. function(e) {
  134. e.data.handler.submit();
  135. });
  136. }
Add Comment
Please, Sign In to add comment