main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. //Se si lancia il loader prima che sia cambiata pagina il loader scompare,
  69. // ritardando invece che appaia, quando appare è già sulla pagina in cambiata
  70. // setTimeout(function () {
  71. // $.mobile.loading('show');
  72. // }, 300);
  73. switch (oldUrl) {
  74. case '#index':
  75. $("#navheader").addClass("hide");
  76. $("#navfooter").addClass("hide");
  77. break;
  78. case '#about':
  79. $("#navheader").removeClass("hide");
  80. $("#navfooter").addClass("hide");
  81. if(window.localStorage.getItem('app_firsttime') === "false") {
  82. $("#init-btn").addClass("hide");
  83. } else {
  84. window.localStorage.setItem('app_firsttime', false);
  85. }
  86. break;
  87. case '#ilove':
  88. $("#navheader").removeClass("hide");
  89. $("#navfooter").addClass("hide");
  90. postsUpdate(FONTE.categories.ilove);
  91. break;
  92. case '#info':
  93. $("#navheader").removeClass("hide");
  94. $("#navfooter").addClass("hide");
  95. postsUpdate(FONTE.categories.info);
  96. break;
  97. case '#events':
  98. $("#navheader").removeClass("hide");
  99. $("#navfooter").addClass("hide");
  100. EVENTS.render();
  101. break;
  102. case '#aggiorna':
  103. $("#navheader").removeClass("hide");
  104. $("#navfooter").removeClass("hide");
  105. // ultimoAggiornamento();
  106. break;
  107. case '#ultime':
  108. case '#tag':
  109. $("#navheader").removeClass("hide");
  110. $("#navfooter").removeClass("hide");
  111. break;
  112. case '#quotidiano':
  113. $("#navheader").removeClass("hide");
  114. $("#navfooter").removeClass("hide");
  115. myJsonRandomPost('#titolosingleR', '#mostracontentsingleR');
  116. break;
  117. }
  118. // $( "#outside" ).panel( "close" );
  119. // // Alter the url according to the anchor's href attribute, and
  120. // // store the data-foo attribute information with the url
  121. // $.mobile.navigate(url);
  122. }
  123. /*///////////////utilities//////////////http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript*/
  124. function getParameterByName(name, url) {
  125. if (!url) url = window.location.href;
  126. name = name.replace(/[\[\]]/g, "\\$&");
  127. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  128. results = regex.exec(url);
  129. if (!results) return null;
  130. if (!results[2]) return '';
  131. return decodeURIComponent(results[2].replace(/\+/g, " "));
  132. }
  133. function display_results(contenitore, messaggio) {
  134. $(contenitore).text(messaggio);
  135. }
  136. $(function () {
  137. $("form").submit(function () {
  138. return false;
  139. });
  140. });
  141. function initMainPage() {
  142. $.mobile.loading('show');
  143. if (checkConnection()) {
  144. postsUpdate(FONTE.categories.info);
  145. } else {
  146. renderLastNews(FONTE.contentListClass, FONTE.categories.info);
  147. }
  148. }
  149. /*per link in browser di default */
  150. document.addEventListener("deviceready", onDeviceReady, false);
  151. //le cose all'inizio
  152. $(window).on("navigate", function (event, data) {
  153. var direction = data.state.direction;
  154. if (direction == 'back') {
  155. FONTE.back_pressed = true;
  156. }
  157. else {
  158. FONTE.back_pressed = false;
  159. }
  160. });
  161. $(document).on("pagecontainershow", function (e, ui) {
  162. var pageId = $('body').pagecontainer('getActivePage').prop('id');
  163. if ((pageId == 'ilove' || pageId == 'info' || pageId == 'events') && !FONTE.back_pressed) {
  164. $.mobile.loading('show');
  165. }
  166. });
  167. $(document).on("mobileinit", function () {
  168. $("body").pagecontainer({
  169. defaults: true
  170. });
  171. $(".ui-loader").loader({
  172. defaults: true
  173. });
  174. $("input").button();
  175. });
  176. $(document).on("pagecontainerchange", function () {
  177. var current = $(".ui-page-active").prop("id");     // Remove active class from nav buttons
  178. if(current == 'about' || current == 'info' || current == 'ilove' || current == 'events' || current == 'cerca'){
  179. $("#navfooter").addClass("hide");
  180. } else {
  181. $("#navfooter").removeClass("hide");
  182. }
  183. console.log("current" + current);
  184. $("[data-role='navbar'] a.ui-btn-active").removeClass("ui-btn-active");     // Add active class to current nav button
  185.     
  186. $("[data-role='navbar'] a").each(function () {
  187. var href = $(this).prop("href");        
  188. if (href.indexOf(current, href.length - current.length) !== -1) {            
  189. $(this).addClass("ui-btn-active");        
  190. }    
  191. });
  192. });
  193. $(document).on("pagecreate", function () {
  194. $("body > [data-role='panel']").panel();
  195. $("body > [data-role='panel'] [data-role='listview']").listview();
  196. $("body > [data-role='header']").toolbar();
  197. $("body > [data-role='footer']").toolbar();
  198. $("body > [data-role='navbar']").navbar();
  199. $("#navfooter").navbar();
  200. postInList();
  201. });
  202. /*per inizio navigazione*/
  203. var Site = function () {
  204. this.homepage = "#index"; // the default page that is loaded
  205. this.init();
  206. };
  207. Site.prototype.init = function () {
  208. var self = this;
  209. $(document).ready(function () {
  210. // default page if no hash given
  211. // this changes the hash, so the event is fired
  212. // no need to call self.navigate
  213. window.location.hash = self.homepage;
  214. });
  215. };
  216. // On document ready
  217. $(function () {
  218. // intercettaUrl();
  219. // new Site();
  220. // $(".minibox").click(function () {});
  221. });
  222. function onDeviceReady() {
  223. // console.log('deviceready');
  224. createDB();
  225. initStorage();
  226. if(window.localStorage.getItem('app_firsttime') !== "false") {
  227. alterContent('#about');
  228. } else {
  229. initMainPage();
  230. }
  231. overlayblock();
  232. $("a.nav").on("click", function (event) {
  233. // Prevent the usual navigation behavior
  234. event.preventDefault();
  235. alterContent($(this).attr("href"));
  236. });
  237. $("button.nav").on("click", function (event) {
  238. alterContent($(this).attr("href"));
  239. });
  240. // eliminare le due righe:
  241. $("body > [data-role='header']").toolbar();
  242. $("[data-role='navbar']").navbar();
  243. //Non uso pageContent per non lanciare 2 volte il recupero dei dati da internet
  244. //per la pagina principale
  245. $("#navheader").removeClass("hide");
  246. $("#navfooter").addClass("hide");
  247. }
  248. function overlayblock() {
  249. $(".overlaynero").toggleClass("hide");
  250. }
  251. /*///////////////fine utilities//////////////*/
  252. var weburl = "http://www.ecn.org/xm24/";
  253. var arrayTag = [];
  254. /*/////finite variabili globali////////*/
  255. function intercettaUrl() {
  256. // LETTURA - Log the results of the navigate event
  257. $(window).on("navigate", function (event, data) {
  258. idPost = getParameterByName('id');
  259. if (idPost !== "") {
  260. myJsonPost(idPost, "#titolosingle", "#mostracontentsingle");
  261. }
  262. });
  263. }
  264. function postInList() {
  265. //SCRITTURA
  266. $("body").on('click', '.listevoci li > a', function (event) {
  267. // console.log("HUU_post in list");
  268. if($(this).attr("rel") === 'external') {
  269. return true;
  270. }
  271. var idOra = $(this).attr("data-post");
  272. var _class = $(this).attr("class");
  273. $("#titolosingle").empty();
  274. $("#mostracontentsingle").empty();
  275. //console.log(idOra);
  276. myJsonPost(idOra, "#titolosingle", "#mostracontentsingle", _class);
  277. // Prevent the usual navigation behavior
  278. event.preventDefault();
  279. /*se serve idTAg, mettiamolo nell'url!! e anche nella page!*/
  280. $("#singleQpost").attr("id", '#singleQpost?id=' + idOra);
  281. $(":mobile-pagecontainer").pagecontainer("load", '#singleQpost' + idOra, {
  282. showLoadMsg: false
  283. });
  284. $("#navfooter").removeClass("hide");
  285. location.hash = '#singleQpost?id=' + idOra;
  286. });
  287. }
  288. function findImg(htmlString) {
  289. var pos = htmlString.indexOf('<img ');
  290. while (pos != -1) {
  291. pos += 5;
  292. htmlString = [htmlString.slice(0, pos), ' onerror="imgNotFound(this);" ', htmlString.slice(pos)].join('');
  293. pos = htmlString.indexOf('<img ', pos);
  294. }
  295. return htmlString;
  296. }
  297. function imgNotFound(img) {
  298. var parent = $(img).parent();
  299. parent.empty();
  300. parent.append('<span>Immagine disponibile online.</span>');
  301. }
  302. function JSONErrorHandler(jqXHR, textStatus, errorThrown) {
  303. $.mobile.loading('hide');
  304. if (!FONTE.ajaxError) {
  305. var errMsg = "Err: ";
  306. if(jqXHR.status === 404 ) {
  307. errMsg += "Sito non raggiungibile. ";
  308. } else if(jqXHR.status === 0) {
  309. return checkConnection(jqXHR.status, "Occore essere connessi per poter ricevere altri articoli");
  310. }
  311. toastr.error(errMsg);
  312. FONTE.ajaxError = true;
  313. }
  314. }
  315. function segnalaBug() {
  316. return checkConnection('ERR_SEGNALA');
  317. }
  318. function mostraEsperto() {
  319. var ret = checkConnection('ERR_ESPERTO');
  320. if (ret) {
  321. myJsonPage(2167, '#espertotitle', '#mostraesperto');
  322. }
  323. return ret;
  324. }
  325. function checkAndUpdate() {
  326. var ret = checkConnection('ERR_ESPERTO');
  327. if (ret) {
  328. update();
  329. }
  330. return ret;
  331. }
  332. function checkAndReset() {
  333. var ret = checkConnection('ERR_ESPERTO');
  334. if (ret) {
  335. resetAndUpdate();
  336. }
  337. return ret;
  338. }