| 1 |
define([ |
| 2 |
"dojo/_base/kernel", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojo/_base/config", |
| 5 |
"dojo/_base/connect", |
| 6 |
"dojo/_base/declare", |
| 7 |
"dojo/_base/lang", |
| 8 |
"dojo/_base/window", |
| 9 |
"dojo/dom", |
| 10 |
"dijit/registry" |
| 11 |
], function(kernel, array, config, connect, declare, lang, win, dom, registry){ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
kernel.experimental("dojox.mobile.ScreenSizeAware"); |
| 17 |
|
| 18 |
var cls = declare("dojox.mobile.ScreenSizeAware", null, { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
splitterId: "", |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
leftPaneId: "", |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
rightPaneId: "", |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
leftViewId: "", |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
leftListId: "", |
| 69 |
|
| 70 |
constructor: function(options){ |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if (options){ |
| 76 |
lang.mixin(this, options); |
| 77 |
} |
| 78 |
connect.subscribe("/dojox/mobile/screenSize/tablet", this, function(dim){ |
| 79 |
this.transformUI("tablet"); |
| 80 |
}); |
| 81 |
connect.subscribe("/dojox/mobile/screenSize/phone", this, function(dim){ |
| 82 |
this.transformUI("phone"); |
| 83 |
}); |
| 84 |
}, |
| 85 |
|
| 86 |
init: function(){ |
| 87 |
|
| 88 |
|
| 89 |
if(this._initialized){ return; } |
| 90 |
this._initialized = true; |
| 91 |
|
| 92 |
|
| 93 |
this.splitter = this.splitterId ? registry.byId(this.splitterId) : |
| 94 |
array.filter(registry.findWidgets(win.body()), |
| 95 |
function(c){ return c.declaredClass.indexOf("Splitter") !== -1; })[0]; |
| 96 |
if(!this.splitter){ |
| 97 |
console.error("Splitter not found."); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
this.leftPane = this.leftPaneId ? registry.byId(this.leftPaneId) : |
| 102 |
this.splitter.getChildren()[0]; |
| 103 |
if(!this.leftPane){ |
| 104 |
console.error("Left pane not found."); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
this.rightPane = this.rightPaneId ? registry.byId(this.rightPaneId) : |
| 109 |
this.splitter.getChildren()[1]; |
| 110 |
if(!this.rightPane){ |
| 111 |
console.error("Right pane not found."); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
this.leftView = this.leftViewId ? registry.byId(this.leftViewId) : |
| 116 |
array.filter(registry.findWidgets(this.leftPane.containerNode), |
| 117 |
function(c){ return c.declaredClass.indexOf("View") !== -1; })[0]; |
| 118 |
if(!this.leftView){ |
| 119 |
console.error("Left view not found."); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
this.leftList = this.leftListId ? registry.byId(this.leftListId) : |
| 124 |
array.filter(registry.findWidgets(this.leftView.containerNode), |
| 125 |
function(c){ return c.declaredClass.indexOf("List") !== -1 || |
| 126 |
c.declaredClass.indexOf("IconContainer") !== -1; })[0]; |
| 127 |
if(!this.leftList){ |
| 128 |
console.error("Left list not found."); |
| 129 |
return; |
| 130 |
} |
| 131 |
}, |
| 132 |
|
| 133 |
isPhone: function(){ |
| 134 |
|
| 135 |
|
| 136 |
return this._currentMode === "phone"; |
| 137 |
}, |
| 138 |
|
| 139 |
getShowingView: function(){ |
| 140 |
|
| 141 |
|
| 142 |
var firstView = |
| 143 |
array.filter(this.rightPane.getChildren(), function(c){ return c.declaredClass.indexOf("View") !== -1; })[0]; |
| 144 |
if(!firstView){ return null; } |
| 145 |
return firstView.getShowingView() || |
| 146 |
array.filter(this.rightPane.getChildren(), function(c){ return c.selected; })[0] || |
| 147 |
firstView; |
| 148 |
}, |
| 149 |
|
| 150 |
updateStateful: function(){ |
| 151 |
|
| 152 |
|
| 153 |
this.leftList.set("stateful", !this.isPhone()); |
| 154 |
}, |
| 155 |
|
| 156 |
getDestinationId: function(item){ |
| 157 |
|
| 158 |
|
| 159 |
return item.moveTo; |
| 160 |
}, |
| 161 |
|
| 162 |
updateBackButton: function(){ |
| 163 |
|
| 164 |
|
| 165 |
array.forEach(this.leftList.getChildren(), function(item){ |
| 166 |
var id = this.getDestinationId(item); |
| 167 |
var view = registry.byId(id); |
| 168 |
if(view){ |
| 169 |
var heading = array.filter(view.getChildren(), function(c){ return c.declaredClass.indexOf("Heading") !== -1; })[0]; |
| 170 |
if(heading.backButton){ |
| 171 |
heading.backButton.domNode.style.display = this.isPhone() ? "" : "none"; |
| 172 |
} |
| 173 |
if(heading.backBtnNode){ |
| 174 |
heading.backBtnNode.style.display = this.isPhone() ? "" : "none"; |
| 175 |
} |
| 176 |
} |
| 177 |
}, this); |
| 178 |
}, |
| 179 |
|
| 180 |
updateTransition: function(){ |
| 181 |
|
| 182 |
|
| 183 |
var transition = this.isPhone() ? "slide" : "none"; |
| 184 |
array.forEach(this.leftList.getChildren(), function(item){ |
| 185 |
item.set("transition", transition); |
| 186 |
}); |
| 187 |
}, |
| 188 |
|
| 189 |
moveList: function(){ |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
var to = this.isPhone() ? this.rightPane: this.leftPane; |
| 194 |
to.containerNode.appendChild(this.leftView.domNode); |
| 195 |
}, |
| 196 |
|
| 197 |
showLeftView: function(){ |
| 198 |
|
| 199 |
|
| 200 |
this.leftPane.domNode.style.display = this.isPhone() ? "none" : ""; |
| 201 |
this.leftView.show(); |
| 202 |
}, |
| 203 |
|
| 204 |
showRightView: function(){ |
| 205 |
|
| 206 |
|
| 207 |
if(this.isPhone()){ return; } |
| 208 |
var view = this.getShowingView(); |
| 209 |
if(view){ |
| 210 |
view.show(); |
| 211 |
}else{ |
| 212 |
this.leftItemSelected(); |
| 213 |
} |
| 214 |
}, |
| 215 |
|
| 216 |
updateSelectedItem: function(){ |
| 217 |
|
| 218 |
|
| 219 |
var id; |
| 220 |
var view = this.getShowingView(); |
| 221 |
if(view && !this.isPhone()){ |
| 222 |
id = view.id; |
| 223 |
} |
| 224 |
if(id){ |
| 225 |
var items = array.filter(this.leftList.getChildren(), |
| 226 |
function(item){ return this.getDestinationId(item) === id; }, this); |
| 227 |
if(items && items.length > 0){ |
| 228 |
items[0].set("selected", true); |
| 229 |
} |
| 230 |
}else{ |
| 231 |
this.leftList.deselectAll && this.leftList.deselectAll(); |
| 232 |
} |
| 233 |
}, |
| 234 |
|
| 235 |
leftItemSelected: function( ){ |
| 236 |
|
| 237 |
|
| 238 |
}, |
| 239 |
|
| 240 |
transformUI: function(mode){ |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
this.init(); |
| 247 |
if(mode === this._currentMode){ return; } |
| 248 |
this._currentMode = mode; |
| 249 |
this.updateStateful(); |
| 250 |
this.updateBackButton(); |
| 251 |
this.updateTransition(); |
| 252 |
this.moveList(); |
| 253 |
this.showLeftView(); |
| 254 |
this.showRightView(); |
| 255 |
this.updateSelectedItem(); |
| 256 |
} |
| 257 |
}); |
| 258 |
|
| 259 |
cls._instance = null; |
| 260 |
cls.getInstance = function(){ |
| 261 |
if(!cls._instance){ |
| 262 |
cls._instance = new cls(); |
| 263 |
} |
| 264 |
return cls._instance; |
| 265 |
}; |
| 266 |
|
| 267 |
return cls; |
| 268 |
}); |