single-cartoon-view.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright (c) 2009 Nokia Corporation.
  3. */
  4. /**
  5. * This is run after the the web page has been loaded (possibly before rendering).
  6. * $(document).ready documented here:
  7. * http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
  8. */
  9. function launch() {
  10. $(document).ready(function() {
  11. try {
  12. console.debug("Init started.");
  13. var params = $.deparam.querystring();
  14. var db;
  15. if (window.openDatabase) {
  16. db = openDatabase("CartoonReader",
  17. "1.0",
  18. "CartoonReader example",
  19. 200000);
  20. }
  21. if (!db) {
  22. alert("Failed to open the database on disk. This is probably because the version was bad or there is not enough space left in this domain's quota");
  23. }
  24. var iframeDocument = document.getElementById('cartoonContainerFrame').contentDocument;
  25. var content = '<html lang="en">'
  26. +' <head>'
  27. +' <title>CartoonReader</title>'
  28. +' <link rel="stylesheet" href="../css/cartoon-view-iframe.css" type="text/css" />'
  29. +' </head>'
  30. +' <body>'
  31. +' <article id="cartoonContainer">'
  32. +' <div id="cartoonVisibleInfo">'
  33. +' <h1 id="cartoonName"></h1>'
  34. +' <h2 id="cartoonTitle"></h2>'
  35. +' <p id="cartoonPublished"></p>'
  36. +' <div class="full-stop-div"></div>'
  37. +' </div>'
  38. +' <img id="cartoonImage" src="../images/loading.gif" />'
  39. +' <div id="cartoonInfo">'
  40. +' <dl>'
  41. +' <dt class="cartoonInfoHidden">Image title</dt>'
  42. +' <dd id="cartoonTitleText"></dd>'
  43. +' <dt class="cartoonInfoHidden">Image alt</dt>'
  44. +' <dd id="cartoonAltText"></dd>'
  45. +' <dt id="cartoonURLTitle">Cartoon <abbr title="Uniform Resource Locator">URL</abbr></dt>'
  46. +' <dd id="cartoonURL"></dd>'
  47. +' </dl>'
  48. +' <button id="cartoonInfoOK">OK</button>'
  49. +' </div>'
  50. +' </article>'
  51. +' </body>'
  52. +'</html>';
  53. iframeDocument.open();
  54. iframeDocument.write(content);
  55. iframeDocument.close();
  56. var backend = new CartoonManagerBackend(db);
  57. var cartoonContainer = $('#cartoonContainerFrame').contents().find("#cartoonContainer");
  58. var elements = {document: document,
  59. navigation: $("nav"),
  60. headerContainer: $("#headerContainer"),
  61. cartoonName: cartoonContainer.end().find("#cartoonName"),
  62. cartoonTitle: cartoonContainer.end().find("#cartoonTitle"),
  63. cartoonImage: cartoonContainer.end().find("#cartoonImage"),
  64. cartoonInfo: cartoonContainer.end().find("#cartoonInfo"),
  65. cartoonTitleText: cartoonContainer.end().find("#cartoonTitleText"),
  66. cartoonAltText: cartoonContainer.end().find("#cartoonAltText"),
  67. cartoonPublished: cartoonContainer.end().find("#cartoonPublished"),
  68. cartoonURL: cartoonContainer.end().find("#cartoonURL"),
  69. images: $("img").add("#cartoonImage",cartoonContainer),
  70. body: $("body"),
  71. navLiButtons: $("nav li button"),
  72. cartoonReader: $("#cartoonReader")};
  73. var params = $.deparam.querystring();
  74. if(window.appWindow) {
  75. window.close = appWindow.close;
  76. window.minimize = appWindow.minimize;
  77. }
  78. var platform = "";
  79. if(window.platformResolver) {
  80. platform = window.platformResolver.platform();
  81. }
  82. var helpers = { jQueryEventProxy: new jQueryEventProxy(console),
  83. dndHelper: new DnDHelper(),
  84. cartoonDataFetcher: new CartoonDataFetcher(new FeedUpdateBroker(),
  85. new CartoonParser())
  86. };
  87. helpers.maemoHelper = new MaemoHelper(window,
  88. helpers.jQueryEventProxy,
  89. helpers.dndHelper);
  90. window.SingleCartoon = new SingleCartoon(elements,
  91. helpers,
  92. backend,
  93. window,
  94. params,
  95. platform);
  96. console.debug("Init finished.");
  97. }
  98. catch(e) {
  99. console.error(e);
  100. alert(e);
  101. }
  102. });
  103. }
  104. function SingleCartoon(elements,
  105. helpers,
  106. backend,
  107. appWindow,
  108. params,
  109. platform) {
  110. this.C_MANAGE_URL = "qrc:/html/manage-cartoons-view.html";
  111. this.PLATFORM = platform;
  112. this.m_backend = backend;
  113. this.m_window = appWindow;
  114. this.m_elements = elements;
  115. this.m_helpers = helpers;
  116. this.m_params = params;
  117. this.m_view = new SingleCartoon.View(elements);
  118. var adapter = new CartoonSourceResultAdapter(this.cartoonSourceCallback());
  119. this.m_backend.registerGetCartoonSourceAdapter(adapter);
  120. var subscribers = [];
  121. subscribers.push(this.m_view);
  122. this.m_model = new SingleCartoon.Model(subscribers);
  123. this.setup(helpers,
  124. elements,
  125. platform);
  126. this.m_backend.getActiveCartoonSource();
  127. }
  128. SingleCartoon.prototype = {
  129. setup: function(helpers,
  130. elements,
  131. platform) {
  132. this.bindNextButton(helpers.jQueryEventProxy,
  133. elements.navigation.find("#nextButton"));
  134. this.bindNewestButton(helpers.jQueryEventProxy,
  135. elements.navigation.find("#newestButton"));
  136. this.bindPreviousButton(helpers.jQueryEventProxy,
  137. elements.navigation.find("#previousButton"));
  138. this.bindInfoToggleButton(helpers.jQueryEventProxy,
  139. elements.navigation.find("#infoToggleButton"));
  140. this.bindInfoToggleButton(helpers.jQueryEventProxy,
  141. elements.cartoonInfo.find("#cartoonInfoOK"));
  142. this.bindManageButton(helpers.jQueryEventProxy,
  143. elements.navigation.find("#manageButton"));
  144. this.bindExitButton(helpers.jQueryEventProxy,
  145. elements.navigation.find("#exitButton"));
  146. helpers.dndHelper.disableDragging(elements.images);
  147. helpers.dndHelper.disableDragging(elements.body);
  148. if(platform === "MAEMO") {
  149. helpers.maemoHelper.createTaskSwitcher(elements.headerContainer);
  150. helpers.maemoHelper.setFontSizeTo(elements.body, "1.25em");
  151. }
  152. },
  153. cartoonSourceCallback: function() {
  154. var callbackObject = { that: this,
  155. callback: this.receiveCartoonSource,
  156. errorCallback: this.receiveError};
  157. return callbackObject;
  158. },
  159. cartoonCallback: function() {
  160. var callbackObject = {cartoonReceiver: { that: this,
  161. callback: this.receiveCartoons},
  162. errorReceiver: { that: this,
  163. callback: this.receiveError}
  164. };
  165. return callbackObject;
  166. },
  167. receiveError: function(url, error) {
  168. alert(error);
  169. this.m_view.setCartoonImageURL("qrc:/images/error-icon.png");
  170. },
  171. receiveCartoonSource: function(sources) {
  172. var source = sources[0];
  173. this.m_helpers.cartoonDataFetcher.getDataFromURL(source.getUrl(),
  174. this.cartoonCallback());
  175. },
  176. receiveCartoons: function(cartoons) {
  177. this.m_model.setCartoons(cartoons);
  178. this.m_model.setActiveCartoonIndex(0);
  179. this.toggleNextPreviousVisiblity();
  180. },
  181. bindNextButton: function(jQueryEventProxy,
  182. nextButton) {
  183. var handler = { that: this,
  184. handler: this.nextButtonClicked};
  185. jQueryEventProxy.bind(nextButton,
  186. "click",
  187. handler);
  188. },
  189. bindNewestButton: function(jQueryEventProxy,
  190. newestButton) {
  191. var handler = { that: this,
  192. handler: this.newestButtonClicked};
  193. jQueryEventProxy.bind(newestButton,
  194. "click",
  195. handler);
  196. },
  197. bindPreviousButton: function(jQueryEventProxy,
  198. previousButton) {
  199. var handler = { that: this,
  200. handler: this.previousButtonClicked};
  201. jQueryEventProxy.bind(previousButton,
  202. "click",
  203. handler);
  204. },
  205. bindInfoToggleButton: function(jQueryEventProxy,
  206. infoToggleButton) {
  207. var handler = { that: this,
  208. handler: this.infoToggleButtonClicked};
  209. jQueryEventProxy.bind(infoToggleButton,
  210. "click",
  211. handler);
  212. },
  213. bindManageButton: function(jQueryEventProxy,
  214. manageButton) {
  215. var handler = { that: this,
  216. handler: this.manageButtonClicked};
  217. jQueryEventProxy.bind(manageButton,
  218. "click",
  219. handler);
  220. },
  221. bindExitButton: function(jQueryEventProxy,
  222. exitButton) {
  223. var handler = { that: this,
  224. handler: this.exitButtonClicked};
  225. jQueryEventProxy.bind(exitButton,
  226. "click",
  227. handler);
  228. },
  229. manageButtonClicked: function(target, event) {
  230. var window = this.m_window;
  231. var url = this.C_MANAGE_URL;
  232. if(this.PLATFORM !== "MAEMO" &&
  233. this.PLATFORM !== "SYMBIAN") {
  234. this.m_view.fadeOut("slow",
  235. function() { window.location = url; });
  236. }
  237. else {
  238. window.location = url;
  239. }
  240. },
  241. nextButtonClicked: function(target, event) {
  242. if(this.isAvailable({ next: true})) {
  243. var that = this;
  244. var model = this.m_model;
  245. var view = this.m_view;
  246. this.m_view.hideCartoon(true, function() {
  247. model.setActiveCartoonIndex(model.getActiveCartoonIndex()-1);
  248. that.toggleNextPreviousVisiblity();
  249. view.hideCartoon(false);
  250. });
  251. }
  252. },
  253. newestButtonClicked: function(target, event) {
  254. this.toggleNextPreviousVisiblity();
  255. if(this.isAvailable({ newest: true})) {
  256. var that = this;
  257. var model = this.m_model;
  258. var view = this.m_view;
  259. this.m_view.hideCartoon(true, function() {
  260. model.setActiveCartoonIndex(0);
  261. that.toggleNextPreviousVisiblity();
  262. view.hideCartoon(false);
  263. });
  264. }
  265. },
  266. previousButtonClicked: function(target, event) {
  267. this.toggleNextPreviousVisiblity();
  268. if(this.isAvailable({ previous: true})) {
  269. var that = this;
  270. var model = this.m_model;
  271. var view = this.m_view;
  272. this.m_view.hideCartoon(true, function() {
  273. model.setActiveCartoonIndex(model.getActiveCartoonIndex()+1);
  274. that.toggleNextPreviousVisiblity();
  275. view.hideCartoon(false);
  276. });
  277. }
  278. },
  279. infoToggleButtonClicked: function(target, event) {
  280. if(this.m_view.infoOn()) {
  281. this.m_view.toggleInfo(false);
  282. }
  283. else {
  284. this.m_view.toggleInfo(true);
  285. }
  286. },
  287. exitButtonClicked: function(target, event) {
  288. this.m_window.close();
  289. },
  290. toggleNextPreviousVisiblity: function() {
  291. this.m_view.hidePreviousButton(!this.isAvailable({ previous: true}), 'slow');
  292. this.m_view.hideNextButton(!this.isAvailable({ next: true}), 'slow');
  293. },
  294. isAvailable: function(wantedAction) {
  295. var returnValue = false;
  296. var activeCartoonIndex = this.m_model.getActiveCartoonIndex();
  297. var cartoonsAvailable = this.m_model.getCartoonsAvailable();
  298. if(wantedAction.next &&
  299. activeCartoonIndex-1 >= 0) {
  300. returnValue = true;
  301. }
  302. if(wantedAction.previous &&
  303. activeCartoonIndex+1 < cartoonsAvailable) {
  304. returnValue = true;
  305. }
  306. if(wantedAction.newest &&
  307. activeCartoonIndex > 0) {
  308. returnValue = true;
  309. }
  310. return returnValue;
  311. }
  312. };
  313. SingleCartoon.Model = function(subscribers) {
  314. this.m_subscribers = subscribers;
  315. this.m_cartoons = undefined;
  316. this.m_activeCartoonIndex = 0;
  317. };
  318. SingleCartoon.Model.prototype = {
  319. getCartoons: function(id) {
  320. return this.m_cartoons;
  321. },
  322. setCartoons: function(cartoons) {
  323. this.m_cartoons = cartoons;
  324. this.informSubscribers({ cartoons: this.m_cartoons });
  325. },
  326. getCartoonsAvailable: function() {
  327. var cartoonsAvailable = 0;
  328. if(this.m_cartoons) {
  329. cartoonsAvailable = this.m_cartoons.cartoons.length;
  330. }
  331. return cartoonsAvailable;
  332. },
  333. getActiveCartoonIndex: function() {
  334. return this.m_activeCartoonIndex;
  335. },
  336. setActiveCartoonIndex: function(activeCartoonIndex) {
  337. this.m_activeCartoonIndex = activeCartoonIndex;
  338. this.informSubscribers({ activeCartoonIndex: this.m_activeCartoonIndex,
  339. activeCartoonItem: this.m_cartoons.cartoons[this.m_activeCartoonIndex]});
  340. },
  341. informSubscribers: function(changes) {
  342. for (var i=0; i<this.m_subscribers.length; i++) {
  343. this.m_subscribers[i].receiveChangedValue(changes);
  344. }
  345. }
  346. };
  347. SingleCartoon.View = function(elements) {
  348. this.m_elements = elements;
  349. this.m_end = 0;
  350. this.setup(elements);
  351. this.m_activeCartoonItem = undefined;
  352. };
  353. SingleCartoon.View.prototype = {
  354. setup: function(elements) {
  355. //this.fadeIn("slow");
  356. },
  357. fadeIn: function(speed, callback) {
  358. this.m_elements.body.fadeIn(speed, callback);
  359. },
  360. fadeOut: function(speed, callback) {
  361. this.m_elements.body.fadeOut(speed, callback);
  362. },
  363. hideCartoon: function(hide,
  364. callback) {
  365. if(!this.cartoonHidden()) {
  366. this.m_elements.cartoonImage.fadeOut('slow', callback);
  367. }
  368. else {
  369. this.m_elements.cartoonImage.fadeIn('slow', callback);
  370. }
  371. },
  372. cartoonHidden: function() {
  373. return (this.m_elements.cartoonImage.css("display") === 'none');
  374. },
  375. hideNextButton: function(hide ,speed, callback) {
  376. var nextButton = this.m_elements.navigation.find("#nextButton");
  377. if(hide) {
  378. nextButton.hide(speed, callback);
  379. }
  380. else {
  381. nextButton.show(speed, callback);
  382. }
  383. },
  384. nextButtonHidden: function() {
  385. var nextButton = this.m_elements.navigation.find("#nextButton");
  386. return (nextButton.attr("display") === 'none');
  387. },
  388. hidePreviousButton: function(hide, speed, callback) {
  389. var previousButton = this.m_elements.navigation.find("#previousButton");
  390. if(hide) {
  391. previousButton.hide(speed, callback);
  392. }
  393. else {
  394. previousButton.show(speed, callback);
  395. }
  396. },
  397. previousButtonHidden: function() {
  398. var previousButton = this.m_elements.navigation.find("#previousButton");
  399. return (previousButton.attr("display") === 'none');
  400. },
  401. updateCartoon: function(cartoons) {
  402. this.setCartoonName(cartoons.title);
  403. this.m_end = cartoons.cartoons.length;
  404. },
  405. updateActiveCartoonItem: function(activeCartoonItem) {
  406. this.m_activeCartoonItem = activeCartoonItem;
  407. this.setCartoonTitle(activeCartoonItem.title);
  408. this.setCartoonTitleText(activeCartoonItem.titleText);
  409. this.setCartoonAltText(activeCartoonItem.altText);
  410. this.setCartoonPublished(activeCartoonItem.date);
  411. this.setCartoonURL(activeCartoonItem.URL);
  412. this.setCartoonImageURL(activeCartoonItem.imageURL);
  413. },
  414. updateNavigation: function(navigation,
  415. itemsAvailable,
  416. activeCartoonIndex) {
  417. this.updatePreviousButton(navigation.find("#previousButton"),
  418. itemsAvailable,
  419. activeCartoonIndex);
  420. this.updateNextButton(navigation.find("#nextButton"),
  421. itemsAvailable,
  422. activeCartoonIndex);
  423. this.updateNewestButton(navigation.find("#newestButton"),
  424. itemsAvailable,
  425. activeCartoonIndex);
  426. },
  427. updatePreviousButton: function(previousButton,
  428. itemsAvailable,
  429. activeCartoonIndex) {
  430. },
  431. updateNextButton: function(nextButton,
  432. itemsAvailable,
  433. activeCartoonIndex) {
  434. },
  435. updateNewestButton: function(newestButton,
  436. itemsAvailable,
  437. activeCartoonIndex) {
  438. },
  439. setCartoonName: function(name) {
  440. this.m_elements.cartoonName.text(name);
  441. },
  442. setCartoonTitle: function(title) {
  443. this.m_elements.cartoonTitle.text(title);
  444. },
  445. setCartoonTitleText: function(titleText) {
  446. this.m_elements.cartoonTitleText.text(titleText);
  447. this.m_elements.cartoonImage.attr("title", titleText);
  448. },
  449. setCartoonAltText: function(alt) {
  450. this.m_elements.cartoonAltText.text(alt);
  451. this.m_elements.cartoonImage.attr("alt", alt);
  452. },
  453. setCartoonPublished: function(date) {
  454. this.m_elements.cartoonPublished.text(date.toDateString());
  455. },
  456. setCartoonURL: function(url) {
  457. this.m_elements.cartoonURL.empty();
  458. this.m_elements.cartoonURL.append("<a href=\"" + url + "\">" + url + "</a>");
  459. },
  460. setCartoonImageURL: function(url) {
  461. this.m_elements.cartoonImage.attr("src", url);
  462. },
  463. infoOn: function() {
  464. return (this.m_elements.cartoonInfo.css('display') !== 'none');
  465. },
  466. toggleInfo: function(infoOn) {
  467. if(infoOn) {
  468. this.m_elements.cartoonInfo.fadeIn('slow');
  469. }
  470. else {
  471. this.m_elements.cartoonInfo.fadeOut('slow');
  472. }
  473. },
  474. receiveChangedValue: function(changes) {
  475. if(changes.cartoons) {
  476. this.updateCartoon(changes.cartoons);
  477. }
  478. if(changes.activeCartoonIndex != undefined &&
  479. changes.activeCartoonItem) {
  480. this.updateNavigation(this.m_elements.navigation,
  481. this.m_end,
  482. changes.activeCartoonIndex);
  483. this.updateActiveCartoonItem(changes.activeCartoonItem);
  484. }
  485. }
  486. };
  487. launch();