Symbian.org/Wiki.js
changeset 18 b73e6caf0031
equal deleted inserted replaced
17:5dc2963cd75f 18:b73e6caf0031
       
     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 resultsPerPage = 30;
       
    11 
       
    12 var wikiSearchBaseUrl = symbianOrgBaseUrl + "/wiki/api.php?format=xml&action=query&list=search&srsearch=";
       
    13 var wikiSearchOffsetQueryPart = "&sroffset=";
       
    14 var wikiSearchLimitQueryPart = "&srlimit=";
       
    15 var wikiSearchWhatQueryPart = "&srwhat=";
       
    16 
       
    17 var wikiViewPageUrlBase = symbianOrgBaseUrl + "/wiki/index.php?title=";
       
    18 var wikiPrintableParam = "&printable=yes";
       
    19 
       
    20 
       
    21 //var wikiListCategoriesUrl = symbianOrgBaseUrl + "/wiki/api.php?format=xml&action=query&list=allcategories&aclimit=500&acprop=size";
       
    22 //var wikiWatchListUrl = "http://en.wikipedia.org/w/api.php?action=query&list=watchlist&wlallrev&wlprop=ids|title|timestamp|user|comment";
       
    23 //var wikiWatchListFeed = "/wiki/api.php?action=feedwatchlist&feedformat=rss";
       
    24 
       
    25 var wikiSearchOptions = [
       
    26     { value: "title", text: "Search in title only" },
       
    27     { value: "text", text: "Full text search" },
       
    28 ];
       
    29 
       
    30 function WikiHome (parent) {
       
    31 	ListView.prototype.init.call(this, null, createCaption("Symbian Wiki"));	
       
    32 	this.previousView = parent;
       
    33 
       
    34 	this.wikiMainPageButton = new NavigationButton(1, "right.gif", "Browse: Main page");
       
    35 	this.wikiSearchButton = new FormButton(null, "Search");
       
    36 	this.wikiSearchSelection = new SelectionList(null, null, wikiSearchOptions, false, wikiSearchOptions[0]);
       
    37 	this.wikiSearchString = new TextField('wikiSearchString', null, "");
       
    38 	
       
    39 	var self = this;
       
    40 	
       
    41 	this.wikiMainPageButton.addEventListener("ActionPerformed", function(){wikiBrowse("Main Page");});
       
    42 	this.wikiSearchButton.addEventListener("ActionPerformed", function(){self.search(0);});
       
    43 
       
    44 	this.addControl(this.wikiMainPageButton);
       
    45 	this.addControl(new Label(null, "Search Wiki", null));
       
    46 	this.addControl(this.wikiSearchString);
       
    47 	this.addControl(this.wikiSearchSelection);
       
    48 	this.addControl(this.wikiSearchButton);
       
    49 
       
    50 
       
    51 //	wikiBrowseButton = new NavigationButton(1, "right.gif", "Browse categories");
       
    52 //	wikiBrowseButton.addEventListener("ActionPerformed", function(){browseWikiCategories.show();});
       
    53 //	this.addControl(wikiBrowseButton);
       
    54 
       
    55 }
       
    56 
       
    57 WikiHome.prototype = new ListView(null, null);
       
    58 WikiHome.prototype.wikiMainPageButton = null;
       
    59 WikiHome.prototype.wikiSearchButton = null;
       
    60 WikiHome.prototype.wikiSearchString = null;
       
    61 WikiHome.prototype.wikiSearchSelection = null;
       
    62 
       
    63 var lastWikiSearchWasFrom = 0;
       
    64 var lastWikiSearchResultCountWas = 0;
       
    65 
       
    66 WikiHome.prototype.search = function(from) {
       
    67 	lastWikiSearchWasFrom = from;
       
    68 	var srstring = this.wikiSearchString.getText();
       
    69     var selectedTitleOrText = this.wikiSearchSelection.getSelected();
       
    70     var titleOrText = (selectedTitleOrText != null) ? selectedTitleOrText.value : "title";
       
    71 	var url = this.formSearchUrl( srstring , from, resultsPerPage, titleOrText );
       
    72 	var reader = new RssReader("Wiki: " + srstring, url, new WikiFeedPresenter(null), this, wikiResponseParser);
       
    73 	reader.show();
       
    74 }
       
    75 
       
    76 WikiHome.prototype.formSearchUrl = function(query, offset, limit, what) {
       
    77 	var buf = wikiSearchBaseUrl + query
       
    78 	if (offset > 0) {
       
    79 		buf += wikiSearchOffsetQueryPart + offset;
       
    80 	}
       
    81 	buf += wikiSearchLimitQueryPart + limit
       
    82 	buf += wikiSearchWhatQueryPart + what;
       
    83 	return buf;
       
    84 }
       
    85 
       
    86 
       
    87 // /////////////////////////////////////////////////////////////////////////////
       
    88 // RssReader customisations
       
    89 
       
    90 // response parser for forum groups
       
    91 function wikiResponseParser(broker, responseStatus, xmlDoc) {
       
    92     if (responseStatus == 200 && xmlDoc != null) {
       
    93 		// for compatibility with rss
       
    94 		var lastModified = new Date();
       
    95 		
       
    96         // init result items array
       
    97         var items = [];
       
    98 
       
    99 		var elements = xmlDoc.getElementsByTagName("p");
       
   100 
       
   101 		for (var i = 0; i < elements.length; i++) {
       
   102 			var pagetitle = elements[i].getAttribute("title");
       
   103             items.push({ id: ""+i, title: pagetitle});
       
   104 		}
       
   105 
       
   106 		lastWikiSearchResultCountWas = elements.length;
       
   107         // update was completed successfully
       
   108         return { status: "ok", lastModified: lastModified, items: items };
       
   109     } else {
       
   110         // update failed
       
   111         return { status: "error" };
       
   112     }
       
   113 }
       
   114 
       
   115 // FeedPresenter implementation for wiki
       
   116 function WikiFeedPresenter(rssreader){
       
   117 	if (rssreader) {
       
   118 		this.init(rssreader);
       
   119 	}
       
   120 }
       
   121 
       
   122 // WikiFeedPresenter is a subclass of ButtonFeedPresenter
       
   123 WikiFeedPresenter.prototype = new ButtonFeedPresenter(null);
       
   124 
       
   125 // WikiFeedPresenter "Constructor"
       
   126 WikiFeedPresenter.prototype.init = function(rssreader) {
       
   127 	ButtonFeedPresenter.prototype.init.call(this, rssreader);
       
   128 }
       
   129 
       
   130 // Handle the click on a specific item
       
   131 WikiFeedPresenter.prototype.feedClicked = function(event){
       
   132 	var buttonid = event.source.id;
       
   133 	
       
   134 	if (buttonid == "Next page") {
       
   135 		wiki.search(lastWikiSearchWasFrom + resultsPerPage);
       
   136 	}
       
   137 	else if (buttonid == "Previous page") {
       
   138 		var from = lastWikiSearchWasFrom - resultsPerPage; 
       
   139 		if ( from < 0 ) from = 0;
       
   140 		wiki.search(from);
       
   141 	}
       
   142 	else {
       
   143 		// show article
       
   144 		var title = this.items[buttonid].title;
       
   145 		wikiBrowse(title);
       
   146 	}
       
   147 }
       
   148 
       
   149 // Create and add controls to be shown before items list.
       
   150 WikiFeedPresenter.prototype.addFooterItems = function(){
       
   151     var self = this;
       
   152 	if (lastWikiSearchResultCountWas == resultsPerPage) {
       
   153 		var nextPageButton = new NavigationButton("Next page", "blueright.gif", "Next page");
       
   154 		nextPageButton.addEventListener("ActionPerformed", function(event){ self.feedClicked(event); });
       
   155 		this.rssreader.addControl(nextPageButton);
       
   156 	}
       
   157 	if (lastWikiSearchWasFrom > 0) {
       
   158 		var prevPageButton = new NavigationButton("Previous page", "blueright.gif", "Previous page");
       
   159 		prevPageButton.addEventListener("ActionPerformed", function(event) { self.feedClicked(event); });
       
   160 		this.rssreader.addControl(prevPageButton);
       
   161 	}
       
   162 }
       
   163 
       
   164 
       
   165 // /////////////////////////////////////////////////////////////////////////////////
       
   166 // Browse / view wiki pages in 'printable format'
       
   167 var wikiAjax;
       
   168 var pageBeingShown;
       
   169 function wikiBrowse(page) {
       
   170 	pageBeingShown = page;
       
   171 	uiManager.showNotification(-1, "wait", "Loading page...", -1);
       
   172 	if ( page ) {
       
   173 		wikiAjax = new Ajax();
       
   174 	    
       
   175 		wikiAjax.onreadystatechange = function() { wikiPageDownloadStateChanged(); };
       
   176 		
       
   177 		var url = wikiViewPageUrlBase  + encodeURIComponent(page) + wikiPrintableParam; 
       
   178 	    // initiate the request
       
   179 	    wikiAjax.open("GET", url, true);
       
   180 	    wikiAjax.send(null);
       
   181 	}
       
   182 }
       
   183 
       
   184 function wikiPageDownloadStateChanged() {
       
   185     if (wikiAjax.readyState == 4) {
       
   186 		uiManager.hideNotification();
       
   187 		var html = wikiAjax.responseText;
       
   188 		var start = html.indexOf('<div id="bodyContent">');
       
   189 		var end = findDivEnd(html, start);
       
   190 		var pageView = new ListView(null, createCaption(pageBeingShown));
       
   191 		var container = new TextPane(null, null, modWikiLinks(html.substring(start, end)));
       
   192 		pageView.addControl(container);
       
   193 		pageView.previousView = uiManager.currentView;
       
   194 		pageView.show();
       
   195 		if (window.widget) {
       
   196 			widget.setNavigationEnabled(true);
       
   197 		}
       
   198 	}
       
   199 }
       
   200 
       
   201 function modWikiLinks(text) {
       
   202 	var tmp = text.replace(/ src="/g, ' src="http://developer.symbian.org');
       
   203 	// images sorted. now links
       
   204 	var strToLookFor = ' href="/wiki/index.php/';
       
   205 	var from = 0;
       
   206 	var ind = tmp.indexOf(strToLookFor);
       
   207 	var buf ="";
       
   208 	while ( ind > 0 ) {
       
   209 		buf = buf + tmp.substring(from, ind);
       
   210 		// extract page name
       
   211 		var ind2 = tmp.indexOf('"', ind + strToLookFor.length);
       
   212 		var pageName = tmp.substring(ind + strToLookFor.length, ind2);
       
   213 		buf += " style=\"text-decoration: underline;\" href=\"JavaScript:void(0)\" onclick=\"wikiBrowse('" + pageName + "'); return false;\"";
       
   214 		from = ind2;
       
   215 		ind = tmp.indexOf(strToLookFor, from);
       
   216 	}
       
   217 	buf = buf + tmp.substring(from);
       
   218 	return buf; 
       
   219 }
       
   220 
       
   221 function findDivEnd(text, from) {
       
   222 	var openDivCount = 1;
       
   223 	var divind = text.indexOf("<div", from+1);
       
   224 	var cdivind = text.indexOf("</div", from+1);
       
   225 	var pos = from;
       
   226 	while ( openDivCount > 0 && divind > 0 && cdivind > 0 ) {
       
   227 		pos = Math.min(divind, cdivind);
       
   228 		if ( pos == divind ) {
       
   229 			openDivCount ++;
       
   230 		}
       
   231 		if ( pos == cdivind ) {
       
   232 			openDivCount --;
       
   233 		}
       
   234 		divind = text.indexOf("<div", pos+1);
       
   235 		cdivind = text.indexOf("</div", pos+1);
       
   236 	}
       
   237 	return pos;
       
   238 }