Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / public / javascripts / .svn / text-base / sitemap.js.svn-base
blob73d3e5e7a8d2d8ced78fdac946f31f77cef17fae
1 var SiteMap = Class.create(RuledTable, {
2   initialize: function($super, element) {
3     $super(element);
4     this.readExpandedCookie();
5     Event.observe(element, 'click', this.onMouseClickRow.bindAsEventListener(this));
6   },
7   
8   onMouseClickRow: function(event) {
9     if (this.isExpander(event.target)) {
10       var row = event.findElement('tr');
11       if (this.hasChildren(row)) {
12         this.toggleBranch(row, event.target);
13       }
14     }
15   },
16   
17   hasChildren: function(row) {
18     return !row.hasClassName('no-children');
19   },
20   
21   isExpander: function(element) {
22     return element.match('img.expander');
23   },
24   
25   isExpanded: function(row) {
26     return row.hasClassName('children-visible');
27   },
28   
29   isRow: function(element) {
30     return element && element.tagName && element.match('tr');
31   },
32   
33   extractLevel: function(row) {
34     if (/level-(\d+)/i.test(row.className))
35       return RegExp.$1.toInteger();
36   },
37   
38   extractPageId: function(row) {
39     if (/page-(\d+)/i.test(row.id))
40       return RegExp.$1.toInteger();
41   },
42   
43   getExpanderImageForRow: function(row) {
44     return row.down('img');
45   },
46   
47   readExpandedCookie: function() {
48     var matches = document.cookie.match(/expanded_rows=(.+?);/);
49     this.expandedRows = matches ? decodeURIComponent(matches[1]).split(',') : [];
50   },
52   saveExpandedCookie: function() {
53     document.cookie = "expanded_rows=" + encodeURIComponent(this.expandedRows.uniq().join(",")) + "; path=/admin";
54   }, 
56   persistCollapsed: function(row) {
57     var pageId = this.extractPageId(row);
58     this.expandedRows = this.expandedRows.without(pageId);
59     this.saveExpandedCookie();
60   },
62   persistExpanded: function(row) {
63     this.expandedRows.push(this.extractPageId(row));
64     this.saveExpandedCookie();
65   },
67   toggleExpanded: function(row, img) {
68     if (!img) img = this.getExpanderImageForRow(row);
69     if (this.isExpanded(row)) {
70       img.src = img.src.replace('collapse', 'expand');
71       row.removeClassName('children-visible');
72       row.addClassName('children-hidden');
73       this.persistCollapsed(row);
74     } else {
75       img.src = img.src.replace('expand', 'collapse');
76       row.removeClassName('children-hidden');
77       row.addClassName('children-visible');
78       this.persistExpanded(row);
79     }
80   },
81   
82   hideBranch: function(parent, img) {
83     var level = this.extractLevel(parent), row = parent.next();
84     while (this.isRow(row) && this.extractLevel(row) > level) {
85       row.hide();
86       row = row.next();
87     }
88     this.toggleExpanded(parent, img);
89   },
90   
91   showBranch: function(parent, img) {
92     var level = this.extractLevel(parent), row = parent.next(),
93         children = false, expandLevels = [level + 1];
94         
95     while (this.isRow(row)) {
96       var currentLevel = this.extractLevel(row);
97       if (currentLevel <= level) break;
98       children = true;
99       if (currentLevel < expandLevels.last()) expandLevels.pop();
100       if (expandLevels.include(currentLevel)) {
101         row.show();
102         if (this.isExpanded(row)) expandLevels.push(currentLevel + 1);
103       }
104       row = row.next();
105     }
106     if (!children) this.getBranch(parent);
107     this.toggleExpanded(parent, img);
108   },
109   
110   getBranch: function(row) {
111     var id = this.extractPageId(row), level = this.extractLevel(row),
112         spinner = $('busy-' + id);
113         
114     new Ajax.Updater(
115       row,
116       '../admin/ui/pages/children/' + id + '/' + level,
117       {
118         insertion: "after",
119         onLoading:  function() { spinner.show(); this.updating = true  }.bind(this),
120         onComplete: function() { spinner.fade(); this.updating = false }.bind(this)
121       }
122     );
123   },
124   
125   toggleBranch: function(row, img) {
126     if (!this.updating) {
127       var method = (this.isExpanded(row) ? 'hide' : 'show') + 'Branch';
128       this[method](row, img);
129     }
130   }