main.js 10 KB

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