You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

153 lines
4.6 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. ListSelector = function(title, intro, list, onupdate, selectable) {
  23. var me = this; this.list = list; this.selectable = selectable;
  24. this.dialog = new Dialog(400,600,title);
  25. this.items = [];
  26. // intro
  27. if(intro) {
  28. intro_area = $a(this.dialog.body, 'div', 'help_box', {margin:'16px', marginBottom:'0px', width:'312px'});
  29. intro_area.innerHTML = intro;
  30. }
  31. // body
  32. this.body = $a(this.dialog.body, 'div', '', {margin:'16px', position: 'relative'});
  33. // items
  34. this.render();
  35. // ---------------------------------------
  36. // update button
  37. var btn = $btn(this.dialog.body, 'Update', function() { me.update() }, {margin:'0px 0px 16px 16px'}, 'green', 1);
  38. this.update = function() {
  39. if(me.selected_item) $bg(me.selected_item, '#FFF');
  40. var ret = [];
  41. for(var i=0; i<me.items.length; i++) {
  42. // [label, idx, selected, det]
  43. ret.push([me.items[i].label, me.items[i].idx, (me.items[i].check ? (me.items[i].check.checked ? 1 : 0) : 0), me.items[i].det]);
  44. }
  45. me.dialog.hide();
  46. // call the user with (done!)
  47. onupdate(ret);
  48. }
  49. this.dialog.show();
  50. }
  51. // ---------------------------------------
  52. ListSelector.prototype.render = function(to_hide) {
  53. this.body.innerHTML = ''; this.items = [];
  54. // sort
  55. this.list.sort(function(a,b) { return a[1] > b[1]; });
  56. // items
  57. for(i=0; i<this.list.length; i++) {
  58. // make checkbox for if selectable
  59. this.list[i][1]=i;
  60. this.items.push(new ListSelectorItem(this, this.list[i], i));
  61. if(i==to_hide) $dh(this.items[i].body);
  62. }
  63. }
  64. // ---------------------------------------
  65. ListSelector.prototype.insert_at = function(item, new_idx) {
  66. for(var i=0;i<this.list.length; i++) {
  67. if(this.list[i][1] >= new_idx) this.list[i][1]++;
  68. }
  69. for(var i=0;i<this.list.length; i++) {
  70. if(this.list[i][1] >= item.idx) this.list[i][1]--;
  71. }
  72. this.list[item.idx][1] = new_idx;
  73. var n = new_idx - ((new_idx > item.idx) ? 1 : 0);
  74. this.render(n);
  75. this.items[n].body.onmousedown();
  76. $(this.items[n].body).slideDown();
  77. }
  78. // =====================================
  79. ListSelectorItem = function(ls, det, idx) {
  80. this.det = det; this.ls = ls; this.idx = idx;
  81. this.body = $a(ls.body, 'div', '', {padding: '8px', margin:'4px 0px',
  82. border:'1px solid #AAA', position: 'relative', width:'320px', height:'14px', cursor:'move'});
  83. if(ls.selectable) {
  84. // with checkbox
  85. this.make_with_checkbox();
  86. } else {
  87. // no checkbox (only label)
  88. this.body.innerHTML = det[0];
  89. }
  90. this.set_drag();
  91. }
  92. // ---------------------------------------
  93. ListSelectorItem.prototype.make_with_checkbox = function() {
  94. this.body.tab = make_table(this.body, 1, 2, null, ['28px',null], {verticalAlign:'top'});
  95. this.check = $a_input($td(this.body.tab, 0, 0), 'checkbox');
  96. if(this.det[2]) this.check.checked = 1;
  97. $td(this.body.tab, 0, 1).innerHTML = this.det[0];
  98. }
  99. // ---------------------------------------
  100. ListSelectorItem.prototype.set_drag = function() {
  101. var me = this;
  102. this.body.item = this;
  103. // color on mousedown
  104. this.body.onmousedown = function() {
  105. $bg(this, '#FFC');
  106. if(me.ls.selected_item && me.ls.selected_item != this) $bg(me.ls.selected_item, '#FFF');
  107. me.ls.selected_item = this;
  108. }
  109. // setup draggable
  110. $(this.body).draggable({
  111. opacity: 0.6,
  112. helper: 'clone',
  113. containment: 'parent',
  114. scroll: false,
  115. cursor: 'move',
  116. drag:function(event, ui){
  117. me.ls.drag_item = this.item;
  118. }
  119. });
  120. $(this.body).droppable({
  121. drop: function(event, ui) {
  122. me.ls.insert_at(me.ls.drag_item, me.idx + (me.ls.drag_item.idx < me.idx ? 1 : 0));
  123. }
  124. });
  125. }