Debian Codegraph @

dojox/mobile/bookmarkable.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/connect",
4 "dojo/_base/lang",
5 "dojo/_base/window",
6 "dojo/hash",
7 "dijit/registry",
8 "./TransitionEvent",
9 "./View",
10 "./viewRegistry"
11 ], function(array, connect, lang, win, hash, registry, TransitionEvent, View, viewRegistry){
12
13 // module:
14 // dojox/mobile/bookmarkable
15
16 var b = {
17 // summary:
18 // Utilities to make the view transitions bookmarkable.
19
20 // settingHash: [private] Boolean
21 // Whether the browser URL needs to be updated to include the hash.
22 settingHash: false,
23
24 // transitionInfo: Array
25 // An array containing information about the transition.
26 transitionInfo: [],
27
28 getTransitionInfo: function(/*String*/ fromViewId, /*String*/ toViewId){
29 // summary:
30 // Returns an array containing the transition information.
31 return this.transitionInfo[fromViewId.replace(/^#/, "") + ":" + toViewId.replace(/^#/, "")]; // Array
32 },
33
34 addTransitionInfo: function(/*String*/ fromViewId, /*String*/ toViewId, /*Object*/args){
35 // summary:
36 // Adds transition information.
37 this.transitionInfo[fromViewId.replace(/^#/, "") + ":" + toViewId.replace(/^#/, "")] = args;
38 },
39
40 findTransitionViews: function(/*String*/moveTo){
41 // summary:
42 // Searches for a starting view and a destination view.
43 if(!moveTo){ return []; }
44 var view = registry.byId(moveTo.replace(/^#/, ""));
45 if(!view){ return []; }
46 for(var v = view.getParent(); v; v = v.getParent()){ // search for the topmost invisible parent node
47 if(v.isVisible && !v.isVisible()){
48 view = v;
49 }
50 }
51 // fromView, toView
52 return [view.getShowingView(), view]; // Array
53 },
54
55 onHashChange: function(value){
56 // summary:
57 // Called on "/dojo/hashchange" events.
58 if(this.settingHash){
59 this.settingHash = false;
60 return;
61 }
62 var params = this.handleFragIds(value);
63 params.hashchange = true;
64 if (params.moveTo) {
65 new TransitionEvent(win.body(), params).dispatch();
66 }
67 },
68
69 handleFragIds: function(/*String*/fragIds){
70 // summary:
71 // Analyzes the given hash (fragment id).
72 // description:
73 // Given a comma-separated list of view IDs, this method
74 // searches for a transition destination, and makes all the
75 // views in the hash visible.
76
77 var arr, moveTo, args, dir;
78 if(!fragIds){
79 moveTo = viewRegistry.initialView.id;
80 arr = this.findTransitionViews(moveTo);
81 }else{
82 var ids = fragIds.replace(/^#/, "").split(/,/);
83 for(var i = 0; i < ids.length; i++){
84 // Search for a transition destination view.
85
86 var view = registry.byId(ids[i]);
87
88 // Skip a non-existent or visible view. Visible view can't be a destination candidate.
89 if(!view || view.isVisible()){ continue; }
90
91 // Check if all the ancestors are in the fragIds.
92 // If not, obviously the view was NOT visible before the previous transition.
93 // That means the previous transition can't happen from that view,
94 // which means the view can't be a destination.
95 var success = true;
96 for(var v = viewRegistry.getParentView(view); v; v = viewRegistry.getParentView(v)){
97 if(array.indexOf(ids, v.id) === -1){
98 success = false;
99 break;
100 }
101 }
102 if(!success){
103 // Simply make the view visible without transition.
104 array.forEach(view.getSiblingViews(), function(v){
105 v.domNode.style.display = (v === view) ? "" : "none";
106 });
107 continue;
108 }
109
110 arr = this.findTransitionViews(ids[i]);
111 if(arr.length === 2){
112 moveTo = ids[i];
113 // The destination found. But continue the loop to make
114 // the other views in the fragIds visible.
115 }
116 }
117 }
118
119 if (arr) {
120 args = this.getTransitionInfo(arr[0].id, arr[1].id);
121 dir = 1;
122 if(!args){
123 args = this.getTransitionInfo(arr[1].id, arr[0].id);
124 dir = -1;
125 }
126 }
127
128 return {
129 moveTo: moveTo ? "#" + moveTo : null,
130 transitionDir: args ? args.transitionDir * dir : 1,
131 transition: args ? args.transition : "none"
132 };
133 },
134
135 setFragIds: function(/*Widget*/toView){
136 // summary:
137 // Updates the hash (fragment id) in the browser URL.
138 // description:
139 // The hash value consists of one or more visible view ids
140 // separated with commas.
141
142 var arr = array.filter(viewRegistry.getViews(), function(v){ return v.isVisible(); });
143 this.settingHash = true;
144 hash(array.map(arr, function(v){ return v.id; }).join(","));
145 }
146 };
147
148 connect.subscribe("/dojo/hashchange", null, function(){ b.onHashChange.apply(b, arguments); });
149
150 lang.extend(View, {
151 getTransitionInfo: function(){ b.getTransitionInfo.apply(b, arguments); },
152 addTransitionInfo: function(){ b.addTransitionInfo.apply(b, arguments); },
153 handleFragIds: function(){ b.handleFragIds.apply(b, arguments); },
154 setFragIds: function(){ b.setFragIds.apply(b, arguments); }
155 });
156
157 return b;
158 });