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