Wikipedia/FeedPresentation.js
author ivanl
Fri, 19 Mar 2010 11:03:38 +0000
changeset 21 f7a5a40d5bc4
parent 20 918767a9c8d3
permissions -rw-r--r--
1.0rc17 Fix blog feed
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
// Feed presentation classes
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    11
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    12
// Abstract class used to create controls to represent feed entries.
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    13
function FeedPresenter(rssreader){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    14
	if (rssreader) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    15
		this.init(rssreader);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    16
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    17
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    18
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    19
// FeedPresenter "Constructor"
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    20
FeedPresenter.prototype.init = function(rssreader){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    21
	this.rssreader = rssreader;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    22
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    23
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    24
// Create a control that represents this item and add it to
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    25
// parent rss reader
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    26
FeedPresenter.prototype.show = function(item){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    27
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    28
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    29
// Create and add controls to be shown before items list.
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    30
FeedPresenter.prototype.addPreambleItems = function(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    31
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    32
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    33
// Create and add controls to be shown before items list.
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    34
FeedPresenter.prototype.addFooterItems = function(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    35
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    36
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    37
// No items returned, show "No messages"
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    38
FeedPresenter.prototype.showNoItems = function(){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    39
	var label = new Label(null, null, "No results");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    40
	this.rssreader.addFeedItemControl(label);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    41
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    42
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    43
// Implementation of FeedPresenter that shows feed in a 
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    44
// ContentPanel
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    45
function HtmlFeedPresenter(rssreader) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    46
	if (rssreader) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    47
		this.init(rssreader);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    48
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    49
	this.expanded = false;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    50
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    51
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    52
// HtmlFeedPresenter is a subclass of FeedPresenter
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    53
HtmlFeedPresenter.prototype = new FeedPresenter(null);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    54
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    55
// HtmlFeedPresenter "constructor"
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    56
HtmlFeedPresenter.prototype.init = function(rssreader) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    57
	FeedPresenter.prototype.init.call(this, rssreader);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    58
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    59
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    60
// Create a control that represents this item and add it to
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    61
// parent rss reader
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    62
HtmlFeedPresenter.prototype.show = function(item) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    63
			// get a feed item control from the pool or create one and
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    64
		// place it in the pool if there aren't enough feed item controls
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    65
		var feedItemControl = new ContentPanel(null, null, null, true);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    66
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    67
		// initialize feed item control
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    68
		feedItemControl.setCaption(item.title);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    69
		feedItemControl.setContent(this.getContentHTMLForFeedItem(item));
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    70
		feedItemControl.setExpanded(this.expanded);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    71
		
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    72
		// add the feed item control to the main view
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    73
		this.rssreader.addFeedItemControl(feedItemControl);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    74
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    75
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    76
// Returns the content HTML for a feed item.
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    77
HtmlFeedPresenter.prototype.getContentHTMLForFeedItem = function (item){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    78
	var buf = "";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    79
	
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    80
	// item date
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    81
	if (item.date != null) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    82
		buf += "<div class=\"FeedItemDate\">" ;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    83
		if ( item.author != null ) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    84
			buf += item.author + ", ";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    85
		}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    86
		buf += item.date + "</div>";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    87
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    88
	
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    89
	// item description
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    90
	if (item.description != null) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    91
		buf += "<div class=\"FeedItemDescription\">" + item.description + "</div>";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    92
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    93
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    94
	if (item.url != null) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    95
		// blogs
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    96
        buf += "<div class=\"FeedItemLink\">";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    97
//            buf += "<a href=\"JavaScript:void(0)\" onclick=\"openURL('" + item.title + "', '" + item.url + "'); return false;\">";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    98
            buf += "<a href=\"JavaScript:void(0)\" onclick=\"openURL('" + item.url + "'); return false;\">";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
    99
            buf += "Read more...";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   100
            buf += "</a>";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   101
        buf += "</div>";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   102
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   103
	
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   104
	return buf;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   105
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   106
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   107
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   108
// Implementation of FeedPresenter that shows feed as a clickable
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   109
// button that shows feed entry title as label
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   110
function ButtonFeedPresenter(rssreader) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   111
	if (rssreader) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   112
		this.init(rssreader);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   113
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   114
	this.indexCounter = 0;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   115
	this.items = [];
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   116
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   117
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   118
// ButtonFeedPresenter is a subclass of FeedPresenter
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   119
ButtonFeedPresenter.prototype = new FeedPresenter(null);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   120
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   121
// ButtonFeedPresenter "constructor"
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   122
ButtonFeedPresenter.prototype.init = function(rssreader) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   123
	FeedPresenter.prototype.init.call(this, rssreader);
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
// Create a control that represents this item and add it to
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   127
// parent rss reader
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   128
ButtonFeedPresenter.prototype.show = function(item) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   129
	this.items[this.indexCounter] = item;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   130
	// get a feed item control from the pool or create one and
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   131
	// place it in the pool if there aren't enough feed item controls
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   132
	var feedItemControl = new NavigationButton(this.indexCounter, "right.gif", item.title);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   133
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   134
	// add button press handler
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   135
    var self = this;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   136
	feedItemControl.addEventListener("ActionPerformed", 
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   137
		function(event) { self.feedClicked(event); } );
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   138
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   139
	this.indexCounter++;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   140
		
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   141
	// add the feed item control to the main view
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   142
	this.rssreader.addFeedItemControl(feedItemControl);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   143
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   144
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   145
// Handle the button-press
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   146
ButtonFeedPresenter.prototype.feedClicked = function(event){
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   147
	var clickedButton = event.source;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   148
	var id = clickedButton.id;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   149
	var url = this.items[id].url;
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   150
	
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   151
	if (url.indexOf("/wiki/index.php")) {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   152
		// hack for printable wiki pages
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   153
		var articleName = url.replace(wikiBaseUrl + "/", "");
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   154
		url = wikiBaseUrl + "?title=" + articleName + "&action=render";
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   155
		openURL(url);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   156
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   157
	else {
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   158
		openURL(url);
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   159
	}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   160
}
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   161
918767a9c8d3 Adding Wikipedia Widget 1.0rc5
ivanl
parents:
diff changeset
   162