mypackage_wrt/FeedPresentation.js
author victorp@symbian.org
Fri, 30 Apr 2010 15:01:03 +0100
changeset 42 20be4dd42b12
permissions -rwxr-xr-x
My package widget code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     1
// ////////////////////////////////////////////////////////////////////////////
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     2
// Symbian Foundation Example Code
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     3
//
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     4
// This software is in the public domain. No copyright is claimed, and you 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     5
// may use it for any purpose without license from the Symbian Foundation.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     6
// No warranty for any purpose is expressed or implied by the authors or
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     7
// the Symbian Foundation. 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     8
// ////////////////////////////////////////////////////////////////////////////
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     9
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    10
// Feed presentation classes
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    11
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    12
// Abstract class used to create controls to represent feed entries.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    13
function FeedPresenter(rssreader){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    14
	if (rssreader) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    15
		this.init(rssreader);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    16
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    17
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    18
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    19
// FeedPresenter "Constructor"
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    20
FeedPresenter.prototype.init = function(rssreader){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    21
	this.rssreader = rssreader;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    22
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    23
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    24
// Create a control that represents this item and add it to
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    25
// parent rss reader
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    26
FeedPresenter.prototype.show = function(item){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    27
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    28
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    29
// Create and add controls to be shown before items list.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    30
FeedPresenter.prototype.addPreambleItems = function(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    31
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    32
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    33
// Create and add controls to be shown before items list.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    34
FeedPresenter.prototype.addFooterItems = function(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    35
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    36
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    37
// No items returned, show "No messages"
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    38
FeedPresenter.prototype.showNoItems = function(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    39
	var label = new Label(null, null, "No messages");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    40
	this.rssreader.addControl(label);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    41
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    42
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    43
// Implementation of FeedPresenter that shows feed in a 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    44
// ContentPanel
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    45
function HtmlFeedPresenter(rssreader) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    46
	if (rssreader) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    47
		this.init(rssreader);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    48
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    49
	this.expanded = false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    50
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    51
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    52
// HtmlFeedPresenter is a subclass of FeedPresenter
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    53
HtmlFeedPresenter.prototype = new FeedPresenter(null);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    54
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    55
// HtmlFeedPresenter "constructor"
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    56
HtmlFeedPresenter.prototype.init = function(rssreader) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    57
	FeedPresenter.prototype.init.call(this, rssreader);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    58
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    59
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    60
// Create a control that represents this item and add it to
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    61
// parent rss reader
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    62
HtmlFeedPresenter.prototype.show = function(item) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    63
			// get a feed item control from the pool or create one and
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    64
		// place it in the pool if there aren't enough feed item controls
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    65
		var feedItemControl = new ContentPanel(null, null, null, true);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    66
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    67
		// initialize feed item control
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    68
		feedItemControl.setCaption(item.title);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    69
		feedItemControl.setContent(this.getContentHTMLForFeedItem(item));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    70
		feedItemControl.setExpanded(this.expanded);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    71
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    72
		// add the feed item control to the main view
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    73
		this.rssreader.feedItemControls.push(feedItemControl);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    74
		this.rssreader.addControl(feedItemControl);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    75
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    76
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    77
// Returns the content HTML for a feed item.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    78
HtmlFeedPresenter.prototype.getContentHTMLForFeedItem = function (item){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    79
	var buf = "";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    80
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    81
	// item date
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    82
	if (item.date != null) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    83
		buf += "<div class=\"FeedItemDate\">" ;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    84
		if ( item.author != null ) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    85
			buf += item.author + ", ";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    86
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    87
		buf += item.date + "</div>";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    88
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    89
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    90
	// item description
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    91
	if (item.description != null) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    92
		buf += "<div class=\"FeedItemDescription\">" + item.description + "</div>";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    93
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    94
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    95
	if (item.url != null) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    96
		// blogs
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    97
        buf += "<div class=\"FeedItemLink\">";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    98
//            buf += "<a href=\"JavaScript:void(0)\" onclick=\"openURL('" + item.title + "', '" + item.url + "'); return false;\">";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    99
            buf += "<a href=\"JavaScript:void(0)\" onclick=\"openURL('" + item.url + "'); return false;\">";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   100
            buf += "Read more...";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   101
            buf += "</a>";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   102
        buf += "</div>";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   103
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   104
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   105
	return buf;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   106
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   107
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   108
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   109
// Implementation of FeedPresenter that shows feed as a clickable
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   110
// button that shows feed entry title as label
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   111
function ButtonFeedPresenter(rssreader) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   112
	if (rssreader) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   113
		this.init(rssreader);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   114
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   115
	this.indexCounter = 0;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   116
	this.items = [];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   117
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   118
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   119
// ButtonFeedPresenter is a subclass of FeedPresenter
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   120
ButtonFeedPresenter.prototype = new FeedPresenter(null);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   121
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   122
// ButtonFeedPresenter "constructor"
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   123
ButtonFeedPresenter.prototype.init = function(rssreader) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   124
	FeedPresenter.prototype.init.call(this, rssreader);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   125
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   126
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   127
// Create a control that represents this item and add it to
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   128
// parent rss reader
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   129
ButtonFeedPresenter.prototype.show = function(item) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   130
	this.items[this.indexCounter] = item;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   131
	// get a feed item control from the pool or create one and
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   132
	// place it in the pool if there aren't enough feed item controls
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   133
	var feedItemControl = new NavigationButton(this.indexCounter, "right.gif", item.title);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   134
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   135
	// add button press handler
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   136
    var self = this;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   137
	feedItemControl.addEventListener("ActionPerformed", 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   138
		function(event) { self.feedClicked(event); } );
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   139
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   140
	this.indexCounter++;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   141
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   142
	// add the feed item control to the main view
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   143
	this.rssreader.feedItemControls.push(feedItemControl);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   144
	this.rssreader.addControl(feedItemControl);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   145
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   146
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   147
// Handle the button-press
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   148
ButtonFeedPresenter.prototype.feedClicked = function(event){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   149
	var clickedButton = event.source;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   150
	var id = clickedButton.id;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   151
	var url = this.items[id].url;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   152
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   153
	if (url.indexOf("/wiki/index.php")) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   154
		// hack for printable wiki pages
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   155
		var articleName = url.replace(wikiBaseUrl + "/", "");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   156
		url = wikiBaseUrl + "?title=" + articleName + "&action=render";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   157
		openURL(url);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   158
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   159
	else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   160
		openURL(url);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   161
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   162
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   163
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   164