PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/blocks/cocoon-forms/cocoon-forms-impl/src/main/resources/org/apache/cocoon/forms/resources/js/MultiValueDoubleList.js

https://github.com/apache/cocoon
JavaScript | 349 lines | 284 code | 30 blank | 35 comment | 101 complexity | e34149bc0c6195fd3746a8126da45867 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. dojo.require("dojo.widget.HtmlWidget");
  18. dojo.provide("cocoon.forms.MultiValueDoubleList");
  19. /**
  20. * A free-entry multivalue field editor style double list.
  21. *
  22. * Some functionality that's not visible at first sight:
  23. * - items can be moved around using ctrl+up and ctrl+down.
  24. * - an item can be replaced/updated by pressing ctrl+enter in the input box
  25. */
  26. dojo.widget.defineWidget(
  27. // widget name and class
  28. "cocoon.forms.MultiValueDoubleList",
  29. // superclass
  30. dojo.widget.HtmlWidget,
  31. function() {
  32. },
  33. // properties and methods
  34. {
  35. isContainer: false,
  36. cformsIdPrefix: "id-prefix-not-set",
  37. resourcesUri: cocoon.formsResourcesUri,
  38. templatePath: cocoon.formsResourcesUri + "/js/templates/MultiValueDoubleList.html",
  39. availableListLabel : "Available",
  40. selectedListLabel : "Selected",
  41. size: "5",
  42. styleClass : "",
  43. fillInTemplate: function(args, frag) {
  44. cocoon.forms.MultiValueDoubleList.superclass.fillInTemplate(this, args, frag);
  45. // Available list options.
  46. this.selectLeft.addOption = function(value, text) {
  47. var alreadyInList = false;
  48. for (var i = 0; this.options.length > i && !alreadyInList; i++) {
  49. if (this.options[i].value == value) {
  50. alreadyInList = true;
  51. }
  52. }
  53. if (!alreadyInList) {
  54. this.options[this.options.length] = new Option(text, value);
  55. }
  56. }
  57. // Selected list options.
  58. this.selectRight.addOption = function(value, text) {
  59. var alreadyInList = false;
  60. for (var i = 0; this.options.length > i && !alreadyInList; i++) {
  61. if (this.options[i].value == value) {
  62. alreadyInList = true;
  63. }
  64. }
  65. if (!alreadyInList) {
  66. this.options[this.options.length] = new Option(text, value);
  67. }
  68. }
  69. dojo.event.connect(this.selectRight, "ondblclick", this, "_transferLeft");
  70. dojo.event.connect(this.transferRight, "onclick", this, "_transferRight");
  71. dojo.event.connect(this.selectLeft, "ondblclick", this, "_transferRight");
  72. dojo.event.connect(this.transferAllRight, "onclick", this, "_transferAllRight");
  73. dojo.event.connect(this.transferLeft, "onclick", this, "_transferLeft");
  74. dojo.event.connect(this.transferAllLeft, "onclick", this, "_transferAllLeft");
  75. dojo.event.connect(this, "addedTo", this, "_addOnSubmitHandler");
  76. this._readData(this.getFragNodeRef(frag));
  77. },
  78. _addOnSubmitHandler: function(parent) {
  79. var form = this._getForm(this);
  80. if (form != null) {
  81. dojo.event.connect("before", form,"onsubmit", this, "_selectAll");
  82. } else {
  83. dojo.debug("MultiValueDoubleList is not being added to a form -- no onSubmitHandler then.");
  84. }
  85. },
  86. /**
  87. * Finds the HTML form to which a widget belongs.
  88. * The widget is passed as an argument since this might become a generic
  89. * utility function outside of this wiget.
  90. */
  91. _getForm: function(widget) {
  92. do {
  93. if (widget.domNode != null && widget.domNode.tagName != null && widget.domNode.tagName.toLowerCase() == "form") {
  94. return widget.domNode;
  95. }
  96. widget = widget.parent;
  97. } while (widget != null)
  98. return null;
  99. },
  100. _readData: function(origFrag) {
  101. // Read data from available list options.
  102. var table = dojo.dom.getFirstChildElement(origFrag, "table");
  103. if (table != null) {
  104. var tbody = dojo.dom.firstElement(table, "tbody");
  105. if (tbody != null) {
  106. var tr = dojo.dom.firstElement(tbody, "tr");
  107. while (tr != null) {
  108. var td = dojo.dom.firstElement(tr, "td");
  109. var value = td != null ? dojo.dom.textContent(td) : null;
  110. var text = td.nextSibling != null ? dojo.dom.textContent(td.nextSibling) : value;
  111. if (value && text) {
  112. this.selectLeft.addOption(value, text);
  113. }
  114. tr = dojo.dom.nextElement(tr, "tr");
  115. }
  116. }
  117. // Read data from selected list options.
  118. var table1 = dojo.dom.getNextSiblingElement(table, "table");
  119. if (table1 != null) {
  120. var tbody1 = dojo.dom.firstElement(table1, "tbody");
  121. if (tbody1 != null) {
  122. var tr1 = dojo.dom.firstElement(tbody1, "tr");
  123. while (tr1 != null) {
  124. var td1 = dojo.dom.firstElement(tr1, "td");
  125. var value1 = td1 != null ? dojo.dom.textContent(td1) : null;
  126. var text1 = td1.nextSibling != null ? dojo.dom.textContent(td1.nextSibling) : value1;
  127. if (value1 && text1) {
  128. this.selectRight.addOption(value1, text1);
  129. }
  130. tr1 = dojo.dom.nextElement(tr1, "tr");
  131. }
  132. }
  133. }
  134. } else {
  135. dojo.debug("MultiValueDoubleList: no data table found");
  136. }
  137. },
  138. _selectAll: function() {
  139. this._selectAllOptions(this.selectRight);
  140. },
  141. _transferLeft: function (event) {
  142. dojo.event.browser.stopEvent(event);
  143. this._moveSelectedOptions(this.selectRight, this.selectLeft);
  144. this._update();
  145. },
  146. _transferRight: function (event) {
  147. dojo.event.browser.stopEvent(event);
  148. this._moveSelectedOptions(this.selectLeft, this.selectRight);
  149. this._update();
  150. },
  151. _transferAllLeft: function (event) {
  152. dojo.event.browser.stopEvent(event);
  153. this._moveAllOptions(this.selectRight, this.selectLeft);
  154. this._update();
  155. },
  156. _transferAllRight: function (event) {
  157. dojo.event.browser.stopEvent(event);
  158. this._moveAllOptions(this.selectLeft, this.selectRight);
  159. this._update();
  160. },
  161. _update: function() {
  162. var removedLeft = new Object();
  163. var removedRight = new Object();
  164. var addedLeft = new Object();
  165. var addedRight = new Object();
  166. var newLeft = new Object();
  167. var newRight = new Object();
  168. var originalLeftValues = new Object();
  169. var originalRightValues = new Object();
  170. var delimiter = ",";
  171. var right = this.selectRight;
  172. var left = this.selectLeft;
  173. var removedLeftField = null;
  174. var removedRightField = null;
  175. var addedLeftField = null;
  176. var addedRightField = null;
  177. var newLeftField = null;
  178. var newRightField = null;
  179. for (var i = 0; i < left.options.length; i++) {
  180. var o = left.options[i];
  181. newLeft[o.value] = 1;
  182. if (typeof(originalLeftValues[o.value]) == "undefined") {
  183. addedLeft[o.value] = 1;
  184. removedRight[o.value] = 1;
  185. }
  186. }
  187. for (var i = 0; i < right.options.length; i++) {
  188. var o = right.options[i];
  189. newRight[o.value] = 1;
  190. if (typeof(originalRightValues[o.value]) == "undefined") {
  191. addedRight[o.value] = 1;
  192. removedLeft[o.value] = 1;
  193. }
  194. }
  195. if (removedLeftField != null) {
  196. removedLeftField.value = this._join(removedLeft, delimiter);
  197. }
  198. if (removedRightField != null) {
  199. removedRightField.value = this._join(removedRight, delimiter);
  200. }
  201. if (addedLeftField != null) {
  202. addedLeftField.value = this._join(addedLeft, delimiter);
  203. }
  204. if (addedRightField != null) {
  205. addedRightField.value = this._join(addedRight, delimiter);
  206. }
  207. if (newLeftField != null) {
  208. newLeftField.value = this._join(newLeft, delimiter);
  209. }
  210. if (newRightField != null) {
  211. newRightField.value = this._join(newRight, delimiter);
  212. }
  213. },
  214. _moveSelectedOptions: function (from, to) {
  215. if (arguments.length > 3) {
  216. var regex = arguments[3];
  217. if(regex != "") {
  218. this._unSelectMatchingOptions(from,regex);
  219. }
  220. }
  221. for(var i = 0; i < from.options.length; i++) {
  222. var o = from.options[i];
  223. if(o.selected) {
  224. to.options[to.options.length] = new Option( o.text, o.value, true, o.selected);
  225. }
  226. }
  227. for (var i = (from.options.length-1); i >= 0; i--) {
  228. var o = from.options[i];
  229. if(o.selected) {
  230. from.options[i] = null;
  231. }
  232. }
  233. if ((arguments.length < 3) || (arguments[2] == true)) {
  234. this._sortSelect(from);
  235. this._sortSelect(to);
  236. }
  237. from.selectedIndex = -1;
  238. to.selectedIndex = -1;
  239. },
  240. _moveAllOptions: function (from, to) {
  241. this._selectAllOptions(from);
  242. if(arguments.length == 2) {
  243. this._moveSelectedOptions(from, to);
  244. } else if(arguments.length == 3) {
  245. this._moveSelectedOptions(from, to, arguments[2]);
  246. } else if(arguments.length == 4) {
  247. this._moveSelectedOptions(from, to, arguments[2], arguments[3]);
  248. }
  249. },
  250. _selectAllOptions: function (obj) {
  251. for(var i = 0; i < obj.options.length; i++) {
  252. obj.options[i].selected = true;
  253. }
  254. },
  255. _unSelectMatchingOptions: function (obj, regex) {
  256. this._selectUnselectMatchingOptions(obj, regex, "unselect", false);
  257. },
  258. _selectUnselectMatchingOptions: function(obj, regex, which, only) {
  259. if (window.RegExp) {
  260. var selected1;
  261. var selected2;
  262. if(which == "select"){
  263. selected1 = true;
  264. selected2 = false;
  265. } else if (which == "unselect") {
  266. selected1 = false;
  267. selected2 = true;
  268. } else {
  269. return;
  270. }
  271. var re = new RegExp(regex);
  272. for (var i = 0;i < obj.options.length; i++) {
  273. if(re.test(obj.options[i].text)) {
  274. obj.options[i].selected = selected1;
  275. } else {
  276. if(only == true) {
  277. obj.options[i].selected = selected2;
  278. }
  279. }
  280. }
  281. }
  282. },
  283. _join: function(o, delimiter) {
  284. var val;
  285. var str="";
  286. for(val in o) {
  287. if (str.length > 0) {
  288. str = str + delimiter;
  289. }
  290. str = str + val;
  291. }
  292. return str;
  293. },
  294. _sortSelect: function(obj) {
  295. var o = new Array();
  296. if(obj.options == null) {
  297. return;
  298. }
  299. for(var i = 0; i < obj.options.length; i++) {
  300. o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
  301. }
  302. if (o.length == 0) {
  303. return;
  304. }
  305. o = o.sort( function(a,b) {
  306. if ((a.text + "") < (b.text + "")) {
  307. return -1;
  308. }
  309. if ((a.text + "") > (b.text + "")) {
  310. return 1;
  311. }
  312. return 0;
  313. } );
  314. for(var i = 0; i < o.length; i++) {
  315. obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
  316. }
  317. }
  318. }
  319. );