main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. ///VARIABILI GLOBALI///
  20. var FONTE = FONTE || {};
  21. FONTE.dbName = "xm24";
  22. FONTE.dbHandler;
  23. FONTE.updatedCategoriesCount;
  24. FONTE.updatedPostsCount;
  25. FONTE.neededPostsCount;
  26. FONTE.tagsPageInitiated = false;
  27. FONTE.categoriesList = [8, 497];
  28. FONTE.categories = {
  29. ilove : [497,647,590], //ilove, 4mar, solid
  30. info : [8,42,68,3], //ilove, 4mar, solid
  31. };
  32. FONTE.page_loaded = {
  33. ilove : 0,
  34. info : 0,
  35. };
  36. FONTE.hrefseguente = "";
  37. FONTE.contentListId = '#mostralistainfo';
  38. FONTE.contentListClass = '.news';
  39. FONTE.ajaxError = false;
  40. FONTE.back_pressed = false;
  41. /*
  42. * data ultimo aggiornamento
  43. */
  44. function displayLastUpdateDate() {
  45. var lastUpdate = window.localStorage.getItem('db_update_date');
  46. if (lastUpdate) {
  47. var data = lastUpdate.split('T');
  48. var datagiorno = data[0];
  49. var dataora = data[1];
  50. $("#datadiritti").empty();
  51. $("#datadirittiora").empty();
  52. display_results("#datadiritti", datagiorno);
  53. display_results("#datadirittiora", dataora);
  54. if (data) {
  55. $("#boxdata").css("display", "block");
  56. }
  57. } else {
  58. toastr.warning('Warn: manca la data del tuo ultimo aggiornamento. Aggiorna al più presto.');
  59. }
  60. }
  61. function alterContent(url) {
  62. var paginacorrente = $("body").pagecontainer("getActivePage").attr("id");
  63. var oldUrl = url;
  64. $( "#outside" ).panel( "close" );
  65. // Alter the url according to the anchor's href attribute, and
  66. // store the data-foo attribute information with the url
  67. $.mobile.navigate(url);
  68. switch (oldUrl) {
  69. case '#index':
  70. $("#navheader").addClass("hide");
  71. $("#navfooter").addClass("hide");
  72. break;
  73. case '#about':
  74. $("#navheader").removeClass("hide");
  75. $("#navfooter").addClass("hide");
  76. if(window.localStorage.getItem('app_firsttime') === "false") {
  77. $("#init-btn").addClass("hide");
  78. } else {
  79. window.localStorage.setItem('app_firsttime', false);
  80. }
  81. break;
  82. case '#ilove':
  83. $("#navheader").removeClass("hide");
  84. $("#navfooter").addClass("hide");
  85. if(checkConnection()) {
  86. postsUpdate(FONTE.categories.ilove);
  87. } else {
  88. renderLastNews(FONTE.contentListClass, FONTE.categories.ilove);
  89. }
  90. break;
  91. case '#info':
  92. $("#navheader").removeClass("hide");
  93. $("#navfooter").addClass("hide");
  94. if(checkConnection()) {
  95. postsUpdate(FONTE.categories.info);
  96. } else {
  97. renderLastNews(FONTE.contentListClass, FONTE.categories.info);
  98. }
  99. break;
  100. case '#events':
  101. $("#navheader").removeClass("hide");
  102. $("#navfooter").addClass("hide");
  103. EVENTS.render(); //il checkconenction è fatto dentro
  104. break;
  105. case '#laboratori':
  106. $("#navheader").removeClass("hide");
  107. $("#navfooter").addClass("hide");
  108. PROJECT.render();
  109. break;
  110. case '#aggiorna':
  111. $("#navheader").removeClass("hide");
  112. $("#navfooter").removeClass("hide");
  113. // ultimoAggiornamento();
  114. break;
  115. case '#ultime':
  116. case '#tag':
  117. $("#navheader").removeClass("hide");
  118. $("#navfooter").removeClass("hide");
  119. break;
  120. case '#quotidiano':
  121. $("#navheader").removeClass("hide");
  122. $("#navfooter").removeClass("hide");
  123. myJsonRandomPost('#titolosingleR', '#mostracontentsingleR');
  124. break;
  125. }
  126. }
  127. /*///////////////utilities//////////////http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript*/
  128. function getParameterByName(name, url) {
  129. if (!url) url = window.location.href;
  130. name = name.replace(/[\[\]]/g, "\\$&");
  131. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  132. results = regex.exec(url);
  133. if (!results) return null;
  134. if (!results[2]) return '';
  135. return decodeURIComponent(results[2].replace(/\+/g, " "));
  136. }
  137. function display_results(contenitore, messaggio) {
  138. $(contenitore).text(messaggio);
  139. }
  140. $(function () {
  141. $("form").submit(function () {
  142. return false;
  143. });
  144. });
  145. function initMainPage() {
  146. if (checkConnection()) {
  147. postsUpdate(FONTE.categories.info);
  148. } else {
  149. renderLastNews(FONTE.contentListClass, FONTE.categories.info);
  150. }
  151. }
  152. /*per link in browser di default */
  153. document.addEventListener("deviceready", onDeviceReady, false);
  154. //le cose all'inizio
  155. $(window).on("navigate", function (event, data) {
  156. var direction = data.state.direction;
  157. if (direction == 'back') {
  158. FONTE.back_pressed = true;
  159. }
  160. else {
  161. FONTE.back_pressed = false;
  162. }
  163. });
  164. $(document).on("pagecontainershow", function (e, ui) {
  165. var pageId = $('body').pagecontainer('getActivePage').prop('id');
  166. if ((pageId == 'ilove' || pageId == 'info' || pageId == 'events') && !FONTE.back_pressed) {
  167. $.mobile.loading('show');
  168. }
  169. });
  170. $(document).on("mobileinit", function () {
  171. $("body").pagecontainer({
  172. defaults: true
  173. });
  174. $(".ui-loader").loader({
  175. defaults: true
  176. });
  177. $("input").button();
  178. });
  179. $(document).on("pagecontainerchange", function () {
  180. var current = $(".ui-page-active").prop("id");     // Remove active class from nav buttons
  181. if(current == 'about' || current == 'info' || current == 'ilove' || current == 'events' || current == 'cerca' || current == 'laboratori'){
  182. $("#navfooter").addClass("hide");
  183. } else {
  184. $("#navfooter").removeClass("hide");
  185. }
  186. console.log("current" + current);
  187. $("[data-role='navbar'] a.ui-btn-active").removeClass("ui-btn-active");     // Add active class to current nav button
  188.     
  189. $("[data-role='navbar'] a").each(function () {
  190. var href = $(this).prop("href");        
  191. if (href.indexOf(current, href.length - current.length) !== -1) {            
  192. $(this).addClass("ui-btn-active");        
  193. }    
  194. });
  195. });
  196. $(document).on("pagecreate", function () {
  197. $("body > [data-role='panel']").panel();
  198. $("body > [data-role='panel'] [data-role='listview']").listview();
  199. $("body > [data-role='header']").toolbar();
  200. $("body > [data-role='footer']").toolbar();
  201. $("body > [data-role='navbar']").navbar();
  202. $("#navfooter").navbar();
  203. postInList();
  204. });
  205. /*per inizio navigazione*/
  206. var Site = function () {
  207. this.homepage = "#index"; // the default page that is loaded
  208. this.init();
  209. };
  210. Site.prototype.init = function () {
  211. var self = this;
  212. $(document).ready(function () {
  213. // default page if no hash given
  214. // this changes the hash, so the event is fired
  215. // no need to call self.navigate
  216. window.location.hash = self.homepage;
  217. });
  218. };
  219. function onDeviceReady() {
  220. //Setto il locale delle plugin delle date
  221. moment.locale('it');
  222. createDB();
  223. initStorage();
  224. if(window.localStorage.getItem('app_firsttime') !== "false") {
  225. alterContent('#about');
  226. } else {
  227. initMainPage();
  228. }
  229. overlayblock();
  230. $("a.nav").on("click", function (event) {
  231. // Prevent the usual navigation behavior
  232. event.preventDefault();
  233. alterContent($(this).attr("href"));
  234. });
  235. $("button.nav").on("click", function (event) {
  236. alterContent($(this).attr("href"));
  237. });
  238. // eliminare le due righe:
  239. $("body > [data-role='header']").toolbar();
  240. $("[data-role='navbar']").navbar();
  241. //Non uso pageContent per non lanciare 2 volte il recupero dei dati da internet
  242. //per la pagina principale
  243. $("#navheader").removeClass("hide");
  244. $("#navfooter").addClass("hide");
  245. }
  246. function overlayblock() {
  247. $(".overlaynero").toggleClass("hide");
  248. }
  249. /*///////////////fine utilities//////////////*/
  250. var weburl = "http://www.ecn.org/xm24/";
  251. var arrayTag = [];
  252. /*/////finite variabili globali////////*/
  253. function intercettaUrl() {
  254. // LETTURA - Log the results of the navigate event
  255. $(window).on("navigate", function (event, data) {
  256. idPost = getParameterByName('id');
  257. if (idPost !== "") {
  258. myJsonPost(idPost, "#titolosingle", "#mostracontentsingle");
  259. }
  260. });
  261. }
  262. function postInList() {
  263. //SCRITTURA
  264. $("body").on('click', '.listevoci li > a', function (event) {
  265. // console.log("HUU_post in list");
  266. if($(this).attr("rel") === 'external') {
  267. return true;
  268. }
  269. var idOra = $(this).attr("data-post");
  270. var _class = $(this).attr("class");
  271. $("#titolosingle").empty();
  272. $("#mostracontentsingle").empty();
  273. //console.log(idOra);
  274. myJsonPost(idOra, "#titolosingle", "#mostracontentsingle", _class);
  275. // Prevent the usual navigation behavior
  276. event.preventDefault();
  277. /*se serve idTAg, mettiamolo nell'url!! e anche nella page!*/
  278. $("#singleQpost").attr("id", '#singleQpost?id=' + idOra);
  279. $(":mobile-pagecontainer").pagecontainer("load", '#singleQpost' + idOra, {
  280. showLoadMsg: false
  281. });
  282. $("#navfooter").removeClass("hide");
  283. location.hash = '#singleQpost?id=' + idOra;
  284. });
  285. }
  286. function findImg(htmlString) {
  287. var pos = htmlString.indexOf('<img ');
  288. while (pos != -1) {
  289. pos += 5;
  290. htmlString = [htmlString.slice(0, pos), ' onerror="imgNotFound(this);" ', htmlString.slice(pos)].join('');
  291. pos = htmlString.indexOf('<img ', pos);
  292. }
  293. return htmlString;
  294. }
  295. function imgNotFound(img) {
  296. var parent = $(img).parent();
  297. parent.empty();
  298. parent.append('<span>Immagine disponibile online.</span>');
  299. }
  300. function JSONErrorHandler(jqXHR, textStatus, errorThrown) {
  301. $.mobile.loading('hide');
  302. if (!FONTE.ajaxError) {
  303. var errMsg = "Err: ";
  304. if(jqXHR.status === 404 ) {
  305. errMsg += "Sito non raggiungibile. ";
  306. } else if(jqXHR.status === 0) {
  307. return checkConnection(jqXHR.status, "Occore essere connessi per poter ricevere altri articoli");
  308. }
  309. toastr.error(errMsg);
  310. FONTE.ajaxError = true;
  311. }
  312. }
  313. function segnalaBug() {
  314. return checkConnection('ERR_SEGNALA');
  315. }
  316. function mostraEsperto() {
  317. var ret = checkConnection('ERR_ESPERTO');
  318. if (ret) {
  319. myJsonPage(2167, '#espertotitle', '#mostraesperto');
  320. }
  321. return ret;
  322. }
  323. function checkAndUpdate() {
  324. var ret = checkConnection('ERR_ESPERTO');
  325. if (ret) {
  326. update();
  327. }
  328. return ret;
  329. }
  330. function checkAndReset() {
  331. var ret = checkConnection('ERR_ESPERTO');
  332. if (ret) {
  333. resetAndUpdate();
  334. }
  335. return ret;
  336. }