Index: trunk/mockups/athena/athena.js |
— | — | @@ -1,2 +1,79 @@ |
2 | 2 | // stub |
| 3 | +(function($) { |
3 | 4 | |
| 5 | +var wiki = { |
| 6 | + base: 'https://$1.wikipedia.org/w/api.php', |
| 7 | + lang: 'en', |
| 8 | + |
| 9 | + /** |
| 10 | + * @return string |
| 11 | + */ |
| 12 | + apiUrl: function() { |
| 13 | + return wiki.base.replace('$1', wiki.lang); |
| 14 | + }, |
| 15 | + |
| 16 | + /** |
| 17 | + * @param object params: map of parameters to send to API |
| 18 | + * @return promise |
| 19 | + */ |
| 20 | + api: function(params) { |
| 21 | + var ajaxParams = { |
| 22 | + format: 'json' |
| 23 | + }; |
| 24 | + $.extend(ajaxParams, params || {}); |
| 25 | + return $.ajax({ |
| 26 | + url: wiki.apiUrl(), |
| 27 | + data: ajaxParams, |
| 28 | + dataType: 'jsonp' |
| 29 | + }); |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +var ui = { |
| 34 | + startSpinner: function() { |
| 35 | + }, |
| 36 | + |
| 37 | + stopSpinner: function() { |
| 38 | + }, |
| 39 | + |
| 40 | + showPage: function(title, content) { |
| 41 | + // @fixme trust issues :) |
| 42 | + console.log('Showing title', title); |
| 43 | + console.log('Showing content', content); |
| 44 | + $('#page-content').html(content); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +var app = { |
| 49 | + loadPage: function(title) { |
| 50 | + ui.startSpinner(); |
| 51 | + wiki.api({ |
| 52 | + action: 'query', |
| 53 | + prop: 'revisions', |
| 54 | + titles: title, |
| 55 | + rvprop: 'timestamp|content', |
| 56 | + rvparse: 1 |
| 57 | + }).then(function(data) { |
| 58 | + console.log('page!', data); |
| 59 | + var pageId, page; |
| 60 | + $.each(data.query.pages, function() { |
| 61 | + pageId = this.id; |
| 62 | + page = this; |
| 63 | + }); |
| 64 | + var revId, rev; |
| 65 | + $.each(page.revisions, function() { |
| 66 | + revId = this.revid; |
| 67 | + rev = this; |
| 68 | + }); |
| 69 | + ui.showPage(page.title, rev['*']); |
| 70 | + ui.stopSpinner(); |
| 71 | + }); |
| 72 | + }, |
| 73 | +}; |
| 74 | + |
| 75 | +$(function() { |
| 76 | + app.loadPage('While My Guitar Gently Weeps'); |
| 77 | +}); |
| 78 | + |
| 79 | +})(jQuery); |
| 80 | + |