1 | define([ |
---|
2 | 'dojo/_base/declare', |
---|
3 | 'dojo/_base/array', |
---|
4 | 'dijit/form/Button', |
---|
5 | 'dojo/dom-class', |
---|
6 | 'dojo/topic', |
---|
7 | 'dijit/_WidgetBase', |
---|
8 | '../app/Router' |
---|
9 | ], function(declare, baseArray, Button, domClass, topic, _WidgetBase,Router){ |
---|
10 | return declare([_WidgetBase], { |
---|
11 | _crumbs: [], |
---|
12 | |
---|
13 | _last: function(){ |
---|
14 | return this._crumbs[this._crumbs.length-1]; |
---|
15 | }, |
---|
16 | _getIndexOf: function(label) { |
---|
17 | baseArray.forEach(this._crumbs, function(crumb, index){ |
---|
18 | if (crumb.label === label) { |
---|
19 | return index; |
---|
20 | } |
---|
21 | }, this); |
---|
22 | return -1; |
---|
23 | }, |
---|
24 | |
---|
25 | addCrumb: function(label, path) { |
---|
26 | this._crumbs.push({ |
---|
27 | label: label, |
---|
28 | path:path |
---|
29 | }); |
---|
30 | }, |
---|
31 | _removeAfter: function(index) { |
---|
32 | var removals = this._crumbs.slice(index+1); |
---|
33 | this._crumbs = this._crumbs.slice(0, index+1); |
---|
34 | baseArray.forEach(removals, function(removal){ |
---|
35 | removal.widget.destroyRecursive(false); // boolean "keepDOMnode" |
---|
36 | }, this); |
---|
37 | this._highlightLast(); |
---|
38 | }, |
---|
39 | |
---|
40 | _highlightLast: function() { |
---|
41 | domClass.add(this._last().widget, "breadcrumbCurrent"); |
---|
42 | }, |
---|
43 | _unhighlightLast: function() { |
---|
44 | domClass.remove(this._last(), "breadcrumbCurrent"); |
---|
45 | }, |
---|
46 | |
---|
47 | _changeTo: function(label, path) { |
---|
48 | // TODO: Differentiate between navigating backwards and sideways/forward! |
---|
49 | var index = baseArray.indexOf({label:label , path:path}); |
---|
50 | //var index = this._getIndexOf(label); // Use this if passing path as an argument is not possible! |
---|
51 | if (index >= 0) this._removeAfter(index); |
---|
52 | this._addCrumb(label, path); |
---|
53 | this._createBreadcrumbs(); |
---|
54 | }, |
---|
55 | |
---|
56 | _createBreadcrumb: function(label, path) { |
---|
57 | return new Button({ |
---|
58 | baseClass: "breadcrumb", |
---|
59 | label: label, |
---|
60 | showlabel: true, |
---|
61 | iconClass: "dijitNoIcon", |
---|
62 | onClick: lang.hitch(this, function(){ |
---|
63 | Router.go(path); // TODO: fix this call! |
---|
64 | }) |
---|
65 | }); |
---|
66 | }, |
---|
67 | _createBreadcrumbs: function() { |
---|
68 | baseArray.forEach(this._crumbs, function(crumb, index){ |
---|
69 | if (!crumb.widget) { |
---|
70 | crumb.widget = this._createBreadcrumb(crumb.label, crumb.path); |
---|
71 | } |
---|
72 | }, this); |
---|
73 | this._highlightLast(); |
---|
74 | }, |
---|
75 | startup: function() { |
---|
76 | if ( this._started ){ return; } |
---|
77 | this.inherited(arguments); |
---|
78 | // TODO: start page crumb creation? |
---|
79 | this._createBreadCrumbs(); |
---|
80 | this._setupEvents(); |
---|
81 | this.inherited(arguments); |
---|
82 | }, |
---|
83 | _setupEvents: function() { |
---|
84 | // TODO: Bind changeTo() listener to an "onLoadPage" event exposed by rft.api! |
---|
85 | topic.subscribe("rft/content/goTo", this._changeTo(label, path)); |
---|
86 | } |
---|
87 | }); |
---|
88 | }); |
---|
89 | |
---|
90 | /* Breadcrumbs system, which automatically generates styled buttons in the topBar. |
---|
91 | * Updates are triggered through a "changePage" subscription to "rft/connect/goTo" (method that loads pages in rft.connect.js) |
---|
92 | * |
---|
93 | * Todo: Startup() does not handle initial creation well. No initial breadcrumb is created on page load. What happens when I refresh the page, do all previous crumbs disappear? |
---|
94 | * Todo: Pages should have a label/name besides the path, for display in the breadcrumbs bar. Perhaps the page main title? (Generate this header dynamically?) |
---|
95 | * Todo: Put connect.publish("rft/content/goTo", args); call in rft/content, to trigger breadcrumbs updates |
---|
96 | * This method uses path and 'args'(?) instead of label and path, change terminology accordingly? |
---|
97 | * Todo: breadcrumbs do not have context yet, unless we can somehow create that context from the path. Split path at "/" char, lookup label/title for each step? |
---|
98 | * (See before!) |
---|
99 | * |
---|
100 | */ |
---|