035fc32ebdbd6d73aca598dd9cb4fea751b7cfca.svn-base 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.onReady(function() {
  7. Ext.QuickTips.init();
  8. // Album toolbar
  9. var newIndex = 3;
  10. var tb = new Ext.Toolbar({
  11. items : [{
  12. text : 'New Album',
  13. iconCls : 'album-btn',
  14. handler : function() {
  15. var node = root
  16. .appendChild(new Ext.tree.TreeNode({
  17. text : 'Album '
  18. + (++newIndex),
  19. cls : 'album-node',
  20. allowDrag : false
  21. }));
  22. tree.getSelectionModel().select(node);
  23. setTimeout(function() {
  24. ge.editNode = node;
  25. ge.startEdit(node.ui.textNode);
  26. }, 10);
  27. }
  28. }]
  29. });
  30. // set up the Album tree
  31. var tree = new Ext.tree.TreePanel({
  32. // tree
  33. animate : true,
  34. enableDD : true,
  35. containerScroll : true,
  36. ddGroup : 'organizerDD',
  37. rootVisible : false,
  38. // layout
  39. region : 'west',
  40. width : 200,
  41. split : true,
  42. // panel
  43. title : 'My Albums',
  44. autoScroll : true,
  45. tbar : tb,
  46. margins : '5 0 5 5'
  47. });
  48. var root = new Ext.tree.TreeNode({
  49. text : 'Albums',
  50. allowDrag : false,
  51. allowDrop : false
  52. });
  53. tree.setRootNode(root);
  54. root.appendChild(new Ext.tree.TreeNode({
  55. text : 'Album 1',
  56. cls : 'album-node',
  57. allowDrag : false
  58. }), new Ext.tree.TreeNode({
  59. text : 'Album 2',
  60. cls : 'album-node',
  61. allowDrag : false
  62. }), new Ext.tree.TreeNode({
  63. text : 'Album 3',
  64. cls : 'album-node',
  65. allowDrag : false
  66. }));
  67. // add an inline editor for the nodes
  68. var ge = new Ext.tree.TreeEditor(tree, {
  69. allowBlank : false,
  70. blankText : 'A name is required',
  71. selectOnFocus : true
  72. });
  73. // Set up images view
  74. var view = new Ext.DataView({
  75. itemSelector : 'div.thumb-wrap',
  76. style : 'overflow:auto',
  77. multiSelect : true,
  78. plugins : new Ext.DataView.DragSelector({
  79. dragSafe : true
  80. }),
  81. store : new Ext.data.JsonStore({
  82. url : '../view/get-images.php',
  83. autoLoad : true,
  84. root : 'images',
  85. id : 'name',
  86. fields : ['name', 'url', {
  87. name : 'shortName',
  88. mapping : 'name',
  89. convert : shortName
  90. }]
  91. }),
  92. tpl : new Ext.XTemplate(
  93. '<tpl for=".">',
  94. '<div class="thumb-wrap" id="{name}">',
  95. '<div class="thumb"><img src="../view/{url}" class="thumb-img"></div>',
  96. '<span>{shortName}</span></div>', '</tpl>')
  97. });
  98. var images = new Ext.Panel({
  99. id : 'images',
  100. title : 'My Images',
  101. region : 'center',
  102. margins : '5 5 5 0',
  103. layout : 'fit',
  104. items : view
  105. });
  106. var layout = new Ext.Panel({
  107. layout : 'border',
  108. width : 650,
  109. height : 400,
  110. items : [tree, images]
  111. });
  112. layout.render('layout');
  113. var dragZone = new ImageDragZone(view, {
  114. containerScroll : true,
  115. ddGroup : 'organizerDD'
  116. });
  117. });
  118. /**
  119. * Create a DragZone instance for our JsonView
  120. */
  121. ImageDragZone = function(view, config) {
  122. this.view = view;
  123. ImageDragZone.superclass.constructor.call(this, view.getEl(), config);
  124. };
  125. Ext.extend(ImageDragZone, Ext.dd.DragZone, {
  126. // We don't want to register our image elements, so let's
  127. // override the default registry lookup to fetch the image
  128. // from the event instead
  129. getDragData : function(e) {
  130. var target = e.getTarget('.thumb-wrap');
  131. if (target) {
  132. var view = this.view;
  133. if (!view.isSelected(target)) {
  134. view.onClick(e);
  135. }
  136. var selNodes = view.getSelectedNodes();
  137. var dragData = {
  138. nodes : selNodes
  139. };
  140. if (selNodes.length == 1) {
  141. dragData.ddel = target.firstChild.firstChild; // the
  142. // img
  143. // element
  144. dragData.single = true;
  145. } else {
  146. var div = document.createElement('div'); // create
  147. // the multi
  148. // element
  149. // drag
  150. // "ghost"
  151. div.className = 'multi-proxy';
  152. for (var i = 0, len = selNodes.length; i < len; i++) {
  153. div.appendChild(selNodes[i].firstChild.firstChild
  154. .cloneNode(true));
  155. if ((i + 1) % 3 == 0) {
  156. div.appendChild(document.createElement('br'));
  157. }
  158. }
  159. dragData.ddel = div;
  160. dragData.multi = true;
  161. }
  162. return dragData;
  163. }
  164. return false;
  165. },
  166. // this method is called by the TreeDropZone after a node drop
  167. // to get the new tree node (there are also other way, but this is
  168. // easiest)
  169. getTreeNode : function() {
  170. var treeNodes = [];
  171. var nodeData = this.view.getRecords(this.dragData.nodes);
  172. for (var i = 0, len = nodeData.length; i < len; i++) {
  173. var data = nodeData[i].data;
  174. treeNodes.push(new Ext.tree.TreeNode({
  175. text : data.name,
  176. icon : '../view/' + data.url,
  177. data : data,
  178. leaf : true,
  179. cls : 'image-node'
  180. }));
  181. }
  182. return treeNodes;
  183. },
  184. // the default action is to "highlight" after a bad drop
  185. // but since an image can't be highlighted, let's frame it
  186. afterRepair : function() {
  187. for (var i = 0, len = this.dragData.nodes.length; i < len; i++) {
  188. Ext.fly(this.dragData.nodes[i]).frame('#8db2e3', 1);
  189. }
  190. this.dragging = false;
  191. },
  192. // override the default repairXY with one offset for the margins and
  193. // padding
  194. getRepairXY : function(e) {
  195. if (!this.dragData.multi) {
  196. var xy = Ext.Element.fly(this.dragData.ddel).getXY();
  197. xy[0] += 3;
  198. xy[1] += 3;
  199. return xy;
  200. }
  201. return false;
  202. }
  203. });
  204. // Utility functions
  205. function shortName(name) {
  206. if (name.length > 15) {
  207. return name.substr(0, 12) + '...';
  208. }
  209. return name;
  210. };