Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.off.ui"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.off.ui"] = true;
3
dojo.provide("dojox.off.ui");
4
 
5
dojo.require("dojox.storage.Provider");
6
dojo.require("dojox.storage.manager");
7
dojo.require("dojox.storage.GearsStorageProvider");
8
 
9
// Author: Brad Neuberg, bkn3@columbia.edu, http://codinginparadise.org
10
 
11
// summary:
12
//	dojox.off.ui provides a standard,
13
//	default user-interface for a
14
//	Dojo Offline Widget that can easily
15
//	be dropped into applications that would
16
//	like to work offline.
17
dojo.mixin(dojox.off.ui, {
18
	// appName: String
19
	//	This application's name, such as "Foobar". Note that
20
	//	this is a string, not HTML, so embedded markup will
21
	//	not work, including entities. Only the following
22
	//	characters are allowed: numbers, letters, and spaces.
23
	//	You must set this property.
24
	appName: "setme",
25
 
26
	// autoEmbed: boolean
27
	//	For advanced usage; most developers can ignore this.
28
	//	Whether to automatically auto-embed the default Dojo Offline
29
	//	widget into this page; default is true.
30
	autoEmbed: true,
31
 
32
	// autoEmbedID: String
33
	//	For advanced usage; most developers can ignore this.
34
	//	The ID of the DOM element that will contain our
35
	//	Dojo Offline widget; defaults to the ID 'dot-widget'.
36
	autoEmbedID: "dot-widget",
37
 
38
	// runLink: String
39
	//	For advanced usage; most developers can ignore this.
40
	//	The URL that should be navigated to to run this
41
	//	application offline; this will be placed inside of a
42
	//	link that the user can drag to their desktop and double
43
	//	click. Note that this URL must exactly match the URL
44
	//	of the main page of our resource that is offline for
45
	//	it to be retrieved from the offline cache correctly.
46
	//	For example, if you have cached your main page as
47
	//	http://foobar.com/index.html, and you set this to
48
	//	http://www.foobar.com/index.html, the run link will
49
	//	not work. By default this value is automatically set to
50
	//	the URL of this page, so it does not need to be set
51
	//	manually unless you have unusual needs.
52
	runLink: window.location.href,
53
 
54
	// runLinkTitle: String
55
	//	For advanced usage; most developers can ignore this.
56
	//	The text that will be inside of the link that a user
57
	//	can drag to their desktop to run this application offline.
58
	//	By default this is automatically set to "Run " plus your
59
	//	application's name.
60
	runLinkTitle: "Run Application",
61
 
62
	// learnHowPath: String
63
	//	For advanced usage; most developers can ignore this.
64
	//	The path to a web page that has information on
65
	//	how to use this web app offline; defaults to
66
	//	src/off/ui-template/learnhow.html, relative to
67
	//	your Dojo installation. Make sure to set
68
	//	dojo.to.ui.customLearnHowPath to true if you want
69
	//	a custom Learn How page.
70
	learnHowPath: dojo.moduleUrl("dojox", "off/resources/learnhow.html"),
71
 
72
	// customLearnHowPath: boolean
73
	//	For advanced usage; most developers can ignore this.
74
	//	Whether the developer is using their own custom page
75
	//	for the Learn How instructional page; defaults to false.
76
	//	Use in conjunction with dojox.off.ui.learnHowPath.
77
	customLearnHowPath: false,
78
 
79
	htmlTemplatePath: dojo.moduleUrl("dojox", "off/resources/offline-widget.html").uri,
80
	cssTemplatePath: dojo.moduleUrl("dojox", "off/resources/offline-widget.css").uri,
81
	onlineImagePath: dojo.moduleUrl("dojox", "off/resources/greenball.png").uri,
82
	offlineImagePath: dojo.moduleUrl("dojox", "off/resources/redball.png").uri,
83
	rollerImagePath: dojo.moduleUrl("dojox", "off/resources/roller.gif").uri,
84
	checkmarkImagePath: dojo.moduleUrl("dojox", "off/resources/checkmark.png").uri,
85
	learnHowJSPath: dojo.moduleUrl("dojox", "off/resources/learnhow.js").uri,
86
 
87
	_initialized: false,
88
 
89
	onLoad: function(){
90
		// summary:
91
		//	A function that should be connected to allow your
92
		//	application to know when Dojo Offline, the page, and
93
		//	the Offline Widget are all initialized and ready to be
94
		//	used:
95
		//
96
		//		dojo.connect(dojox.off.ui, "onLoad", someFunc)
97
	},
98
 
99
	_initialize: function(){
100
		//console.debug("dojox.off.ui._initialize");
101
 
102
		// make sure our app name is correct
103
		if(this._validateAppName(this.appName) == false){
104
			alert("You must set dojox.off.ui.appName; it can only contain "
105
					+ "letters, numbers, and spaces; right now it "
106
					+ "is incorrectly set to '" + dojox.off.ui.appName + "'");
107
			dojox.off.enabled = false;
108
			return;
109
		}
110
 
111
		// set our run link text to its default
112
		this.runLinkText = "Run " + this.appName;
113
 
114
		// setup our event listeners for Dojo Offline events
115
		// to update our UI
116
		dojo.connect(dojox.off, "onNetwork", this, "_onNetwork");
117
		dojo.connect(dojox.off.sync, "onSync", this, "_onSync");
118
 
119
		// cache our default UI resources
120
		dojox.off.files.cache([
121
							this.htmlTemplatePath,
122
							this.cssTemplatePath,
123
							this.onlineImagePath,
124
							this.offlineImagePath,
125
							this.rollerImagePath,
126
							this.checkmarkImagePath
127
							]);
128
 
129
		// embed the offline widget UI
130
		if(this.autoEmbed){
131
			this._doAutoEmbed();
132
		}
133
	},
134
 
135
	_doAutoEmbed: function(){
136
		// fetch our HTML for the offline widget
137
 
138
		// dispatch the request
139
		dojo.xhrGet({
140
			url:	 this.htmlTemplatePath,
141
			handleAs:	"text",
142
			error:		function(err){
143
				dojox.off.enabled = false;
144
				err = err.message||err;
145
				alert("Error loading the Dojo Offline Widget from "
146
						+ this.htmlTemplatePath + ": " + err);
147
			},
148
			load:		dojo.hitch(this, this._templateLoaded)
149
		});
150
	},
151
 
152
	_templateLoaded: function(data){
153
		//console.debug("dojox.off.ui._templateLoaded");
154
		// inline our HTML
155
		var container = dojo.byId(this.autoEmbedID);
156
		if(container){ container.innerHTML = data; }
157
 
158
		// fill out our image paths
159
		this._initImages();
160
 
161
		// update our network indicator status ball
162
		this._updateNetIndicator();
163
 
164
		// update our 'Learn How' text
165
		this._initLearnHow();
166
 
167
		this._initialized = true;
168
 
169
		// check offline cache settings
170
		if(!dojox.off.hasOfflineCache){
171
			this._showNeedsOfflineCache();
172
			return;
173
		}
174
 
175
		// check to see if we need a browser restart
176
		// to be able to use this web app offline
177
		if(dojox.off.hasOfflineCache && dojox.off.browserRestart){
178
			this._needsBrowserRestart();
179
			return;
180
		}else{
181
			var browserRestart = dojo.byId("dot-widget-browser-restart");
182
			if(browserRestart){ browserRestart.style.display = "none"; }
183
		}
184
 
185
		// update our sync UI
186
		this._updateSyncUI();
187
 
188
		// register our event listeners for our main buttons
189
		this._initMainEvtHandlers();
190
 
191
		// if offline functionality is disabled, disable everything
192
		this._setOfflineEnabled(dojox.off.enabled);
193
 
194
		// update our UI based on the state of the network
195
		this._onNetwork(dojox.off.isOnline ? "online" : "offline");
196
 
197
		// try to go online
198
		this._testNet();
199
	},
200
 
201
	_testNet: function(){
202
		dojox.off.goOnline(dojo.hitch(this, function(isOnline){
203
			//console.debug("testNet callback, isOnline="+isOnline);
204
 
205
			// display our online/offline results
206
			this._onNetwork(isOnline ? "online" : "offline");
207
 
208
			// indicate that our default UI
209
			// and Dojo Offline are now ready to
210
			// be used
211
			this.onLoad();
212
		}));
213
	},
214
 
215
	_updateNetIndicator: function(){
216
		var onlineImg = dojo.byId("dot-widget-network-indicator-online");
217
		var offlineImg = dojo.byId("dot-widget-network-indicator-offline");
218
		var titleText = dojo.byId("dot-widget-title-text");
219
 
220
		if(onlineImg && offlineImg){
221
			if(dojox.off.isOnline == true){
222
				onlineImg.style.display = "inline";
223
				offlineImg.style.display = "none";
224
			}else{
225
				onlineImg.style.display = "none";
226
				offlineImg.style.display = "inline";
227
			}
228
		}
229
 
230
		if(titleText){
231
			if(dojox.off.isOnline){
232
				titleText.innerHTML = "Online";
233
			}else{
234
				titleText.innerHTML = "Offline";
235
			}
236
		}
237
	},
238
 
239
	_initLearnHow: function(){
240
		var learnHow = dojo.byId("dot-widget-learn-how-link");
241
 
242
		if(!learnHow){ return; }
243
 
244
		if(!this.customLearnHowPath){
245
			// add parameters to URL so the Learn How page
246
			// can customize itself and display itself
247
			// correctly based on framework settings
248
			var dojoPath = djConfig.baseRelativePath;
249
			this.learnHowPath += "?appName=" + encodeURIComponent(this.appName)
250
									+ "&hasOfflineCache=" + dojox.off.hasOfflineCache
251
									+ "&runLink=" + encodeURIComponent(this.runLink)
252
									+ "&runLinkText=" + encodeURIComponent(this.runLinkText)
253
									+ "&baseRelativePath=" + encodeURIComponent(dojoPath);
254
 
255
			// cache our Learn How JavaScript page and
256
			// the HTML version with full query parameters
257
			// so it is available offline without a cache miss
258
			dojox.off.files.cache(this.learnHowJSPath);
259
			dojox.off.files.cache(this.learnHowPath);
260
		}
261
 
262
		learnHow.setAttribute("href", this.learnHowPath);
263
 
264
		var appName = dojo.byId("dot-widget-learn-how-app-name");
265
 
266
		if(!appName){ return; }
267
 
268
		appName.innerHTML = "";
269
		appName.appendChild(document.createTextNode(this.appName));
270
	},
271
 
272
	_validateAppName: function(appName){
273
		if(!appName){ return false; }
274
 
275
		return (/^[a-z0-9 ]*$/i.test(appName));
276
	},
277
 
278
	_updateSyncUI: function(){
279
		var roller = dojo.byId("dot-roller");
280
		var checkmark = dojo.byId("dot-success-checkmark");
281
		var syncMessages = dojo.byId("dot-sync-messages");
282
		var details = dojo.byId("dot-sync-details");
283
		var cancel = dojo.byId("dot-sync-cancel");
284
 
285
		if(dojox.off.sync.isSyncing){
286
			this._clearSyncMessage();
287
 
288
			if(roller){ roller.style.display = "inline"; }
289
 
290
			if(checkmark){ checkmark.style.display = "none"; }
291
 
292
			if(syncMessages){
293
				dojo.removeClass(syncMessages, "dot-sync-error");
294
			}
295
 
296
			if(details){ details.style.display = "none"; }
297
 
298
			if(cancel){ cancel.style.display = "inline"; }
299
		}else{
300
			if(roller){ roller.style.display = "none"; }
301
 
302
			if(cancel){ cancel.style.display = "none"; }
303
 
304
			if(syncMessages){
305
				dojo.removeClass(syncMessages, "dot-sync-error");
306
			}
307
		}
308
	},
309
 
310
	_setSyncMessage: function(message){
311
		var syncMessage = dojo.byId("dot-sync-messages");
312
		if(syncMessage){
313
			// when used with Google Gears pre-release in Firefox/Mac OS X,
314
			// the browser would crash when testing in Moxie
315
			// if we set the message this way for some reason.
316
			// Brad Neuberg, bkn3@columbia.edu
317
			//syncMessage.innerHTML = message;
318
 
319
			while(syncMessage.firstChild){
320
				syncMessage.removeChild(syncMessage.firstChild);
321
			}
322
			syncMessage.appendChild(document.createTextNode(message));
323
		}
324
	},
325
 
326
	_clearSyncMessage: function(){
327
		this._setSyncMessage("");
328
	},
329
 
330
	_initImages: function(){
331
		var onlineImg = dojo.byId("dot-widget-network-indicator-online");
332
		if(onlineImg){
333
			onlineImg.setAttribute("src", this.onlineImagePath);
334
		}
335
 
336
		var offlineImg = dojo.byId("dot-widget-network-indicator-offline");
337
		if(offlineImg){
338
			offlineImg.setAttribute("src", this.offlineImagePath);
339
		}
340
 
341
		var roller = dojo.byId("dot-roller");
342
		if(roller){
343
			roller.setAttribute("src", this.rollerImagePath);
344
		}
345
 
346
		var checkmark = dojo.byId("dot-success-checkmark");
347
		if(checkmark){
348
			checkmark.setAttribute("src", this.checkmarkImagePath);
349
		}
350
	},
351
 
352
	_showDetails: function(evt){
353
		// cancel the button's default behavior
354
		evt.preventDefault();
355
		evt.stopPropagation();
356
 
357
		if(!dojox.off.sync.details.length){
358
			return;
359
		}
360
 
361
		// determine our HTML message to display
362
		var html = "";
363
		html += "<html><head><title>Sync Details</title><head><body>";
364
		html += "<h1>Sync Details</h1>\n";
365
		html += "<ul>\n";
366
		for(var i = 0; i < dojox.off.sync.details.length; i++){
367
			html += "<li>";
368
			html += dojox.off.sync.details[i];
369
			html += "</li>";
370
		}
371
		html += "</ul>\n";
372
		html += "<a href='javascript:window.close()' "
373
				 + "style='text-align: right; padding-right: 2em;'>"
374
				 + "Close Window"
375
				 + "</a>\n";
376
		html += "</body></html>";
377
 
378
		// open a popup window with this message
379
		var windowParams = "height=400,width=600,resizable=true,"
380
							+ "scrollbars=true,toolbar=no,menubar=no,"
381
							+ "location=no,directories=no,dependent=yes";
382
 
383
		var popup = window.open("", "SyncDetails", windowParams);
384
 
385
		if(!popup){ // aggressive popup blocker
386
			alert("Please allow popup windows for this domain; can't display sync details window");
387
			return;
388
		}
389
 
390
		popup.document.open();
391
		popup.document.write(html);
392
		popup.document.close();
393
 
394
		// put the focus on the popup window
395
		if(popup.focus){
396
			popup.focus();
397
		}
398
	},
399
 
400
	_cancel: function(evt){
401
		// cancel the button's default behavior
402
		evt.preventDefault();
403
		evt.stopPropagation();
404
 
405
		dojox.off.sync.cancel();
406
	},
407
 
408
	_needsBrowserRestart: function(){
409
		var browserRestart = dojo.byId("dot-widget-browser-restart");
410
		if(browserRestart){
411
			dojo.addClass(browserRestart, "dot-needs-browser-restart");
412
		}
413
 
414
		var appName = dojo.byId("dot-widget-browser-restart-app-name");
415
		if(appName){
416
			appName.innerHTML = "";
417
			appName.appendChild(document.createTextNode(this.appName));
418
		}
419
 
420
		var status = dojo.byId("dot-sync-status");
421
		if(status){
422
			status.style.display = "none";
423
		}
424
	},
425
 
426
	_showNeedsOfflineCache: function(){
427
		var widgetContainer = dojo.byId("dot-widget-container");
428
		if(widgetContainer){
429
			dojo.addClass(widgetContainer, "dot-needs-offline-cache");
430
		}
431
	},
432
 
433
	_hideNeedsOfflineCache: function(){
434
		var widgetContainer = dojo.byId("dot-widget-container");
435
		if(widgetContainer){
436
			dojo.removeClass(widgetContainer, "dot-needs-offline-cache");
437
		}
438
	},
439
 
440
	_initMainEvtHandlers: function(){
441
		var detailsButton = dojo.byId("dot-sync-details-button");
442
		if(detailsButton){
443
			dojo.connect(detailsButton, "onclick", this, this._showDetails);
444
		}
445
		var cancelButton = dojo.byId("dot-sync-cancel-button");
446
		if(cancelButton){
447
			dojo.connect(cancelButton, "onclick", this, this._cancel);
448
		}
449
	},
450
 
451
	_setOfflineEnabled: function(enabled){
452
		var elems = [];
453
		elems.push(dojo.byId("dot-sync-status"));
454
 
455
		for(var i = 0; i < elems.length; i++){
456
			if(elems[i]){
457
				elems[i].style.visibility =
458
							(enabled ? "visible" : "hidden");
459
			}
460
		}
461
	},
462
 
463
	_syncFinished: function(){
464
		this._updateSyncUI();
465
 
466
		var checkmark = dojo.byId("dot-success-checkmark");
467
		var details = dojo.byId("dot-sync-details");
468
 
469
		if(dojox.off.sync.successful == true){
470
			this._setSyncMessage("Sync Successful");
471
			if(checkmark){ checkmark.style.display = "inline"; }
472
		}else if(dojox.off.sync.cancelled == true){
473
			this._setSyncMessage("Sync Cancelled");
474
 
475
			if(checkmark){ checkmark.style.display = "none"; }
476
		}else{
477
			this._setSyncMessage("Sync Error");
478
 
479
			var messages = dojo.byId("dot-sync-messages");
480
			if(messages){
481
				dojo.addClass(messages, "dot-sync-error");
482
			}
483
 
484
			if(checkmark){ checkmark.style.display = "none"; }
485
		}
486
 
487
		if(dojox.off.sync.details.length && details){
488
			details.style.display = "inline";
489
		}
490
	},
491
 
492
	_onFrameworkEvent: function(type, saveData){
493
		if(type == "save"){
494
			if(saveData.status == dojox.storage.FAILED && !saveData.isCoreSave){
495
				alert("Please increase the amount of local storage available "
496
						+ "to this application");
497
				if(dojox.storage.hasSettingsUI()){
498
					dojox.storage.showSettingsUI();
499
				}
500
 
501
				// FIXME: Be able to know if storage size has changed
502
				// due to user configuration
503
			}
504
		}else if(type == "coreOperationFailed"){
505
			console.log("Application does not have permission to use Dojo Offline");
506
 
507
			if(!this._userInformed){
508
				alert("This application will not work if Google Gears is not allowed to run");
509
				this._userInformed = true;
510
			}
511
		}else if(type == "offlineCacheInstalled"){
512
			// clear out the 'needs offline cache' info
513
			this._hideNeedsOfflineCache();
514
 
515
			// check to see if we need a browser restart
516
			// to be able to use this web app offline
517
			if(dojox.off.hasOfflineCache == true
518
				&& dojox.off.browserRestart == true){
519
				this._needsBrowserRestart();
520
				return;
521
			}else{
522
				var browserRestart = dojo.byId("dot-widget-browser-restart");
523
				if(browserRestart){
524
					browserRestart.style.display = "none";
525
				}
526
			}
527
 
528
			// update our sync UI
529
			this._updateSyncUI();
530
 
531
			// register our event listeners for our main buttons
532
			this._initMainEvtHandlers();
533
 
534
			// if offline is disabled, disable everything
535
			this._setOfflineEnabled(dojox.off.enabled);
536
 
537
			// try to go online
538
			this._testNet();
539
		}
540
	},
541
 
542
	_onSync: function(type){
543
		//console.debug("ui, onSync="+type);
544
		switch(type){
545
			case "start":
546
				this._updateSyncUI();
547
				break;
548
 
549
			case "refreshFiles":
550
				this._setSyncMessage("Downloading UI...");
551
				break;
552
 
553
			case "upload":
554
				this._setSyncMessage("Uploading new data...");
555
				break;
556
 
557
			case "download":
558
				this._setSyncMessage("Downloading new data...");
559
				break;
560
 
561
			case "finished":
562
				this._syncFinished();
563
				break;
564
 
565
			case "cancel":
566
				this._setSyncMessage("Canceling Sync...");
567
				break;
568
 
569
			default:
570
				dojo.warn("Programming error: "
571
							+ "Unknown sync type in dojox.off.ui: " + type);
572
				break;
573
		}
574
	},
575
 
576
	_onNetwork: function(type){
577
		// summary:
578
		//	Called when we go on- or off-line
579
		// description:
580
		//	When we go online or offline, this method is called to update
581
		//	our UI. Default behavior is to update the Offline
582
		//	Widget UI and to attempt a synchronization.
583
		// type: String
584
		//	"online" if we just moved online, and "offline" if we just
585
		//	moved offline.
586
 
587
		if(!this._initialized){ return; }
588
 
589
		// update UI
590
		this._updateNetIndicator();
591
 
592
		if(type == "offline"){
593
			this._setSyncMessage("You are working offline");
594
 
595
			// clear old details
596
			var details = dojo.byId("dot-sync-details");
597
			if(details){ details.style.display = "none"; }
598
 
599
			// if we fell offline during a sync, hide
600
			// the sync info
601
			this._updateSyncUI();
602
		}else{ // online
603
			// synchronize, but pause for a few seconds
604
			// so that the user can orient themselves
605
			if(dojox.off.sync.autoSync){
606
				window.setTimeout("dojox.off.sync.synchronize()", 1000);
607
			}
608
		}
609
	}
610
});
611
 
612
// register ourselves for low-level framework events
613
dojo.connect(dojox.off, "onFrameworkEvent", dojox.off.ui, "_onFrameworkEvent");
614
 
615
// start our magic when the Dojo Offline framework is ready to go
616
dojo.connect(dojox.off, "onLoad", dojox.off.ui, dojox.off.ui._initialize);
617
 
618
}