123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- /*
- * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
- *
- * http://extjs.com/license
- */
- Ext.onReady(function() {
- var xt = Ext.tree;
- // seeds for the new node suffix
- var cseed = 0, oseed = 0;
- // turn on quick tips
- Ext.QuickTips.init();
- var cview = Ext.DomHelper.append('main-ct', {
- cn : [{
- id : 'main-tb'
- }, {
- id : 'cbody'
- }]
- });
- // create the primary toolbar
- var tb = new Ext.Toolbar('main-tb');
- tb.add({
- id : 'save',
- text : 'Save',
- disabled : true,
- handler : save,
- cls : 'x-btn-text-icon save',
- tooltip : 'Saves all components to the server'
- }, '-', {
- id : 'add',
- text : 'Component',
- handler : addComponent,
- cls : 'x-btn-text-icon add-cmp',
- tooltip : 'Add a new Component to the dependency builder'
- }, {
- id : 'option',
- text : 'Option',
- disabled : true,
- handler : addOption,
- cls : 'x-btn-text-icon add-opt',
- tooltip : 'Add a new optional dependency to the selected component'
- }, '-', {
- id : 'remove',
- text : 'Remove',
- disabled : true,
- handler : removeNode,
- cls : 'x-btn-text-icon remove',
- tooltip : 'Remove the selected item'
- });
- // for enabling and disabling
- var btns = tb.items.map;
- // create our layout
- var layout = new Ext.BorderLayout('main-ct', {
- west : {
- split : true,
- initialSize : 200,
- minSize : 175,
- maxSize : 400,
- titlebar : true,
- margins : {
- left : 5,
- right : 0,
- bottom : 5,
- top : 5
- }
- },
- center : {
- title : 'Components',
- margins : {
- left : 0,
- right : 5,
- bottom : 5,
- top : 5
- }
- }
- }, 'main-ct');
- layout.batchAdd({
- west : {
- id : 'source-files',
- autoCreate : true,
- title : 'Ext Source Files',
- autoScroll : true,
- fitToFrame : true
- },
- center : {
- el : cview,
- autoScroll : true,
- fitToFrame : true,
- toolbar : tb,
- resizeEl : 'cbody'
- }
- });
- // this is the source code tree
- var stree = new xt.TreePanel('source-files', {
- animate : true,
- loader : new xt.TreeLoader({
- dataUrl : 'dependency.php'
- }),
- enableDrag : true,
- containerScroll : true
- });
- new xt.TreeSorter(stree, {
- folderSort : true
- });
- var sroot = new xt.AsyncTreeNode({
- text : 'Ext JS',
- draggable : false,
- id : 'source'
- });
- stree.setRootNode(sroot);
- stree.render();
- sroot.expand(false, false);
- // the component tree
- var ctree = new xt.TreePanel('cbody', {
- animate : true,
- enableDD : true,
- containerScroll : true,
- lines : false,
- rootVisible : false,
- loader : new Ext.tree.TreeLoader()
- });
- ctree.el.addKeyListener(Ext.EventObject.DELETE, removeNode);
- var croot = new xt.AsyncTreeNode({
- allowDrag : false,
- allowDrop : true,
- id : 'croot',
- text : 'Packages and Components',
- cls : 'croot',
- loader : new Ext.tree.TreeLoader({
- dataUrl : 'dep-tree.json',
- createNode : readNode
- })
- });
- ctree.setRootNode(croot);
- ctree.render();
- croot.expand();
- // some functions to determine whether is not the drop is allowed
- function hasNode(t, n) {
- return (t.attributes.type == 'fileCt' && t.findChild('id', n.id))
- || (t.leaf === true && t.parentNode.findChild('id', n.id));
- };
- function isSourceCopy(e, n) {
- var a = e.target.attributes;
- return n.getOwnerTree() == stree
- && !hasNode(e.target, n)
- && ((e.point == 'append' && a.type == 'fileCt') || a.leaf === true);
- };
- function isReorder(e, n) {
- return n.parentNode == e.target.parentNode && e.point != 'append';
- };
- // handle drag over and drag drop
- ctree.on('nodedragover', function(e) {
- var n = e.dropNode;
- return isSourceCopy(e, n) || isReorder(e, n);
- });
- ctree.on('beforenodedrop', function(e) {
- var n = e.dropNode;
- // copy node from source tree
- if (isSourceCopy(e, n)) {
- var copy = new xt.TreeNode(Ext.apply({
- allowDelete : true,
- expanded : true
- }, n.attributes));
- copy.loader = undefined;
- if (e.target.attributes.options) {
- e.target = createOption(e.target, copy.text);
- // return false;
- }
- e.dropNode = copy;
- return true;
- }
- return isReorder(e, n);
- });
- ctree.on('contextmenu', prepareCtx);
- // track whether save is allowed
- ctree.on('append', trackSave);
- ctree.on('remove', trackSave);
- ctree.el.swallowEvent('contextmenu', true);
- ctree.el.on('keypress', function(e) {
- if (e.isNavKeyPress()) {
- e.stopEvent();
- }
- });
- // when the tree selection changes, enable/disable the toolbar buttons
- var sm = ctree.getSelectionModel();
- sm.on('selectionchange', function() {
- var n = sm.getSelectedNode();
- if (!n) {
- btns.remove.disable();
- btns.option.disable();
- return;
- }
- var a = n.attributes;
- btns.remove.setDisabled(!a.allowDelete);
- btns.option.setDisabled(!a.cmpId);
- });
- // create the editor for the component tree
- var ge = new xt.TreeEditor(ctree, {
- allowBlank : false,
- blankText : 'A name is required',
- selectOnFocus : true
- });
- ge.on('beforestartedit', function() {
- if (!ge.editNode.attributes.allowEdit) {
- return false;
- }
- });
- // add component handler
- function addComponent() {
- var id = guid('c-');
- var text = 'Component ' + (++cseed);
- var node = createComponent(id, text);
- node.expand(false, false);
- node.select();
- node.lastChild.ensureVisible();
- ge.triggerEdit(node);
- }
- function createComponent(id, text, cfiles, cdep, coptions) {
- var node = new xt.TreeNode({
- text : text,
- iconCls : 'cmp',
- cls : 'cmp',
- type : 'cmp',
- id : id,
- cmpId : id,
- allowDelete : true,
- allowEdit : true
- });
- croot.appendChild(node);
- var files = new xt.AsyncTreeNode({
- text : 'Files',
- allowDrag : false,
- allowDrop : true,
- iconCls : 'folder',
- type : 'fileCt',
- cmpId : id,
- allowDelete : false,
- children : cfiles || [],
- expanded : true
- });
- var dep = new xt.AsyncTreeNode({
- text : 'Dependencies',
- allowDrag : false,
- allowDrop : true,
- iconCls : 'folder',
- type : 'fileCt',
- cmpId : id,
- allowDelete : false,
- children : cdep || [],
- expanded : true,
- allowCopy : true
- });
- var options = new xt.AsyncTreeNode({
- text : 'Optional Dependencies',
- allowDrag : false,
- allowDrop : true,
- iconCls : 'folder',
- type : 'fileCt',
- options : true,
- cmpId : id,
- allowDelete : false,
- children : coptions || [],
- expanded : true,
- allowCopy : true
- });
- node.appendChild(files);
- node.appendChild(dep);
- node.appendChild(options);
- return node;
- }
- // remove handler
- function removeNode() {
- var n = sm.getSelectedNode();
- if (n && n.attributes.allowDelete) {
- ctree.getSelectionModel().selectPrevious();
- n.parentNode.removeChild(n);
- }
- }
- // add option handler
- function addOption() {
- var n = sm.getSelectedNode();
- if (n) {
- var node = createOption(n, 'Option' + (++oseed));
- node.select();
- ge.triggerEdit(node);
- }
- }
- function createOption(n, text) {
- var cnode = ctree.getNodeById(n.attributes.cmpId);
- var node = new xt.TreeNode({
- text : text,
- cmpId : cnode.id,
- iconCls : 'folder',
- type : 'fileCt',
- allowDelete : true,
- allowEdit : true,
- id : guid('o-')
- });
- cnode.childNodes[2].appendChild(node);
- cnode.childNodes[2].expand(false, false);
- return node;
- }
- // semi unique ids across edits
- function guid(prefix) {
- return prefix + (allGetServerTime().getTime());
- }
- function trackSave() {
- btns.save.setDisabled(!croot.hasChildNodes());
- }
- function storeChildren(cmp, n, name) {
- if (n.childrenRendered) {
- cmp[name] = [];
- n.eachChild(function(f) {
- cmp[name].push(f.attributes);
- });
- } else {
- cmp[name] = n.attributes.children || [];
- }
- }
- // save to the server in a format usable in PHP
- function save() {
- var ch = [];
- croot.eachChild(function(c) {
- var cmp = {
- text : c.text,
- id : c.id,
- options : []
- };
- storeChildren(cmp, c.childNodes[0], 'files');
- storeChildren(cmp, c.childNodes[1], 'dep');
- var onode = c.childNodes[2];
- if (!onode.childrenRendered) {
- cmp.options = onode.attributes.children || [];
- } else {
- onode.eachChild(function(o) {
- var opt = Ext.apply({}, o.attributes);
- storeChildren(opt, o, 'children');
- cmp.options.push(opt);
- });
- }
- ch.push(cmp);
- });
- layout.el.mask('Sending data to server...', 'x-mask-loading');
- var hide = layout.el.unmask.createDelegate(layout.el);
- Ext.lib.Ajax.request('POST', 'save-dep.php', {
- success : hide,
- failure : hide
- }, 'data=' + encodeURIComponent(Ext.encode(ch)));
- }
- function readNode(o) {
- createComponent(o.id, o.text, o.files, o.dep, o.options);
- }
- // context menus
- var ctxMenu = new Ext.menu.Menu({
- id : 'copyCtx',
- items : [{
- id : 'expand',
- handler : expandAll,
- cls : 'expand-all',
- text : 'Expand All'
- }, {
- id : 'collapse',
- handler : collapseAll,
- cls : 'collapse-all',
- text : 'Collapse All'
- }, '-', {
- id : 'remove',
- handler : removeNode,
- cls : 'remove-mi',
- text : 'Remove Item'
- }]
- });
- function prepareCtx(node, e) {
- node.select();
- ctxMenu.items.get('remove')[node.attributes.allowDelete
- ? 'enable'
- : 'disable']();
- ctxMenu.showAt(e.getXY());
- }
- function collapseAll() {
- ctxMenu.hide();
- setTimeout(function() {
- croot.eachChild(function(n) {
- n.collapse(false, false);
- });
- }, 10);
- }
- function expandAll() {
- ctxMenu.hide();
- setTimeout(function() {
- croot.eachChild(function(n) {
- n.expand(false, false);
- });
- }, 10);
- }
- });
|