Wikipedia/Bookmarks.js
changeset 20 918767a9c8d3
equal deleted inserted replaced
19:f3521a11d878 20:918767a9c8d3
       
     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 var MILLIS_IN_A_DAY = 24*60*60*1000;
       
    11 
       
    12 function BookmarksViewItem(articleTitle, articleContent, articleBase, pos) {
       
    13 	var d = new Date();
       
    14 	this.articleTitle = articleTitle;
       
    15 	this.articleContent = articleContent; 
       
    16 	this.pos = pos;
       
    17 	this.articleBase = articleBase;
       
    18 	this.timestamp = d.getTime();
       
    19 }
       
    20 
       
    21 
       
    22 function BookmarksView (){
       
    23 	try {
       
    24 		ListView.prototype.init.call(this, null, null);	
       
    25 		var caption = new NavigationButton(1, "titlebar.png", "Bookmarks", true);
       
    26 		caption.addEventListener("ActionPerformed", function(){wikiHomeView.show();});
       
    27 		this.addControl(caption);
       
    28 		this.previousView = null;
       
    29 		this.items = new Array();
       
    30 		this.startindex = 0;
       
    31 		this.endindex = 0;
       
    32 		this.rendered = false;	
       
    33 		this.todayControl = null;
       
    34 		this.loadItems();
       
    35 	}catch(w) {alert(w);}
       
    36 }
       
    37 
       
    38 BookmarksView.prototype = new ListView(null, null);
       
    39 
       
    40 BookmarksView.prototype.items = null;
       
    41 
       
    42 
       
    43 BookmarksView.prototype.addItem = function(articleTitle, articleContent, articleBase){
       
    44 	try {
       
    45 		this.removeItem(articleTitle);
       
    46 		this.rendered = false;
       
    47 		var item = null;
       
    48 		item = new BookmarksViewItem(articleTitle, null /*articleContent*/, articleBase, this.endindex);
       
    49 		this.items[this.endindex - this.startindex] = item;
       
    50 		this.endindex++;
       
    51 		this.saveItem(item);
       
    52 	}catch(w) { alert(w);}
       
    53 }
       
    54 
       
    55 
       
    56 BookmarksView.prototype.saveItem = function(item){
       
    57 	if ( window.widget ) {
       
    58 //		alert("BM: Saving item: pos=" + item.pos +", title=" + item.articleTitle +", ts=" + item.timestamp
       
    59 //		+ "titlekey:" + getBookmarksTitleKey(item.pos) + ", timekey: " + getBookmarksTimeKey(item.pos)	);
       
    60 
       
    61 		widget.setPreferenceForKey(item.articleTitle, getBookmarksTitleKey(item.pos));
       
    62 		//widget.setPreferenceForKey(item.articleContent, getBookmarksContentKey(item.pos));
       
    63 		widget.setPreferenceForKey(item.articleBase, getBookmarksBaseKey(item.pos));
       
    64 		widget.setPreferenceForKey(""+item.timestamp, getBookmarksTimeKey(item.pos));
       
    65 		this.storeIndex();
       
    66 	}
       
    67 }
       
    68 
       
    69 BookmarksView.prototype.storeIndex = function () {
       
    70 	widget.setPreferenceForKey("" + this.startindex, "boox-start");
       
    71 	widget.setPreferenceForKey("" + this.endindex, "boox-end");
       
    72 //	alert("BM: index stored, start:"+this.startindex + ", end:"+this.endindex);
       
    73 }
       
    74 
       
    75 
       
    76 BookmarksView.prototype.clear = function(){
       
    77 	this.items = new Array();
       
    78 	if (window.widget) {
       
    79 		for (var i = this.startindex; i < this.endindex + 1; i++) {
       
    80 			widget.setPreferenceForKey(null, getBookmarksTitleKey(i));
       
    81 			widget.setPreferenceForKey(null, getBookmarksContentKey(i));
       
    82 			widget.setPreferenceForKey(null, getBookmarksBaseKey(i));
       
    83 			widget.setPreferenceForKey(null, getBookmarksTimeKey(i));
       
    84 		}
       
    85 		this.startindex = 0;
       
    86 		this.endindex = 0;
       
    87 		this.storeIndex();
       
    88 		this.rendered = false;
       
    89 		this.render();
       
    90 	}
       
    91 }
       
    92 
       
    93 BookmarksView.prototype.loadItems = function(){
       
    94 	uiManager.showNotification(-1, "wait", "Loading...");
       
    95 	var self = this;
       
    96 	setTimeout(function(){self.doLoadItems();},0);
       
    97 }
       
    98 
       
    99 
       
   100 BookmarksView.prototype.doLoadItems = function(){
       
   101 	if ( window.widget ) {
       
   102 		var d = new Date();
       
   103 		var now = d.getTime();
       
   104 		this.items = new Array();
       
   105 		var tmpstart 	= 	widget.preferenceForKey("boox-start");
       
   106 		var tmpend 		= 	widget.preferenceForKey("boox-end");
       
   107 
       
   108 		this.startindex = (( tmpstart != undefined && tmpstart != null ) ? parseInt(tmpstart) : 0);
       
   109 		this.endindex = (( tmpend != undefined && tmpend != null ) ? parseInt(tmpend) : 0);
       
   110 
       
   111 //		alert("BM: startindex=" + this.startindex +", endindex=" + this.endindex);
       
   112 
       
   113 		for (var i = this.startindex; i < this.endindex; i++) {
       
   114 			var pos =  i - this.startindex;
       
   115 			var title = widget.preferenceForKey(getBookmarksTitleKey(i));
       
   116 //			alert("BM: Examining item: i=" + i + ", pos=" + pos +", title=" + title );
       
   117 			if ( !title || title == null || title == "null") continue;
       
   118 			var item = new BookmarksViewItem(title,null,null,i);
       
   119 			item.articleContent = widget.preferenceForKey(getBookmarksContentKey(i));
       
   120 			item.articleBase = widget.preferenceForKey(getBookmarksBaseKey(i));
       
   121 			item.timestamp = parseFloat(widget.preferenceForKey(getBookmarksTimeKey(i)));
       
   122 			item.pos = i;
       
   123 			this.items[pos] = item;
       
   124 //			alert("BM: Loaded item: i=" + i + ", pos=" + pos +", title=" + title +", ts=" + item.timestamp);
       
   125 		}
       
   126 		this.rendered = false;
       
   127 	}
       
   128 	uiManager.hideNotification();
       
   129 }
       
   130 
       
   131 BookmarksView.prototype.render = function(){
       
   132 	if ( this.rendered ) return;
       
   133 	var d = new Date();
       
   134 	var now = d.getTime();
       
   135 	// clear components
       
   136 	this.removeAllControls();
       
   137 	var caption = new NavigationButton(1, "titlebar.png", "Bookmarks", true);
       
   138 	caption.addEventListener("ActionPerformed", function(){
       
   139 		wikiHomeView.show();
       
   140 	});
       
   141 	this.addControl(caption);
       
   142 	
       
   143 	// add components for bookmarks items, up to today
       
   144 	
       
   145 	var thecontrol = new ContentPanel(null, "All bookmarks", "", true, true);
       
   146 	thecontrol.setContent(this.renderBookmarksContent(0, now));
       
   147 	this.addControl(thecontrol);
       
   148 	this.rendered = true;
       
   149 }
       
   150 
       
   151 BookmarksView.prototype.renderBookmarksContent = function (from , to) {
       
   152 	var buf = "<table class='historytable'>";
       
   153 	for ( var i = this.endindex ; i >= this.startindex ; i -- ) {
       
   154 		var ind = i - this.startindex;
       
   155 		if ( this.items[ind] == undefined || this.items[ind] == null ) continue;
       
   156 		if ( this.items[ind].timestamp < from ) break;
       
   157 		if ( this.items[ind].timestamp < to ) {
       
   158 			buf += this.renderBookmarksItem(this.items[ind]);			
       
   159 		}
       
   160 	}
       
   161 	buf += "</table>"
       
   162 	return buf;
       
   163 }
       
   164 
       
   165 BookmarksView.prototype.renderBookmarksItem = function(item){
       
   166 	if ( item == undefined || item == null ) return "";
       
   167     var buf = "<tr><td><small>";
       
   168 	buf += shortFormatTime(item.timestamp);
       
   169 	buf += "</small></td><td width=70%>";	 
       
   170 	buf += "<div class=\"FeedItemLink\">";
       
   171 	buf += "<a href=\"JavaScript:void(0)\" onclick=\"wikiBookmarksView.goTo(" ;
       
   172 	buf += item.pos;
       
   173 	buf +=  "); return false;\">";
       
   174 	buf += "<strong>";
       
   175 	buf += shorten(item.articleTitle, 30);
       
   176 	buf += "<strong></a>";
       
   177 	buf += "</div>";
       
   178 	buf += "</td><td><div class=\"FeedItemLink\">";
       
   179 	buf += "<a href=\"JavaScript:void(0)\" onclick=\"event.stopPropagation();wikiBookmarksView.deleteItem(" ;
       
   180 	buf += item.pos;
       
   181 	buf +=  "); wikiBookmarksView.render(); return false;\">";
       
   182 	buf += "<img src=delete.png border=0></a>";
       
   183 	buf += "</div>";
       
   184 	buf += "</td></tr>";
       
   185 	return buf;
       
   186 }
       
   187 
       
   188 BookmarksView.prototype.deleteItem = function(ind){	
       
   189 	try {
       
   190 	//	alert("BM: removeItem: ind=" + ind);
       
   191 		// remove item
       
   192 		this.items[ind-this.startindex] = null;
       
   193 		if (window.widget) {
       
   194 			widget.setPreferenceForKey(null, getBookmarksTitleKey(ind));
       
   195 			widget.setPreferenceForKey(null, getBookmarksContentKey(ind));
       
   196 			widget.setPreferenceForKey(null, getBookmarksBaseKey(ind));
       
   197 			widget.setPreferenceForKey(null, getBookmarksTimeKey(ind));
       
   198 		}
       
   199 		this.rendered = false;
       
   200 	}catch(ex) { alert(ex);}
       
   201 }
       
   202 
       
   203 BookmarksView.prototype.goTo = function(pos){
       
   204 	wikiBrowse(this.items[pos-this.startindex].articleTitle, false, this.items[pos-this.startindex].articleBase); // true means don't re-add to history	 	
       
   205 }
       
   206 
       
   207 
       
   208 
       
   209 BookmarksView.prototype.show = function(){
       
   210 	try {
       
   211 		this.render();
       
   212 	}catch(e){ alert(e);}
       
   213 	View.prototype.show.call(this);
       
   214 }
       
   215 
       
   216 
       
   217 // abstract function for updating per-view menu
       
   218 // only called if window.widget is defined
       
   219 BookmarksView.prototype.setupMenu = function(){
       
   220 	if ( window.widget ) {
       
   221 		menu.clear();
       
   222 		var clearMenuItem = new MenuItem("Clear", MENU_ITEM_BOOKMARKS_CLEAR);
       
   223 		var self = this; 
       
   224 		clearMenuItem.onSelect = function(){self.clear();};
       
   225 		menu.append(clearMenuItem);
       
   226 
       
   227 		var self = this;
       
   228 		var searchMenuItem = new MenuItem("Search", MENU_ITEM_MAIN_SEARCH); 
       
   229 		searchMenuItem.onSelect = function(){wikiHomeView.show();};
       
   230 		menu.append(searchMenuItem);
       
   231 		var historyMenuItem = new MenuItem("History", MENU_ITEM_MAIN_HISTORY); 
       
   232 		historyMenuItem.onSelect = function(){wikiHistoryView.previousView = self; wikiHistoryView.show();};
       
   233 		menu.append(historyMenuItem);
       
   234 		var bookmarksMenuItem = new MenuItem("Bookmarks", MENU_ITEM_MAIN_BOOKMARKS); 
       
   235 		bookmarksMenuItem.onSelect = function(){wikiBookmarksView.show();};
       
   236 		menu.append(bookmarksMenuItem);
       
   237 
       
   238 		addHelpMenuItems();
       
   239 	}
       
   240 }
       
   241 
       
   242 
       
   243 function getBookmarksKeyBase(pos) {
       
   244 	return "boox." + pos + "."; 
       
   245 }
       
   246 
       
   247 function getBookmarksTitleKey(pos) {
       
   248 	return getBookmarksKeyBase(pos) + "title"; 
       
   249 }
       
   250 
       
   251 function getBookmarksContentKey(pos) {
       
   252 	return getBookmarksKeyBase(pos) + "content"; 
       
   253 }
       
   254 
       
   255 function getBookmarksTimeKey(pos) {
       
   256 	return getBookmarksKeyBase(pos) + "time"; 
       
   257 }
       
   258 
       
   259 function getBookmarksBaseKey(pos) {
       
   260 	return getHistoryKeyBase(pos) + "base"; 
       
   261 }
       
   262 
       
   263 
       
   264 
       
   265 BookmarksView.prototype.removeItem = function(articleTitle){
       
   266 	// find index
       
   267 	var ind = this.findItemIdFromTitle(articleTitle)
       
   268 	if ( ind == -1 ) {
       
   269 		return;
       
   270 	}
       
   271 	deleteItem(ind);
       
   272 }
       
   273 
       
   274 BookmarksView.prototype.findItemIdFromTitle = function(title) {
       
   275 	var ind = -1;
       
   276 	for ( var i = this.startindex ; i < this.endindex + 1 ; i ++ ) {
       
   277 		var arrind = i-this.startindex;
       
   278 		if ( this.items[arrind] != undefined && this.items[arrind] != null && title == this.items[arrind].articleTitle) {
       
   279 			ind = i;
       
   280 			break;
       
   281 		}
       
   282 	}
       
   283 	return ind;
       
   284 }
       
   285 
       
   286 // return already downloaded content for an article or null
       
   287 // if we don't have it
       
   288 BookmarksView.prototype.getContentForArticle = function(articleTitle){
       
   289 	if (window.widget) {
       
   290 		var i = this.findItemIdFromTitle(articleTitle);
       
   291 		return widget.preferenceForKey(getBookmarksContentKey(i));
       
   292 	}
       
   293 	return null;
       
   294 }
       
   295