main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 '#aggiorna':
  92. $("#navheader").removeClass("hide");
  93. $("#navfooter").removeClass("hide");
  94. // ultimoAggiornamento();
  95. break;
  96. case '#ultime':
  97. case '#tag':
  98. $("#navheader").removeClass("hide");
  99. $("#navfooter").removeClass("hide");
  100. break;
  101. case '#quotidiano':
  102. $("#navheader").removeClass("hide");
  103. $("#navfooter").removeClass("hide");
  104. myJsonRandomPost('#titolosingleR', '#mostracontentsingleR');
  105. break;
  106. }
  107. // Alter the url according to the anchor's href attribute, and
  108. // store the data-foo attribute information with the url
  109. $.mobile.navigate(url);
  110. }
  111. /*///////////////utilities//////////////http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript*/
  112. function getParameterByName(name, url) {
  113. if (!url) url = window.location.href;
  114. name = name.replace(/[\[\]]/g, "\\$&");
  115. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  116. results = regex.exec(url);
  117. if (!results) return null;
  118. if (!results[2]) return '';
  119. return decodeURIComponent(results[2].replace(/\+/g, " "));
  120. }
  121. function display_results(contenitore, messaggio) {
  122. $(contenitore).text(messaggio);
  123. }
  124. $(function () {
  125. $("form").submit(function () {
  126. return false;
  127. });
  128. });
  129. function benvenuta() {
  130. $("#benvenuta").popup("open");
  131. }
  132. //function aggiornaora() {
  133. // $("#aggiornaora").popup("open");
  134. //};
  135. function initMainPage() {
  136. if (checkConnection()) {
  137. postsUpdate(FONTE.categories.info);
  138. } else {
  139. renderLastNews(FONTE.contentListClass, FONTE.categories.info);
  140. }
  141. }
  142. /*per link in browser di default */
  143. document.addEventListener("deviceready", onDeviceReady, false);
  144. //le cose all'inizio
  145. $(document).on("mobileinit", function () {
  146. $("body").pagecontainer({
  147. defaults: true
  148. });
  149. $(".ui-loader").loader({
  150. defaults: true
  151. });
  152. $("input").button();
  153. });
  154. $(document).on("pagecontainerchange", function () {
  155. var current = $(".ui-page-active").prop("id");     // Remove active class from nav buttons
  156. if(current == 'about' || current == 'info' || current == 'ilove' || current == 'events'){
  157. $("#navfooter").addClass("hide");
  158. } else {
  159. $("#navfooter").removeClass("hide");
  160. }
  161. console.log("current" + current);
  162. $("[data-role='navbar'] a.ui-btn-active").removeClass("ui-btn-active");     // Add active class to current nav button
  163.     
  164. $("[data-role='navbar'] a").each(function () {
  165. var href = $(this).prop("href");        
  166. if (href.indexOf(current, href.length - current.length) !== -1) {            
  167. $(this).addClass("ui-btn-active");        
  168. }    
  169. });
  170. });
  171. $(document).on("pagecreate", function () {
  172. $("body > [data-role='panel']").panel();
  173. $("body > [data-role='panel'] [data-role='listview']").listview();
  174. $("body > [data-role='header']").toolbar();
  175. $("body > [data-role='footer']").toolbar();
  176. $("body > [data-role='navbar']").navbar();
  177. $("#navfooter").navbar();
  178. postInList();
  179. });
  180. /*per inizio navigazione*/
  181. var Site = function () {
  182. this.homepage = "#index"; // the default page that is loaded
  183. this.init();
  184. };
  185. Site.prototype.init = function () {
  186. var self = this;
  187. $(document).ready(function () {
  188. // default page if no hash given
  189. // this changes the hash, so the event is fired
  190. // no need to call self.navigate
  191. window.location.hash = self.homepage;
  192. });
  193. };
  194. // On document ready
  195. $(function () {
  196. // intercettaUrl();
  197. // new Site();
  198. // $(".minibox").click(function () {});
  199. });
  200. function onDeviceReady() {
  201. // console.log('deviceready');
  202. createDB();
  203. initStorage();
  204. if(window.localStorage.getItem('app_firsttime') !== "false") {
  205. alterContent('#about');
  206. } else {
  207. initMainPage();
  208. }
  209. overlayblock();
  210. $("a.nav").on("click", function (event) {
  211. // Prevent the usual navigation behavior
  212. event.preventDefault();
  213. alterContent($(this).attr("href"));
  214. });
  215. $("button.nav").on("click", function (event) {
  216. alterContent($(this).attr("href"));
  217. });
  218. // eliminare le due righe:
  219. $("body > [data-role='header']").toolbar();
  220. $("[data-role='navbar']").navbar();
  221. //Non uso pageContent per non lanciare 2 volte il recupero dei dati da internet
  222. $("#navheader").removeClass("hide");
  223. $("#navfooter").addClass("hide");
  224. }
  225. function overlayblock() {
  226. $(".overlaynero").toggleClass("hide");
  227. }
  228. /*///////////////fine utilities//////////////*/
  229. var weburl = "http://www.ecn.org/xm24/";
  230. var arrayTag = [];
  231. /*/////finite variabili globali////////*/
  232. function intercettaUrl() {
  233. // LETTURA - Log the results of the navigate event
  234. $(window).on("navigate", function (event, data) {
  235. idPost = getParameterByName('id');
  236. if (idPost !== "") {
  237. myJsonPost(idPost, "#titolosingle", "#mostracontentsingle");
  238. }
  239. });
  240. }
  241. function postInList() {
  242. //SCRITTURA
  243. $("body").on('click', '.listevoci li > a', function (event) {
  244. // console.log("HUU_post in list");
  245. var idOra = $(this).attr("data-post");
  246. var _class = $(this).attr("class");
  247. $("#titolosingle").empty();
  248. $("#mostracontentsingle").empty();
  249. //console.log(idOra);
  250. myJsonPost(idOra, "#titolosingle", "#mostracontentsingle", _class);
  251. // Prevent the usual navigation behavior
  252. event.preventDefault();
  253. /*se serve idTAg, mettiamolo nell'url!! e anche nella page!*/
  254. $("#singleQpost").attr("id", '#singleQpost?id=' + idOra);
  255. $(":mobile-pagecontainer").pagecontainer("load", '#singleQpost' + idOra, {
  256. showLoadMsg: false
  257. });
  258. $("#navfooter").removeClass("hide");
  259. location.hash = '#singleQpost?id=' + idOra;
  260. });
  261. }
  262. function cercaMe() {
  263. var input_text = $("input.cerca").val();
  264. input_text = encodeURIComponent(input_text);
  265. var jsonurl = weburl + "wp-json/wp/v2/posts?per_page=10&search=" + input_text + "";
  266. $(".listresult").empty();
  267. $("body").pagecontainer("change", "#ricerca");
  268. $.mobile.loading('show');
  269. var lastUpdate = window.localStorage.getItem('db_update_date');
  270. if (lastUpdate) {
  271. var lastUpdateDate = Date.parse(lastUpdate);
  272. $.ajax({
  273. dataType: "json",
  274. url: jsonurl,
  275. success: function (data) {
  276. var items = [];
  277. $.each(data, function (key, val) {
  278. var id = val.id;
  279. var time = val.modified;
  280. var title = val.title.rendered;
  281. var url = val.guid.rendered;
  282. var tag = data.tags;
  283. var splitTime = time.split("T");
  284. var cleanTime = splitTime[0];
  285. var dTime = new Date(time);
  286. if (dTime.getTime() <= lastUpdateDate) {
  287. //key mi dice l'indice'
  288. items.push("<li><a href='#singleQpost' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small></li>");
  289. } else {
  290. items.push("<p><a href='#' onclick='aggiornaora();' style='color:#666;' data-post='" + id + "'>" + title + "</a><br/><small>" + cleanTime + "</small></p>");
  291. }
  292. });
  293. $("<ul/>", {
  294. "class": "my-new-list",
  295. html: items.join("")
  296. }).prependTo(".listresult");
  297. $.mobile.loading('hide');
  298. },
  299. error: JSONErrorHandler
  300. });
  301. } else {
  302. toastr.error('Err: ERR_NOUPDATE');
  303. }
  304. }
  305. function findImg(htmlString) {
  306. var pos = htmlString.indexOf('<img ');
  307. while (pos != -1) {
  308. pos += 5;
  309. htmlString = [htmlString.slice(0, pos), ' onerror="imgNotFound(this);" ', htmlString.slice(pos)].join('');
  310. pos = htmlString.indexOf('<img ', pos);
  311. }
  312. return htmlString;
  313. }
  314. function imgNotFound(img) {
  315. var parent = $(img).parent();
  316. parent.empty();
  317. parent.append('<span>Immagine disponibile online.</span>');
  318. }
  319. function JSONErrorHandler(jqXHR, textStatus, errorThrown) {
  320. if (!FONTE.ajaxError) {
  321. var errMsg = "Err: ";
  322. if(jqXHR.status === 404) {
  323. errMsg += "Sito non raggiungibile. ";
  324. }
  325. toastr.error(errMsg);
  326. FONTE.ajaxError = true;
  327. }
  328. }
  329. function cerca() {
  330. var ret = checkConnection('ERR_CERCA');
  331. if (ret) {
  332. cercaMe();
  333. }
  334. return ret;
  335. }
  336. function segnalaBug() {
  337. return checkConnection('ERR_SEGNALA');
  338. }
  339. function mostraEsperto() {
  340. var ret = checkConnection('ERR_ESPERTO');
  341. if (ret) {
  342. myJsonPage(2167, '#espertotitle', '#mostraesperto');
  343. }
  344. return ret;
  345. }
  346. function checkAndUpdate() {
  347. var ret = checkConnection('ERR_ESPERTO');
  348. if (ret) {
  349. update();
  350. }
  351. return ret;
  352. }
  353. function checkAndReset() {
  354. var ret = checkConnection('ERR_ESPERTO');
  355. if (ret) {
  356. resetAndUpdate();
  357. }
  358. return ret;
  359. }