Wikipedia/main.js
author ivanl
Tue, 09 Mar 2010 10:34:58 +0000
changeset 20 918767a9c8d3
permissions -rw-r--r--
Adding Wikipedia Widget 1.0rc5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
     1
// ////////////////////////////////////////////////////////////////////////////
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
     2
// Symbian Foundation Example Code
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
     3
//
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
     4
// This software is in the public domain. No copyright is claimed, and you 
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
     5
// may use it for any purpose without license from the Symbian Foundation.
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
     6
// No warranty for any purpose is expressed or implied by the authors or
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
     7
// the Symbian Foundation. 
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
     8
// ////////////////////////////////////////////////////////////////////////////
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
     9
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    10
// Reference to the WRTKit user interface manager and main view.
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    11
var uiManager;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    12
var wikiHomeView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    13
var wikiHistoryView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    14
var wikiBookmarksView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    15
var languageSelectView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    16
var helpView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    17
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    18
// Update variables
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    19
var myversion = "1.0rc5";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    20
var updateBaseUrl = "http://developer.symbian.org/wiki/" 
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    21
var versionWikiPageUrl = updateBaseUrl + "index.php/Wikipedia_Reader_Widget";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    22
var versionWikiPageString = "Current widget version is [";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    23
var downloadUrl = updateBaseUrl + "images/b/b3/Wikipedia.wgz";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    24
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    25
var resultsPerPage = 10;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    26
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    27
// we support variety of screen sizes and orientations
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    28
var portrait;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    29
var touchscreen;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    30
var miniview;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    31
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    32
var initCalled = false;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    33
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    34
// Called from the onload event handler to initialize the widget.
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    35
function init(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    36
	if (window.widget) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    37
		setInterval("Noop();", 10000);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    38
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    39
	try{
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    40
		doUiInit();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    41
	} catch(x) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    42
		alert(x);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    43
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    44
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    45
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    46
function doUiInit(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    47
    // set tab-navigation mode and show softkeys
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    48
    // (only if we are in the WRT environment)
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    49
    if (window.widget) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    50
        widget.setNavigationEnabled(false);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    51
        menu.showSoftkeys();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    52
    }
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    53
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    54
    // create UI manager
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    55
	var elem = document.getElementById("uimandiv");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    56
    uiManager = new UIManager(elem,null,true);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    57
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    58
    // create main view
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    59
    wikiHomeView = new WikiHome();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    60
	wikiHistoryView = new HistoryView();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    61
	wikiHistoryView.previousView = wikiHomeView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    62
	wikiBookmarksView = new BookmarksView();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    63
	wikiBookmarksView.previousView = wikiHomeView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    64
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    65
	initHelpView();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    66
	initLanguageView();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    67
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    68
	initCalled = true;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    69
	setViewMode();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    70
	
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    71
//	setupLog();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    72
	restoreLanguage();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    73
    // display the main view
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    74
	wikiHomeView.show();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    75
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    76
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    77
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    78
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    79
function setViewMode() {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    80
	if ( !initCalled ) return;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    81
	portrait = window.innerWidth < window.innerHeight;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    82
	touchscreen = Math.min(window.innerWidth,window.innerHeight) > 240;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    83
	miniview = window.innerHeight < 150;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    84
	if ( miniview ) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    85
		var main = document.getElementById("uimandiv");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    86
		var mini = document.getElementById("minidiv");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    87
		main.style.display = "none";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    88
		mini.style.display = "";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    89
	} else {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    90
		// no preference available, check screen size
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    91
		if ( touchscreen ) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    92
			// hi res screen, use large font
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    93
			setLargeView();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    94
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    95
		else {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    96
			// lo res screen, use small font
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    97
			setSmallView();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    98
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    99
		var main = document.getElementById("uimandiv");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   100
		var mini = document.getElementById("minidiv");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   101
		main.style.display = "";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   102
		mini.style.display = "none";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   103
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   104
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   105
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   106
function setLargeView(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   107
	document.getElementById('stylesheet').href = 'WRTKit/Resources/UI-large.css';
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   108
//	setCssBodyFontSize(22);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   109
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   110
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   111
function setSmallView(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   112
	document.getElementById('stylesheet').href = 'WRTKit/Resources/UI.css';
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   113
//	setCssBodyFontSize(14);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   114
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   115
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   116
function Noop(){}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   117
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   118
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   119
// /////////////////////////////////////////////////////////////////////////////////
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   120
// Help view setup
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   121
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   122
function showHelp(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   123
	helpView.show();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   124
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   125
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   126
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   127
function initHelpView(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   128
	helpView = new ListView();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   129
	var ajax = null;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   130
	try {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   131
		ajax = new Ajax();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   132
		ajax.open("GET", "help.txt", true);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   133
		ajax.onreadystatechange = function(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   134
			HelpLoadComplete(ajax);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   135
		};
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   136
		ajax.send(null);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   137
	} 
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   138
	catch (e) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   139
		alert(e);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   140
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   141
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   142
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   143
function HelpLoadComplete(ajax){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   144
    if (ajax.readyState == 4) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   145
		var text;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   146
		if (ajax == null || ajax.responseText == undefined || ajax.responseText == null) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   147
			text = "No help available.";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   148
//			alert("responseText=" + ajax.responseText + ", responseXML=" + ajax.responseXml + ", status=" + ajax.status);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   149
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   150
		else {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   151
			text = ajax.responseText;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   152
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   153
		var caption = new NavigationButton(1, "titlebar.png", "Help", true);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   154
		caption.addEventListener("ActionPerformed", function(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   155
			wikiHomeView.show();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   156
		});
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   157
		helpView.addControl(caption);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   158
		var docModel = ParseIntoDocModel(text);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   159
		var preamble = new TextPane(null, null, docModel.preface);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   160
		helpView.addControl(preamble);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   161
		for (var i = 0; i < docModel.sections.length; i++) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   162
			if (docModel.sections[i].caption == "Contents") {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   163
				continue;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   164
			}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   165
			var control = new ContentPanel(null, docModel.sections[i].caption, docModel.sections[i].content, true, false); // foldable & expanded
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   166
			helpView.addControl(control);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   167
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   168
		
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   169
		helpView.previousView = wikiHomeView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   170
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   171
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   172
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   173
function addHelpMenuItems(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   174
	var languageMenuItem = new MenuItem("Language", MENU_ITEM_MAIN_LANGUAGE);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   175
	languageMenuItem.onSelect = selectLanguage;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   176
	menu.append(languageMenuItem);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   177
	var updateMenuItem = new MenuItem("Check for updates", MENU_ITEM_MAIN_UPDATES);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   178
	updateMenuItem.onSelect = checkForUpdates;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   179
	menu.append(updateMenuItem);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   180
	var helpMenuItem = new MenuItem("Help", MENU_ITEM_MAIN_HELP);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   181
	helpMenuItem.onSelect = showHelp;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   182
	menu.append(helpMenuItem);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   183
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   184
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   185
// ////////////////////////////////////////////////////////////////////////
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   186
// Language selection
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   187
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   188
function setLanguage(lang) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   189
	baseUrl = lang;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   190
	wikiViewPageUrlBase = baseUrl + "/w/index.php?title=";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   191
	uiManager.showNotification(1000, "info", "Language set");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   192
	if ( window.widget ) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   193
		widget.setPreferenceForKey(lang, "language");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   194
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   195
	wikiHomeView.show();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   196
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   197
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   198
function selectLanguage(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   199
	languageSelectView.previousView = uiManager.currentView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   200
	languageSelectView.show();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   201
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   202
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   203
function restoreLanguage(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   204
	if (window.widget) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   205
		var lang = widget.preferenceForKey("language");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   206
		if ( lang != undefined && lang != null ) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   207
			setLanguage(lang);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   208
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   209
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   210
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   211
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   212
function initLanguageView(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   213
	languageSelectView = new ListView();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   214
	var ajax = null;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   215
	try {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   216
		ajax = new Ajax();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   217
		ajax.open("GET", "lang.txt", true);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   218
		ajax.onreadystatechange = function(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   219
			LanguageLoadComplete(ajax);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   220
		};
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   221
		ajax.send(null);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   222
	} 
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   223
	catch (e) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   224
		alert(e);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   225
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   226
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   227
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   228
function LanguageLoadComplete(ajax){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   229
    if (ajax.readyState == 4) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   230
		var text;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   231
		if (ajax == null || ajax.responseText == undefined || ajax.responseText == null) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   232
			text = "Language list not available.";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   233
//			alert("responseText=" + ajax.responseText + ", responseXML=" + ajax.responseXml + ", status=" + ajax.status);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   234
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   235
		else {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   236
			text = ajax.responseText;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   237
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   238
		var caption = new NavigationButton(1, "titlebar.png", "Languages", true);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   239
		caption.addEventListener("ActionPerformed", function(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   240
			wikiHomeView.show();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   241
		});
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   242
		languageSelectView.addControl(caption);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   243
		var docModel = ParseIntoDocModel(text);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   244
		var preamble = new TextPane(null, null, docModel.preface);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   245
		languageSelectView.addControl(preamble);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   246
		for (var i = 0; i < docModel.sections.length; i++) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   247
			if (docModel.sections[i].caption == "Contents") {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   248
				continue;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   249
			}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   250
			var control = new ContentPanel(null, docModel.sections[i].caption, docModel.sections[i].content, true, false); // foldable & expanded
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   251
			languageSelectView.addControl(control);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   252
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   253
		languageSelectView.show = function (){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   254
				View.prototype.show.call(this);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   255
				if (window.widget) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   256
					widget.setNavigationEnabled(true);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   257
				} 
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   258
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   259
		languageSelectView.previousView = wikiHomeView;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   260
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   261
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   262
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   263
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   264
// auto update code
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   265
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   266
var updatePageAjax = null;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   267
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   268
function checkForUpdates() {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   269
	uiManager.showNotification(-1, "wait", "Checking for updates...", -1);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   270
	updatePageAjax = new Ajax();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   271
	updatePageAjax.onreadystatechange = checkForUpdatesStage2;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   272
	updatePageAjax.open('GET', nocache(versionWikiPageUrl), true);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   273
	updatePageAjax.send(null);	
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   274
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   275
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   276
function checkForUpdatesStage2() {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   277
    if (updatePageAjax.readyState == 4) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   278
		// extract version number
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   279
		var content = updatePageAjax.responseText;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   280
		var ind = content.indexOf(versionWikiPageString);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   281
		if ( ind == -1 ) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   282
			uiManager.showNotification(3000, "warning", "Update failed, check manually.");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   283
			return;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   284
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   285
		ind += versionWikiPageString.length;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   286
		var ind2 = content.indexOf("]",ind);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   287
		if ( ind2 == -1 || (ind2-ind) > 10 ) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   288
			uiManager.showNotification(3000, "warning", "Update failed, check manually.");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   289
			return;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   290
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   291
		var version = content.substring(ind,ind2);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   292
		// compare to this version
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   293
		if ( version != myversion ) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   294
			var answer = confirm("Install version " + version + "?\nNote: Update will clear history and bookmarks.");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   295
			if (answer) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   296
				// ok, we have the update
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   297
				uiManager.hideNotification();
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   298
				openURL(nocache(downloadUrl));
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   299
				setTimeout(function () {window.close();}, 1000);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   300
			} else {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   301
			uiManager.showNotification(3000, "info", "Update cancelled.");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   302
			}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   303
		} else {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   304
			uiManager.showNotification(3000, "info", "Up to date!");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   305
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   306
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   307
}