|
| 1 | +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | + * |
| 3 | + * The contents of this file are subject to the Mozilla Public License |
| 4 | + * Version 1.1 (the "License"); you may not use this file except in |
| 5 | + * compliance with the License. You may obtain a copy of the License at |
| 6 | + * http://www.mozilla.org/MPL/ |
| 7 | + * |
| 8 | + * Software distributed under the License is distributed on an "AS IS" basis, |
| 9 | + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 10 | + * for the specific language governing rights and limitations under the |
| 11 | + * License. |
| 12 | + * |
| 13 | + * The Original Code is FireFTP |
| 14 | + * |
| 15 | + * The Initial Developer of the Original Code is |
| 16 | + * Mime Cuvalo |
| 17 | + * Portions created by Mime Cuvalo are Copyright (C) 2004 Mime Cuvalo. |
| 18 | + * |
| 19 | + * Alternatively, the contents of this file may be used under the |
| 20 | + * terms of the GNU Public License (the "GPL"), in which case the |
| 21 | + * provisions of the GPL are applicable instead of those above. |
| 22 | + * If you wish to allow use of your version of this file only |
| 23 | + * under the terms of the GPL and not to allow others to use your |
| 24 | + * version of this file under the MPL, indicate your decision by |
| 25 | + * deleting the provisions above and replace them with the notice |
| 26 | + * and other provisions required by the GPL. If you do not delete |
| 27 | + * the provisions above, a recipient may use your version of this |
| 28 | + * file under either the MPL or the GPL. |
| 29 | + * |
| 30 | + * Contributor(s): |
| 31 | + * Gijs Kruitbosch, <gijskruitbosch@gmail`com> |
| 32 | + */ |
| 33 | +/* |
| 34 | + * Large portions of this code are taken literally or partially from code for |
| 35 | + * the FireFTP extension by Mime Cuvalo. Many thanks to him for writing the |
| 36 | + * original code. |
| 37 | + */ |
| 38 | + |
| 39 | +var chromeDirTree = { |
| 40 | + data: new Array, |
| 41 | + getCellText: cdt_getCellText, |
| 42 | + getLevel: cdt_getLevel, |
| 43 | + getParentIndex: cdt_getParentIndex, |
| 44 | + getImageSrc: function(row,col) {}, |
| 45 | + getCellProperties: function(row,col,props) {}, |
| 46 | + getColumnProperties: function(colid,col,props) {}, |
| 47 | + getRowProperties: function(row,props) {}, |
| 48 | + hasNextSibling: function(row,nextrow) { return this.data[row].hasNext; }, |
| 49 | + isContainer: function(row) { return true; }, |
| 50 | + isContainerEmpty: cdt_isContainerEmpty, |
| 51 | + isContainerOpen: function(row) { return this.data[row].open; }, |
| 52 | + isSeparator: function(row) { return false; }, |
| 53 | + isSorted: function(row) { return false; }, |
| 54 | + setTree: function(treebox) { this.treebox = treebox; }, |
| 55 | + toggleOpenState: cdt_toggleOpenState, |
| 56 | + updateParentIndices: cdt_updateParentIndices, |
| 57 | + cdup: cdt_cdup, |
| 58 | + reselectCurrentDirectory: cdt_reselectCurrentDirectory, |
| 59 | + changeDir: cdt_changeDir, |
| 60 | + indexOfURL: cdt_indexOfURL, |
| 61 | + canDrop: function (aIndex, aOrientation) { return false; }, |
| 62 | + click: cdt_click |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +function cdt_getCellText(row,column) |
| 67 | +{ |
| 68 | + return row == -1 ? "" : this.data[row].leafName; |
| 69 | +} |
| 70 | + |
| 71 | +function cdt_getLevel(row) |
| 72 | +{ |
| 73 | + if ((row < 0) || (row >= this.data.length)) |
| 74 | + return 0; // Don't feed bogus, please. |
| 75 | + |
| 76 | + return this.data[row].level - 1; // Woo! Decrement because <tree> assumes lowest level is 0. |
| 77 | +} |
| 78 | + |
| 79 | +function cdt_isContainerEmpty(row) |
| 80 | +{ |
| 81 | + if ((row < 0) || (row >= this.data.length)) // rows we don't know are empty. |
| 82 | + return true; |
| 83 | + return this.data[row].empty; |
| 84 | +} |
| 85 | + |
| 86 | +function cdt_getParentIndex(row) |
| 87 | +{ |
| 88 | + if (row <= 0) |
| 89 | + return -1; |
| 90 | + return this.data[row].parentIndex; |
| 91 | +} |
| 92 | + |
| 93 | +function cdt_toggleOpenState(row) |
| 94 | +{ |
| 95 | + // Don't feed us nonsense. |
| 96 | + if (row < 0 || row > this.data.length) |
| 97 | + return; |
| 98 | + |
| 99 | + if (this.isContainerOpen(row)) |
| 100 | + { |
| 101 | + // The container is open, find all children and remove them. |
| 102 | + var currentLevel = this.getLevel(row); |
| 103 | + // last child to remove: |
| 104 | + var lastChild = row; |
| 105 | + // Find the last child's index (within the limits of the data and with a higher level): |
| 106 | + while ((lastChild + 1 < this.rowCount) && (this.getLevel(lastChild + 1) > currentLevel)) |
| 107 | + ++lastChild; |
| 108 | + // Remove the subdirectories and clean up: |
| 109 | + this.data[row].children = this.data.splice(row + 1, lastChild - row); |
| 110 | + this.updateParentIndices(); |
| 111 | + this.rowCount = this.data.length; |
| 112 | + this.treebox.rowCountChanged(row, -(lastChild - row)); |
| 113 | + |
| 114 | + // Alright, it's no longer open: |
| 115 | + this.data[row].open = false; |
| 116 | + // Update the row: |
| 117 | + this.treebox.invalidateRow(row); |
| 118 | + |
| 119 | + // Technically, we can be asked to collapse a node above the node we're |
| 120 | + // viewing. We need to cope with that: |
| 121 | + if (chromeTree.currentURL.indexOf(this.data[row].href) == 0 |
| 122 | + && chromeTree.currentURL != this.data[row].href |
| 123 | + && chromeTree.currentLevel > this.getLevel(row)) |
| 124 | + { |
| 125 | + chromeTree.currentURL = this.data[row].href; |
| 126 | + chromeTree.updateView(); |
| 127 | + this.selection.select(row); |
| 128 | + this.treebox.ensureRowIsVisible(row); |
| 129 | + } |
| 130 | + else if (chromeTree.currentURL == this.data[row].href) |
| 131 | + { |
| 132 | + this.selection.select(row); |
| 133 | + this.treebox.ensureRowIsVisible(row); |
| 134 | + } |
| 135 | + } |
| 136 | + else // Okay, this node was closed, we open it, we need to add the children. |
| 137 | + { |
| 138 | + // Do we have stored data on the children from before? Pretty please? :-D |
| 139 | + if (this.data[row].children) |
| 140 | + { |
| 141 | + for (var x = this.data[row].children.length - 1; x >= 0; --x) |
| 142 | + this.data.splice(row + 1, 0, this.data[row].children[x]); |
| 143 | + |
| 144 | + // Clean up |
| 145 | + this.updateParentIndices(); |
| 146 | + this.rowCount = this.data.length; |
| 147 | + this.treebox.rowCountChanged(row + 1, this.data[row].children.length); |
| 148 | + this.data[row].children = null; |
| 149 | + this.data[row].open = true; |
| 150 | + this.treebox.invalidateRow(row); |
| 151 | + } |
| 152 | + else // Awwwww. :-(. Well, let's do it the hard way then. |
| 153 | + { |
| 154 | + var dirNode = chromeStructure.findURL(this.data[row].href); |
| 155 | + var subDirs = []; |
| 156 | + for (var k in dirNode.directories) |
| 157 | + { |
| 158 | + subDirs.push( {leafName: k, parent: dirNode.href, href: dirNode.directories[k].href, |
| 159 | + level: dirNode.directories[k].level, |
| 160 | + open: false, empty: false, children: null, hasNext: true, parentIndex: -1 }); |
| 161 | + } |
| 162 | + if (subDirs.length == 0) |
| 163 | + { |
| 164 | + this.data[row].empty = true; |
| 165 | + this.data[row].open = false; |
| 166 | + this.treebox.invalidateRow(row); |
| 167 | + return; |
| 168 | + } |
| 169 | + subDirs.sort(dirSort); |
| 170 | + subDirs[subDirs.length - 1].hasNext = false; // Last element doesn't have a next item. |
| 171 | + |
| 172 | + for (var x = subDirs.length - 1; x >= 0; --x) |
| 173 | + this.data.splice(row + 1, 0, subDirs[x]); |
| 174 | + this.rowCount = this.data.length; |
| 175 | + this.treebox.rowCountChanged(row + 1, subDirs.length); |
| 176 | + this.data[row].open = true; |
| 177 | + this.treebox.invalidateRow(row); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +function cdt_updateParentIndices() |
| 183 | +{ |
| 184 | + for (var x = 0; x < this.data.length; ++x) |
| 185 | + { |
| 186 | + var pIndex = this.data[x].parent ? this.indexOfURL(this.data[x].parent) : -1; |
| 187 | + this.data[x].parentIndex = pIndex; |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +function cdt_cdup() |
| 192 | +{ |
| 193 | + var parentIndex = this.getParentIndex(this.selection.currentIndex); |
| 194 | + if (parentIndex != -1) |
| 195 | + { |
| 196 | + this.changeDir(this.data[parentIndex].href); |
| 197 | + this.selection.select(parentIndex); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +function cdt_reselectCurrentDirectory() |
| 202 | +{ |
| 203 | + var index = this.indexOfURL(chromeTree.currentURL); |
| 204 | + this.selection.select(index); |
| 205 | + this.treebox.ensureRowIsVisible(index); |
| 206 | +} |
| 207 | + |
| 208 | +function cdt_changeDir(href) |
| 209 | +{ |
| 210 | + // Hrmmm.... |
| 211 | + if (!(/\/$/).test(href)) // No slash at the end? tsk. |
| 212 | + href = href + "/"; |
| 213 | + |
| 214 | + var oldDir = chromeTree.currentURL; |
| 215 | + chromeTree.currentURL = href; |
| 216 | + if (this.data.length == 0) // We need to create the full data array first. |
| 217 | + { |
| 218 | + var elemsAdded = 0; |
| 219 | + for (var dir in chromeStructure.directories) |
| 220 | + { |
| 221 | + var dirObj = chromeStructure.directories[dir]; |
| 222 | + this.data.push( {leafName: dir, parent: "", href: dirObj.href, open: false, level: dirObj.level, |
| 223 | + empty: false, children: null, hasNext:false, parentIndex: -1} ); |
| 224 | + elemsAdded++; |
| 225 | + } |
| 226 | + this.data.sort(dirSort); |
| 227 | + this.rowCount = elemsAdded; |
| 228 | + this.treebox.rowCountChanged(0, elemsAdded); |
| 229 | + this.selection.select(0); |
| 230 | + } |
| 231 | + |
| 232 | + // Now we're sure we have a tree. Do something useful with it. |
| 233 | + var currentLevel = chromeTree.currentLevel; |
| 234 | + // open parent directories til we find the directory |
| 235 | + for (var x = 0; x < this.data.length; ++x) |
| 236 | + { |
| 237 | + for (var y = this.data.length - 1; y >= x; --y) |
| 238 | + { |
| 239 | + // Does the current row have a matching href? |
| 240 | + if (chromeTree.currentURL.indexOf(this.data[y].href) == 0 |
| 241 | + // Is the level smaller (parent dir), or do we have an exactly matching URL? |
| 242 | + && (this.getLevel(y) < currentLevel || chromeTree.currentURL == this.data[y].href)) |
| 243 | + { |
| 244 | + x = y; |
| 245 | + break; |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + if (chromeTree.currentURL.indexOf(this.data[x].href) == 0) |
| 250 | + { |
| 251 | + // If the directory is not open, open it. |
| 252 | + if (!this.data[x].open) |
| 253 | + this.toggleOpenState(x); |
| 254 | + |
| 255 | + if (chromeTree.currentURL == this.data[x].href) // Woo, we're done! |
| 256 | + { |
| 257 | + chromeTree.updateView(); |
| 258 | + return; |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + // If we get here, we never found the damn thing, and that's bad. |
| 263 | + // We should go back to where we were previously. |
| 264 | + // XXX: maybe fix this to refresh the entire dir structure (bad, lots of work!) |
| 265 | + chromeTree.currentURL = oldDir; |
| 266 | +} |
| 267 | + |
| 268 | +function cdt_indexOfURL(href) |
| 269 | +{ // binary search to find an url in the chromeDirTree |
| 270 | + var left = 0; |
| 271 | + var right = this.data.length - 1; |
| 272 | + href = href.replace(/\x2f/g, "\x01").toLowerCase(); // make '/' less than everything (except null) |
| 273 | + |
| 274 | + while (left <= right) { |
| 275 | + var mid = Math.floor((left + right) / 2); |
| 276 | + var dataHref = this.data[mid].href.replace(/\x2f/g, "\x01").toLowerCase(); |
| 277 | + if (dataHref == href || dataHref + "\x01" == href || dataHref == href + "\x01") |
| 278 | + return mid; |
| 279 | + else if (href < dataHref) |
| 280 | + right = mid - 1; |
| 281 | + else if (href > dataHref) |
| 282 | + left = mid + 1; |
| 283 | + } |
| 284 | + return -1; |
| 285 | +} |
| 286 | + |
| 287 | +function dirSort(a, b) |
| 288 | +{ |
| 289 | + // make '/' less than everything (except null) |
| 290 | + var tempA = a.href.replace(/\x2f/g, "\x01").toLowerCase(); |
| 291 | + var tempB = b.href.replace(/\x2f/g, "\x01").toLowerCase(); |
| 292 | + |
| 293 | + if (tempA < tempB) |
| 294 | + return -1; |
| 295 | + if (tempA > tempB) |
| 296 | + return 1; |
| 297 | + return 0; |
| 298 | +} |
| 299 | + |
| 300 | + |
| 301 | +////////////////////////////// |
| 302 | +// Handlers |
| 303 | +////////////////////////////// |
| 304 | + |
| 305 | +function cdt_click(event) |
| 306 | +{ |
| 307 | + if (event.button == 0 || event.button == 2) |
| 308 | + { |
| 309 | + var row = {}; var col = {}; var child = {}; |
| 310 | + this.treebox.getCellAt(event.pageX, event.pageY, row, col, child); |
| 311 | + var index = this.selection.currentIndex; |
| 312 | + |
| 313 | + // index == row.value in case were collapsing the folder |
| 314 | + if ((index == row.value) && (this.data[index].href != chromeTree.currentURL)) |
| 315 | + this.changeDir(this.data[index].href); |
| 316 | + } |
| 317 | +} |
0 commit comments