| 1 | // This is a view that contains subviews. Just set it up |
|---|
| 2 | // by initializing it with a hash of name => view pairs, |
|---|
| 3 | // and you can switch between views by calling |
|---|
| 4 | // setView(name) |
|---|
| 5 | |
|---|
| 6 | MultiView = new Class(View, { |
|---|
| 7 | // views is a hash of { name => view } |
|---|
| 8 | init: function (opts) { |
|---|
| 9 | if (!opts.views) |
|---|
| 10 | |
|---|
| 11 | this.currentSelection = null; |
|---|
| 12 | this.views = opts.views; |
|---|
| 13 | |
|---|
| 14 | this.viewList = []; |
|---|
| 15 | for (var p in opts.views) { |
|---|
| 16 | if (opts.views[p].hasOwnProperty("view")) { |
|---|
| 17 | this.viewList.push(p); |
|---|
| 18 | } |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | MultiView.superClass.init.apply(this, arguments); |
|---|
| 22 | |
|---|
| 23 | this.hideAllViews(); |
|---|
| 24 | |
|---|
| 25 | if (opts.defaultView) |
|---|
| 26 | this.setView(opts.defaultView); |
|---|
| 27 | }, |
|---|
| 28 | |
|---|
| 29 | hideAllViews: function () { |
|---|
| 30 | for (var i=0; i < this.viewList.length; i++) { |
|---|
| 31 | var ele = this.getElement(this.viewList[i]); |
|---|
| 32 | if (!ele) |
|---|
| 33 | continue; |
|---|
| 34 | |
|---|
| 35 | ele.style.display = "none"; |
|---|
| 36 | } |
|---|
| 37 | }, |
|---|
| 38 | |
|---|
| 39 | setView: function (name) { |
|---|
| 40 | name += ""; // treat as hash not array |
|---|
| 41 | if (!this.views[name]) return; |
|---|
| 42 | |
|---|
| 43 | this._setView(this.getElement(name)); |
|---|
| 44 | }, |
|---|
| 45 | |
|---|
| 46 | getElement: function (name) { |
|---|
| 47 | return this.views[name].getView(); |
|---|
| 48 | }, |
|---|
| 49 | |
|---|
| 50 | _setView: function (ele) { |
|---|
| 51 | if (!ele) |
|---|
| 52 | return; |
|---|
| 53 | |
|---|
| 54 | if (this.currentSelection == ele) return; |
|---|
| 55 | |
|---|
| 56 | ele.style.display = "block"; |
|---|
| 57 | |
|---|
| 58 | this.view.appendChild(ele); |
|---|
| 59 | |
|---|
| 60 | if (this.currentSelection) |
|---|
| 61 | this.view.removeChild(this.currentSelection); |
|---|
| 62 | |
|---|
| 63 | this.currentSelection = ele; |
|---|
| 64 | |
|---|
| 65 | this._render(); |
|---|
| 66 | } |
|---|
| 67 | }); |
|---|