dependency.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. var xt = Ext.tree;
  8. // seeds for the new node suffix
  9. var cseed = 0, oseed = 0;
  10. // turn on quick tips
  11. Ext.QuickTips.init();
  12. var cview = Ext.DomHelper.append('main-ct', {
  13. cn : [{
  14. id : 'main-tb'
  15. }, {
  16. id : 'cbody'
  17. }]
  18. });
  19. // create the primary toolbar
  20. var tb = new Ext.Toolbar('main-tb');
  21. tb.add({
  22. id : 'save',
  23. text : 'Save',
  24. disabled : true,
  25. handler : save,
  26. cls : 'x-btn-text-icon save',
  27. tooltip : 'Saves all components to the server'
  28. }, '-', {
  29. id : 'add',
  30. text : 'Component',
  31. handler : addComponent,
  32. cls : 'x-btn-text-icon add-cmp',
  33. tooltip : 'Add a new Component to the dependency builder'
  34. }, {
  35. id : 'option',
  36. text : 'Option',
  37. disabled : true,
  38. handler : addOption,
  39. cls : 'x-btn-text-icon add-opt',
  40. tooltip : 'Add a new optional dependency to the selected component'
  41. }, '-', {
  42. id : 'remove',
  43. text : 'Remove',
  44. disabled : true,
  45. handler : removeNode,
  46. cls : 'x-btn-text-icon remove',
  47. tooltip : 'Remove the selected item'
  48. });
  49. // for enabling and disabling
  50. var btns = tb.items.map;
  51. // create our layout
  52. var layout = new Ext.BorderLayout('main-ct', {
  53. west : {
  54. split : true,
  55. initialSize : 200,
  56. minSize : 175,
  57. maxSize : 400,
  58. titlebar : true,
  59. margins : {
  60. left : 5,
  61. right : 0,
  62. bottom : 5,
  63. top : 5
  64. }
  65. },
  66. center : {
  67. title : 'Components',
  68. margins : {
  69. left : 0,
  70. right : 5,
  71. bottom : 5,
  72. top : 5
  73. }
  74. }
  75. }, 'main-ct');
  76. layout.batchAdd({
  77. west : {
  78. id : 'source-files',
  79. autoCreate : true,
  80. title : 'Ext Source Files',
  81. autoScroll : true,
  82. fitToFrame : true
  83. },
  84. center : {
  85. el : cview,
  86. autoScroll : true,
  87. fitToFrame : true,
  88. toolbar : tb,
  89. resizeEl : 'cbody'
  90. }
  91. });
  92. // this is the source code tree
  93. var stree = new xt.TreePanel('source-files', {
  94. animate : true,
  95. loader : new xt.TreeLoader({
  96. dataUrl : 'dependency.php'
  97. }),
  98. enableDrag : true,
  99. containerScroll : true
  100. });
  101. new xt.TreeSorter(stree, {
  102. folderSort : true
  103. });
  104. var sroot = new xt.AsyncTreeNode({
  105. text : 'Ext JS',
  106. draggable : false,
  107. id : 'source'
  108. });
  109. stree.setRootNode(sroot);
  110. stree.render();
  111. sroot.expand(false, false);
  112. // the component tree
  113. var ctree = new xt.TreePanel('cbody', {
  114. animate : true,
  115. enableDD : true,
  116. containerScroll : true,
  117. lines : false,
  118. rootVisible : false,
  119. loader : new Ext.tree.TreeLoader()
  120. });
  121. ctree.el.addKeyListener(Ext.EventObject.DELETE, removeNode);
  122. var croot = new xt.AsyncTreeNode({
  123. allowDrag : false,
  124. allowDrop : true,
  125. id : 'croot',
  126. text : 'Packages and Components',
  127. cls : 'croot',
  128. loader : new Ext.tree.TreeLoader({
  129. dataUrl : 'dep-tree.json',
  130. createNode : readNode
  131. })
  132. });
  133. ctree.setRootNode(croot);
  134. ctree.render();
  135. croot.expand();
  136. // some functions to determine whether is not the drop is allowed
  137. function hasNode(t, n) {
  138. return (t.attributes.type == 'fileCt' && t.findChild('id', n.id))
  139. || (t.leaf === true && t.parentNode.findChild('id', n.id));
  140. };
  141. function isSourceCopy(e, n) {
  142. var a = e.target.attributes;
  143. return n.getOwnerTree() == stree
  144. && !hasNode(e.target, n)
  145. && ((e.point == 'append' && a.type == 'fileCt') || a.leaf === true);
  146. };
  147. function isReorder(e, n) {
  148. return n.parentNode == e.target.parentNode && e.point != 'append';
  149. };
  150. // handle drag over and drag drop
  151. ctree.on('nodedragover', function(e) {
  152. var n = e.dropNode;
  153. return isSourceCopy(e, n) || isReorder(e, n);
  154. });
  155. ctree.on('beforenodedrop', function(e) {
  156. var n = e.dropNode;
  157. // copy node from source tree
  158. if (isSourceCopy(e, n)) {
  159. var copy = new xt.TreeNode(Ext.apply({
  160. allowDelete : true,
  161. expanded : true
  162. }, n.attributes));
  163. copy.loader = undefined;
  164. if (e.target.attributes.options) {
  165. e.target = createOption(e.target, copy.text);
  166. // return false;
  167. }
  168. e.dropNode = copy;
  169. return true;
  170. }
  171. return isReorder(e, n);
  172. });
  173. ctree.on('contextmenu', prepareCtx);
  174. // track whether save is allowed
  175. ctree.on('append', trackSave);
  176. ctree.on('remove', trackSave);
  177. ctree.el.swallowEvent('contextmenu', true);
  178. ctree.el.on('keypress', function(e) {
  179. if (e.isNavKeyPress()) {
  180. e.stopEvent();
  181. }
  182. });
  183. // when the tree selection changes, enable/disable the toolbar buttons
  184. var sm = ctree.getSelectionModel();
  185. sm.on('selectionchange', function() {
  186. var n = sm.getSelectedNode();
  187. if (!n) {
  188. btns.remove.disable();
  189. btns.option.disable();
  190. return;
  191. }
  192. var a = n.attributes;
  193. btns.remove.setDisabled(!a.allowDelete);
  194. btns.option.setDisabled(!a.cmpId);
  195. });
  196. // create the editor for the component tree
  197. var ge = new xt.TreeEditor(ctree, {
  198. allowBlank : false,
  199. blankText : 'A name is required',
  200. selectOnFocus : true
  201. });
  202. ge.on('beforestartedit', function() {
  203. if (!ge.editNode.attributes.allowEdit) {
  204. return false;
  205. }
  206. });
  207. // add component handler
  208. function addComponent() {
  209. var id = guid('c-');
  210. var text = 'Component ' + (++cseed);
  211. var node = createComponent(id, text);
  212. node.expand(false, false);
  213. node.select();
  214. node.lastChild.ensureVisible();
  215. ge.triggerEdit(node);
  216. }
  217. function createComponent(id, text, cfiles, cdep, coptions) {
  218. var node = new xt.TreeNode({
  219. text : text,
  220. iconCls : 'cmp',
  221. cls : 'cmp',
  222. type : 'cmp',
  223. id : id,
  224. cmpId : id,
  225. allowDelete : true,
  226. allowEdit : true
  227. });
  228. croot.appendChild(node);
  229. var files = new xt.AsyncTreeNode({
  230. text : 'Files',
  231. allowDrag : false,
  232. allowDrop : true,
  233. iconCls : 'folder',
  234. type : 'fileCt',
  235. cmpId : id,
  236. allowDelete : false,
  237. children : cfiles || [],
  238. expanded : true
  239. });
  240. var dep = new xt.AsyncTreeNode({
  241. text : 'Dependencies',
  242. allowDrag : false,
  243. allowDrop : true,
  244. iconCls : 'folder',
  245. type : 'fileCt',
  246. cmpId : id,
  247. allowDelete : false,
  248. children : cdep || [],
  249. expanded : true,
  250. allowCopy : true
  251. });
  252. var options = new xt.AsyncTreeNode({
  253. text : 'Optional Dependencies',
  254. allowDrag : false,
  255. allowDrop : true,
  256. iconCls : 'folder',
  257. type : 'fileCt',
  258. options : true,
  259. cmpId : id,
  260. allowDelete : false,
  261. children : coptions || [],
  262. expanded : true,
  263. allowCopy : true
  264. });
  265. node.appendChild(files);
  266. node.appendChild(dep);
  267. node.appendChild(options);
  268. return node;
  269. }
  270. // remove handler
  271. function removeNode() {
  272. var n = sm.getSelectedNode();
  273. if (n && n.attributes.allowDelete) {
  274. ctree.getSelectionModel().selectPrevious();
  275. n.parentNode.removeChild(n);
  276. }
  277. }
  278. // add option handler
  279. function addOption() {
  280. var n = sm.getSelectedNode();
  281. if (n) {
  282. var node = createOption(n, 'Option' + (++oseed));
  283. node.select();
  284. ge.triggerEdit(node);
  285. }
  286. }
  287. function createOption(n, text) {
  288. var cnode = ctree.getNodeById(n.attributes.cmpId);
  289. var node = new xt.TreeNode({
  290. text : text,
  291. cmpId : cnode.id,
  292. iconCls : 'folder',
  293. type : 'fileCt',
  294. allowDelete : true,
  295. allowEdit : true,
  296. id : guid('o-')
  297. });
  298. cnode.childNodes[2].appendChild(node);
  299. cnode.childNodes[2].expand(false, false);
  300. return node;
  301. }
  302. // semi unique ids across edits
  303. function guid(prefix) {
  304. return prefix + (allGetServerTime().getTime());
  305. }
  306. function trackSave() {
  307. btns.save.setDisabled(!croot.hasChildNodes());
  308. }
  309. function storeChildren(cmp, n, name) {
  310. if (n.childrenRendered) {
  311. cmp[name] = [];
  312. n.eachChild(function(f) {
  313. cmp[name].push(f.attributes);
  314. });
  315. } else {
  316. cmp[name] = n.attributes.children || [];
  317. }
  318. }
  319. // save to the server in a format usable in PHP
  320. function save() {
  321. var ch = [];
  322. croot.eachChild(function(c) {
  323. var cmp = {
  324. text : c.text,
  325. id : c.id,
  326. options : []
  327. };
  328. storeChildren(cmp, c.childNodes[0], 'files');
  329. storeChildren(cmp, c.childNodes[1], 'dep');
  330. var onode = c.childNodes[2];
  331. if (!onode.childrenRendered) {
  332. cmp.options = onode.attributes.children || [];
  333. } else {
  334. onode.eachChild(function(o) {
  335. var opt = Ext.apply({}, o.attributes);
  336. storeChildren(opt, o, 'children');
  337. cmp.options.push(opt);
  338. });
  339. }
  340. ch.push(cmp);
  341. });
  342. layout.el.mask('Sending data to server...', 'x-mask-loading');
  343. var hide = layout.el.unmask.createDelegate(layout.el);
  344. Ext.lib.Ajax.request('POST', 'save-dep.php', {
  345. success : hide,
  346. failure : hide
  347. }, 'data=' + encodeURIComponent(Ext.encode(ch)));
  348. }
  349. function readNode(o) {
  350. createComponent(o.id, o.text, o.files, o.dep, o.options);
  351. }
  352. // context menus
  353. var ctxMenu = new Ext.menu.Menu({
  354. id : 'copyCtx',
  355. items : [{
  356. id : 'expand',
  357. handler : expandAll,
  358. cls : 'expand-all',
  359. text : 'Expand All'
  360. }, {
  361. id : 'collapse',
  362. handler : collapseAll,
  363. cls : 'collapse-all',
  364. text : 'Collapse All'
  365. }, '-', {
  366. id : 'remove',
  367. handler : removeNode,
  368. cls : 'remove-mi',
  369. text : 'Remove Item'
  370. }]
  371. });
  372. function prepareCtx(node, e) {
  373. node.select();
  374. ctxMenu.items.get('remove')[node.attributes.allowDelete
  375. ? 'enable'
  376. : 'disable']();
  377. ctxMenu.showAt(e.getXY());
  378. }
  379. function collapseAll() {
  380. ctxMenu.hide();
  381. setTimeout(function() {
  382. croot.eachChild(function(n) {
  383. n.collapse(false, false);
  384. });
  385. }, 10);
  386. }
  387. function expandAll() {
  388. ctxMenu.hide();
  389. setTimeout(function() {
  390. croot.eachChild(function(n) {
  391. n.expand(false, false);
  392. });
  393. }, 10);
  394. }
  395. });