Wikipedia/History.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 var KEEP_HISTORY_FOR = 90 * MILLIS_IN_A_DAY; // 90 days
       
    12 var KEEP_CONTENT = false;
       
    13 
       
    14 function HistoryViewItem(articleTitle, articleContent, articleBase, pos) {
       
    15 	var d = new Date();
       
    16 	this.articleTitle = articleTitle;
       
    17 	this.articleContent = articleContent; 
       
    18 	this.articleBase = articleBase;
       
    19 	this.pos = pos;
       
    20 	this.timestamp = d.getTime();
       
    21 }
       
    22 
       
    23 
       
    24 function HistoryView (){
       
    25 	try {
       
    26 		ListView.prototype.init.call(this, null, null);	
       
    27 		var caption = new NavigationButton(1, "titlebar.png", "History", true);
       
    28 		caption.addEventListener("ActionPerformed", function(){wikiHomeView.show();});
       
    29 		this.addControl(caption);
       
    30 		this.previousView = null;
       
    31 		this.items = new Array();
       
    32 		this.startindex = 0;
       
    33 		this.endindex = 0;
       
    34 		this.current = 0;
       
    35 		this.lastRender = 0;	
       
    36 		this.todayControl = null;
       
    37 		this.loadItems();
       
    38 	}catch(w) {alert(w);}
       
    39 }
       
    40 
       
    41 HistoryView.prototype = new ListView(null, null);
       
    42 
       
    43 HistoryView.prototype.items = null;
       
    44 
       
    45 
       
    46 HistoryView.prototype.addItem = function(articleTitle, articleContent, articleBase){
       
    47 	// do not store it if its the same as previous one
       
    48 	if ( this.items[this.current] != undefined && this.items[this.current] != null && articleTitle == this.items[this.current].articleTitle){
       
    49 		return;
       
    50 	}
       
    51 	var item = null;
       
    52 	this.current = this.endindex;
       
    53 	if ( KEEP_CONTENT ){
       
    54 		item = new HistoryViewItem(articleTitle, articleContent, articleBase, this.current);
       
    55 	} else {
       
    56 		item = new HistoryViewItem(articleTitle, null, articleBase, this.current);	
       
    57 	}
       
    58 	this.items[this.current-this.startindex]= item;
       
    59 	this.endindex++;
       
    60 	this.saveItem(item);
       
    61 }
       
    62 
       
    63 
       
    64 HistoryView.prototype.saveItem = function(item){
       
    65 	if ( window.widget ) {
       
    66 //		alert("Saving item: pos=" + item.pos +", title=" + item.articleTitle +", ts=" + item.timestamp
       
    67 //		+ "titlekey:" + getHistoryTitleKey(item.pos) + ", timekey: " + getHistoryTimeKey(item.pos)	);
       
    68 
       
    69 		widget.setPreferenceForKey(item.articleTitle, getHistoryTitleKey(item.pos));
       
    70 		if (KEEP_CONTENT) {
       
    71 			widget.setPreferenceForKey(item.articleContent, getHistoryContentKey(item.pos));
       
    72 		}
       
    73 		widget.setPreferenceForKey(item.articleBase, getHistoryBaseKey(item.pos));
       
    74 		widget.setPreferenceForKey(""+item.timestamp, getHistoryTimeKey(item.pos));
       
    75 		this.storeIndex();
       
    76 	}
       
    77 }
       
    78 
       
    79 HistoryView.prototype.storeIndex = function () {
       
    80 	widget.setPreferenceForKey("" + this.startindex, "history.start");
       
    81 	widget.setPreferenceForKey("" + this.endindex, "history.end");
       
    82 	widget.setPreferenceForKey("" + this.current, "history.current");
       
    83 //	alert("index stored, start:"+this.startindex + ", end:"+this.endindex + ", current:"+this.current);
       
    84 }
       
    85 
       
    86 
       
    87 HistoryView.prototype.clear = function(){
       
    88 	this.items = new Array();
       
    89 	if (window.widget) {
       
    90 		for (var i = this.startindex; i < this.endindex + 1; i++) {
       
    91 			widget.setPreferenceForKey(null, getHistoryTitleKey(i));
       
    92 			if (KEEP_CONTENT) {
       
    93 				widget.setPreferenceForKey(null, getHistoryContentKey(i));
       
    94 			}
       
    95 			widget.setPreferenceForKey(null, getHistoryTimeKey(i));
       
    96 		}
       
    97 		this.startindex = 0;
       
    98 		this.endindex = 0;
       
    99 		this.current = 0;
       
   100 		this.storeIndex();
       
   101 		this.lastRender = 0;
       
   102 		this.render();
       
   103 	}
       
   104 }
       
   105 
       
   106 HistoryView.prototype.loadItems = function(){
       
   107 	uiManager.showNotification(-1, "wait", "Loading...");
       
   108 	var self = this;
       
   109 	setTimeout(function(){self.doLoadItems();},0);
       
   110 }
       
   111 
       
   112 
       
   113 HistoryView.prototype.doLoadItems = function(){
       
   114 	if ( window.widget ) {
       
   115 		var d = new Date();
       
   116 		var now = d.getTime();
       
   117 		this.items = new Array();
       
   118 		var tmpstart 	= 	widget.preferenceForKey("history.start");
       
   119 		var tmpend 		= 	widget.preferenceForKey("history.end");
       
   120 		var tmpcurr		=	widget.preferenceForKey("history.current");
       
   121 
       
   122 		this.startindex = (( tmpstart != undefined && tmpstart != null ) ? parseInt(tmpstart) : 0);
       
   123 		this.endindex = (( tmpend != undefined && tmpend != null ) ? parseInt(tmpend) : 0);
       
   124 		this.current = (( tmpcurr != undefined && tmpcurr != null ) ? parseInt(tmpcurr) : 0);
       
   125 
       
   126 //		alert("startindex=" + this.startindex +", endindex=" + this.endindex+ ", current=" + this.current);
       
   127 
       
   128 		for (var i = this.startindex; i < this.endindex; i++) {
       
   129 			var pos =  i - this.startindex;
       
   130 			var title = widget.preferenceForKey(getHistoryTitleKey(i));
       
   131 //			alert("Examining item: i=" + i + ", pos=" + pos +", title=" + title );
       
   132 			if ( !title || title == null || title == "null") continue;
       
   133 			var item = new HistoryViewItem(title,null,null,i);
       
   134 			if (KEEP_CONTENT) {
       
   135 				item.articleContent = widget.preferenceForKey(getHistoryContentKey(i));
       
   136 			}
       
   137 			item.articleBase = widget.preferenceForKey(getHistoryBaseKey(i));
       
   138 			item.timestamp = parseFloat(widget.preferenceForKey(getHistoryTimeKey(i)));
       
   139 			// delete items older than KEEP_HISTORY_FOR
       
   140 			if (item.timestamp < (now - KEEP_HISTORY_FOR)) {
       
   141 //				alert("Deleting old item: ts="+ item.timestamp + ", th=" + (now - KEEP_HISTORY_FOR));
       
   142 				widget.setPreferenceForKey(null, getHistoryTitleKey(i));
       
   143 				if (KEEP_CONTENT) {
       
   144 					widget.setPreferenceForKey(null, getHistoryContentKey(i));
       
   145 				}
       
   146 				widget.setPreferenceForKey(null, getHistoryBaseKey(i));
       
   147 				widget.setPreferenceForKey(null, getHistoryTimeKey(i));
       
   148 			}
       
   149 			else {
       
   150 				item.pos = i;
       
   151 				this.items[pos] = item;
       
   152 			}
       
   153 //			alert("Loaded item: i=" + i + ", pos=" + pos +", title=" + title +", ts=" + item.timestamp);
       
   154 		}
       
   155 	}
       
   156 	uiManager.hideNotification();
       
   157 }
       
   158 
       
   159 HistoryView.prototype.render = function(){
       
   160 	var d = new Date();
       
   161 	var now = d.getTime();
       
   162 	// what needs to be rendered?
       
   163 
       
   164 	if (now - this.lastRender > MILLIS_IN_A_DAY || this.todayControl == null) { // render all
       
   165 		
       
   166 		// clear components
       
   167 		this.removeAllControls();
       
   168 		var caption = new NavigationButton(1, "titlebar.png", "History", true);
       
   169 		caption.addEventListener("ActionPerformed", function(){
       
   170 			wikiHomeView.show();
       
   171 		});
       
   172 		this.addControl(caption);
       
   173 		
       
   174 		// add components for history items, up to today
       
   175 		
       
   176 		this.todayControl = new ContentPanel(null, "Last 24 hours", "", true, true);
       
   177 		this.todayControl.setContent(this.renderHistoryContent(now-MILLIS_IN_A_DAY, now));
       
   178 		this.addControl(this.todayControl);
       
   179 		var weekControl = new ContentPanel(null, "Last 7 days", "", true, false);
       
   180 		weekControl.setContent(this.renderHistoryContent(now-7*MILLIS_IN_A_DAY,now-MILLIS_IN_A_DAY));
       
   181 		this.addControl(weekControl);
       
   182 		var moreControl = new ContentPanel(null, "More than 7 days", "", true, false);
       
   183 		moreControl.setContent(this.renderHistoryContent(0,now-7*MILLIS_IN_A_DAY));
       
   184 		this.addControl(moreControl);
       
   185 	} else {
       
   186 		// render only today
       
   187 		this.todayControl.setContent(this.renderHistoryContent(now-MILLIS_IN_A_DAY, now));
       
   188 	} 
       
   189 	
       
   190 	this.lastRender = now;		
       
   191 }
       
   192 
       
   193 HistoryView.prototype.renderHistoryContent = function (from , to) {
       
   194 	var buf = "<table class='historytable'>";
       
   195 	for ( var i = this.endindex ; i >= this.startindex ; i -- ) {
       
   196 		var ind = i - this.startindex;
       
   197 		if ( this.items[ind] == undefined || this.items[ind] == null ) continue;
       
   198 		if ( this.items[ind].timestamp < from ) break;
       
   199 		if ( this.items[ind].timestamp < to ) {
       
   200 			buf += this.renderHistoryItem(this.items[ind]);			
       
   201 		}
       
   202 	}
       
   203 	buf += "</table>"
       
   204 	return buf;
       
   205 }
       
   206 
       
   207 HistoryView.prototype.renderHistoryItem = function(item){
       
   208 	if ( item == undefined || item == null ) return "";
       
   209     var buf = "<tr><td><small>";
       
   210 	buf += shortFormatTime(item.timestamp);
       
   211 	buf += "</small></td><td  width=70%>";	 
       
   212 	buf += "<div class=\"FeedItemLink\">";
       
   213 	buf += "<a href=\"JavaScript:void(0)\" onclick=\"event.stopPropagation();wikiHistoryView.goTo(" ;
       
   214 	buf += item.pos;
       
   215 	buf +=  "); return false;\">";
       
   216 	buf += "<strong>";
       
   217 	buf += shorten(item.articleTitle, 30);
       
   218 	buf += "</strong></a>";
       
   219 	buf += "</div>";
       
   220 	buf += "</td><td><div class=\"FeedItemLink\">";
       
   221 	buf += "<a href=\"JavaScript:void(0)\" onclick=\"event.stopPropagation();wikiHistoryView.deleteItem(" ;
       
   222 	buf += item.pos;
       
   223 	buf +=  "); return false;\">";
       
   224 	buf += "<img src=delete.png border=0></a>";
       
   225 	buf += "</div>";
       
   226 	buf += "</td></tr>";
       
   227 	return buf;
       
   228 }
       
   229 
       
   230 HistoryView.prototype.deleteItem = function(pos){	
       
   231 	this.items[pos-this.startindex] = null;
       
   232 	if ( window.widget ) {
       
   233 		widget.setPreferenceForKey(null, getHistoryTitleKey(pos));
       
   234 		if (KEEP_CONTENT) {
       
   235 			widget.setPreferenceForKey(null, getHistoryContentKey(pos));
       
   236 		}
       
   237 		widget.setPreferenceForKey(null, getHistoryBaseKey(pos));
       
   238 		widget.setPreferenceForKey(null, getHistoryTimeKey(pos));
       
   239 		this.storeIndex();
       
   240 	}
       
   241 	this.lastRender = 0;
       
   242 	this.render();
       
   243 }
       
   244 
       
   245 HistoryView.prototype.show = function(){
       
   246 	try {
       
   247 		this.render();
       
   248 	}catch(e){ alert(e);}
       
   249 	View.prototype.show.call(this);
       
   250 }
       
   251 
       
   252 HistoryView.prototype.goTo = function(pos){
       
   253 	this.current = pos;
       
   254 	wikiBrowse(this.items[pos-this.startindex].articleTitle, true, this.items[pos-this.startindex].articleBase); // true means don't re-add to history	 	
       
   255 }
       
   256 
       
   257 HistoryView.prototype.go = function(offset){
       
   258 	var newcurrent = this.current + offset;
       
   259 	var idx = newcurrent-this.startindex;
       
   260 	if ( newcurrent >= this.startindex && newcurrent <= this.endindex
       
   261 		 && idx >= 0 && this.items[idx] != undefined && this.items[idx] != null	) {
       
   262 		this.current = newcurrent;
       
   263 		wikiBrowse(this.items[idx].articleTitle, true, this.items[idx].articleBase);	 	
       
   264 	}
       
   265 }
       
   266 
       
   267 HistoryView.prototype.hasNext = function(){
       
   268 	var newidx = this.current + 1; 
       
   269 	return ( newidx < this.endindex && this.items[newidx] != undefined && this.items[newidx] != null );  
       
   270 }
       
   271 
       
   272 // abstract function for updating per-view menu
       
   273 // only called if window.widget is defined
       
   274 HistoryView.prototype.setupMenu = function(){
       
   275 	if ( window.widget ) {
       
   276 		menu.clear();
       
   277 		var clearMenuItem = new MenuItem("Clear", MENU_ITEM_HISTORY_CLEAR);
       
   278 		var self = this; 
       
   279 		clearMenuItem.onSelect = function(){self.clear();};
       
   280 		menu.append(clearMenuItem);
       
   281 
       
   282 		var self = this;
       
   283 		var searchMenuItem = new MenuItem("Search", MENU_ITEM_MAIN_SEARCH); 
       
   284 		searchMenuItem.onSelect = function(){wikiHomeView.show();};
       
   285 		menu.append(searchMenuItem);
       
   286 		var historyMenuItem = new MenuItem("History", MENU_ITEM_MAIN_HISTORY); 
       
   287 		historyMenuItem.onSelect = function(){wikiHistoryView.previousView = self; wikiHistoryView.show();};
       
   288 		menu.append(historyMenuItem);
       
   289 		var bookmarksMenuItem = new MenuItem("Bookmarks", MENU_ITEM_MAIN_BOOKMARKS); 
       
   290 		bookmarksMenuItem.onSelect = function(){wikiBookmarksView.show();};
       
   291 		menu.append(bookmarksMenuItem);
       
   292 
       
   293 		addHelpMenuItems();
       
   294 	}
       
   295 }
       
   296 
       
   297 
       
   298 function getHistoryKeyBase(pos) {
       
   299 	return "hist." + pos + "."; 
       
   300 }
       
   301 
       
   302 function getHistoryTitleKey(pos) {
       
   303 	return getHistoryKeyBase(pos) + "title"; 
       
   304 }
       
   305 
       
   306 function getHistoryContentKey(pos) {
       
   307 	return getHistoryKeyBase(pos) + "content"; 
       
   308 }
       
   309 
       
   310 function getHistoryBaseKey(pos) {
       
   311 	return getHistoryKeyBase(pos) + "base"; 
       
   312 }
       
   313 
       
   314 function getHistoryTimeKey(pos) {
       
   315 	return getHistoryKeyBase(pos) + "time"; 
       
   316 }
       
   317 
       
   318 
       
   319 //HistoryView.prototype.removeItem = function(articleTitle){
       
   320 //	// find index
       
   321 //	var ind = this.findItemIdFromTitle(articleTitle)
       
   322 //	if ( ind == -1 ) {
       
   323 //		return;
       
   324 //	}
       
   325 //
       
   326 //	// remove item
       
   327 //	this.items[ind-this.startindex] = null;
       
   328 //	if (window.widget) {
       
   329 //		widget.setPreferenceForKey(null, getHistoryTitleKey(ind));
       
   330 //		if (KEEP_CONTENT) {
       
   331 //			widget.setPreferenceForKey(null, getHistoryContentKey(ind));
       
   332 //		}
       
   333 //		widget.setPreferenceForKey(null, getHistoryTimeKey(ind));
       
   334 //	}
       
   335 //	this.current = ind;
       
   336 //	this.endindex = ind;
       
   337 //	this.render();
       
   338 //}
       
   339 
       
   340 //HistoryView.prototype.findItemIdFromTitle = function(title) {
       
   341 //	var ind = -1;
       
   342 //	for ( var i = this.startindex ; i < this.endindex + 1 ; i ++ ) {
       
   343 //		var arrind = i-this.startindex;
       
   344 //		if ( this.items[arrind] && this.items[arrind] != null && title == this.items[arrind].articleTitle) {
       
   345 //			ind = i;
       
   346 //			break;
       
   347 //		}
       
   348 //	}
       
   349 //	return ind;
       
   350 //}
       
   351 // return already downloaded content for an article or null
       
   352 // if we don't have it
       
   353 //HistoryView.prototype.getContentForArticle = function(articleTitle){
       
   354 //	if (window.widget && KEEP_CONTENT) {
       
   355 //		var i = this.findItemIdFromTitle(articleTitle);
       
   356 //		return widget.preferenceForKey(getHistoryContentKey(i));
       
   357 //	}
       
   358 //	return null;
       
   359 //}
       
   360