PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ElmcityAdmin/elmcity-1.15.js

https://github.com/judell/elmcity
JavaScript | 1130 lines | 1018 code | 95 blank | 17 comment | 61 complexity | 954238f4d29d481f0a5fb38b31f565cc MD5 | raw file
  1. var host = 'http://elmcity.cloudapp.net/';
  2. var blobhost = 'http://elmcity.blob.core.windows.net/';
  3. var anchor_names = [];
  4. var today = new Date();
  5. var last_day;
  6. var datepicker = false;
  7. //var is_mobile = false;
  8. //var is_mobile_declared = false;
  9. //var is_mobile_detected = false;
  10. var is_eventsonly = false;
  11. var is_theme = false;
  12. var is_view = false;
  13. var is_sidebar = true;
  14. var show_images = true;
  15. var hide_maps = true;
  16. var top_method = 0; // for use in position_sidebar
  17. var default_args = {};
  18. var $j = jQuery.noConflict();
  19. var redirected_hubs = [ 'AnnArborChronicle'];
  20. var redirected_hubs_dict = { 'AnnArborChronicle':'events.annarborchronicle.com' };
  21. var category_images = {};
  22. var source_images = {};
  23. function position_sidebar(top_element)
  24. {
  25. try
  26. {
  27. var top_elt_bottom = $j('#' + top_element)[0].getClientRects()[0].bottom;
  28. }
  29. catch (e)
  30. {
  31. console.log(e.message);
  32. top_elt_bottom = 0;
  33. }
  34. if ( top_elt_bottom <= 0 )
  35. $j('#sidebar').css('top', $j(window).scrollTop() - top_offset + 'px');
  36. else
  37. $j('#sidebar').css('top', top_method);
  38. }
  39. function on_load()
  40. {
  41. }
  42. function get_elmcity_id()
  43. {
  44. return $j('#elmcity_id').text().trim();
  45. }
  46. function get_view()
  47. {
  48. return $j('#view').text().trim();
  49. }
  50. function get_selected_hub()
  51. {
  52. return $j('#hub_select option:selected').val();
  53. }
  54. function get_generated_hub()
  55. {
  56. return $j('#hub').text().trim();
  57. }
  58. function gup( name )
  59. {
  60. var value = default_args[name];
  61. if ( value == null ) value = '';
  62. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  63. var regexS = "[\\?&]"+name+"=([^&#]*)";
  64. var regex = new RegExp( regexS );
  65. var results = regex.exec( window.location.href );
  66. if( results != null )
  67. value = results[1].replace(/%20/,' ');
  68. return value;
  69. }
  70. function parse_yyyy_mm_dd(date_str)
  71. {
  72. var match = /(\d{4,4})(\d{2,2})(\d{2,2})/.exec(date_str);
  73. return { year: match[1], month: match[2], day: match[3] }
  74. }
  75. function parse_mm_dd_yyyy(date_str)
  76. {
  77. var match = /(\d{2,2})\/(\d{2,2})\/(\d{4,4})/.exec(date_str);
  78. return { month: match[1], day: match[2], year: match[3] }
  79. }
  80. function parse_yyyy_mm_dd_T_hh_mm(date_str)
  81. {
  82. var match = /(\d{4,4})-(\d+)-(\d+)T(\d+):(\d+)/.exec(date_str);
  83. return { year: match[1], month: match[2], day: match[3], hour: match[4], minute: match[5] }
  84. }
  85. function scroll(event)
  86. {
  87. if ( is_eventsonly )
  88. return;
  89. if ( $j('#sidebar').css('position') != 'fixed' ) // unframed, no fixed elements
  90. {
  91. position_sidebar(top_element);
  92. // position_ad();
  93. }
  94. var date_str = find_current_name().replace('d','');
  95. var parsed = parse_yyyy_mm_dd(date_str)
  96. setDate(parsed['year'], parsed['month'], parsed['day']);
  97. }
  98. function find_last_day()
  99. {
  100. try
  101. {
  102. var last_anchor = anchor_names[anchor_names.length - 1];
  103. var parsed = parse_yyyy_mm_dd(last_anchor.replace('d',''));
  104. return new Date(parsed['year'], parsed['month'] - 1, parsed['day']);
  105. }
  106. catch (e)
  107. {
  108. return new Date();
  109. }
  110. }
  111. function get_anchor_names(anchors)
  112. {
  113. var anchor_names = [];
  114. for (var i = 0; i < anchors.length; i++)
  115. {
  116. anchor_names.push(anchors[i].name);
  117. }
  118. return anchor_names;
  119. }
  120. function day_anchors()
  121. {
  122. return $j('a[name^="d"]');
  123. }
  124. function find_current_name()
  125. {
  126. if ( is_eventsonly )
  127. return;
  128. // console.log("find_current_name");
  129. try
  130. {
  131. var before = [];
  132. var datepicker_top = $j("#datepicker")[0].getClientRects()[0].top;
  133. var datepicker_bottom = $j("#datepicker")[0].getClientRects()[0].bottom;
  134. var datepicker_height = datepicker_bottom - datepicker_top;
  135. var datepicker_center = datepicker_top + ( datepicker_height / 2 );
  136. var anchors = day_anchors();
  137. for (var i = 0; i < anchors.length; i++)
  138. {
  139. var anchor = anchors[i];
  140. var anchor_top = anchor.getClientRects()[0].top;
  141. if ( anchor_top < datepicker_center )
  142. before.push(anchor.name);
  143. else
  144. break;
  145. }
  146. var ret = before[before.length-1];
  147. if ( typeof ret == 'undefined' )
  148. ret = anchors[0].name;
  149. }
  150. catch (e)
  151. {
  152. console.log("find_current_name: " + e.message);
  153. }
  154. return ret;
  155. }
  156. $j(window).scroll(function(event) {
  157. scroll(event);
  158. });
  159. //$j(window).load(function () {
  160. // window.scrollTo(0,0);
  161. //});
  162. function prep_day_anchors_and_last_day()
  163. {
  164. var anchors = day_anchors();
  165. anchor_names = get_anchor_names(anchors);
  166. last_day = find_last_day();
  167. }
  168. function setup_datepicker()
  169. {
  170. if ( is_eventsonly || datepicker )
  171. return;
  172. // console.log("setup_datepicker");
  173. prep_day_anchors_and_last_day();
  174. $j('#datepicker').datepicker({
  175. onSelect: function(dateText, inst) { goDay(dateText); },
  176. onChangeMonthYear: function(year, month, inst) { goMonth(year, month); },
  177. minDate: today,
  178. maxDate: last_day,
  179. hideIfNoPrevNext: true,
  180. beforeShowDay: maybeShowDay
  181. });
  182. setDate(today.getFullYear(), today.getMonth() + 1, today.getDate());
  183. if ( $j('#sidebar').css('position') != 'fixed' ) // unframed, no fixed elements
  184. {
  185. position_sidebar(top_element)
  186. // position_ad();
  187. $j('#sidebar').css('visibility','visible');
  188. $j('#datepicker').css('visibility','visible');
  189. $j('#tags').css('visibility','visible');
  190. }
  191. datepicker = true;
  192. }
  193. $j(document).ready(function(){
  194. $j('.ttl a').removeAttr('target');
  195. var elmcity_id = get_elmcity_id();
  196. load_category_images(elmcity_id);
  197. load_source_images(elmcity_id);
  198. $j('.sd').css('font-size','x-large');
  199. $j('.atc').css('font-size','x-large');
  200. is_theme = gup('theme') != '';
  201. var view = gup('view');
  202. is_view = view != '';
  203. is_eventsonly = gup('eventsonly').startsWith('y');
  204. if ( is_eventsonly ) {
  205. show_images = false; // default to no images for eventsonly views
  206. }
  207. else {
  208. show_images = true; // default to show images for full views
  209. }
  210. if ( gup('show_images').startsWith('n') ) // optionally override to false
  211. show_images = false;
  212. if ( gup('show_images').startsWith('y') ) // optionally override to true
  213. show_images = true;
  214. if ( gup('hide_maps').startsWith('n') ) // optionally override to false
  215. hide_maps = false;
  216. // is_mobile_declared = gup('mobile').startsWith('y');
  217. // is_mobile_detected = $j('#mobile_detected').text().trim() == "__MOBILE_DETECTED__";
  218. // is_mobile = is_mobile_declared || is_mobile_detected;
  219. if ( is_eventsonly )
  220. $j('.bl').css('margin-right','3%'); // could overwrite theme-defined?
  221. is_sidebar = ! is_eventsonly;
  222. if ( gup('hubtitle').startsWith('n') )
  223. $j('.hubtitle').remove();
  224. if ( gup('tags').startsWith('n') )
  225. $j('.cat').remove();
  226. if ( gup('tags').startsWith('hide') ) // keep them invisibly for use with image display
  227. $j('.cat').css('display','none');
  228. if ( is_view && is_sidebar )
  229. try {
  230. var href = $j('#subscribe').attr('href');
  231. href = href.replace('__VIEW__', gup('view'));
  232. $j('#subscribe').attr('href',href);
  233. $j('#subscribe').text('subscribe');
  234. }
  235. catch (e) {
  236. console.log('cannot activate subscribe link');
  237. }
  238. if ( gup('timeofday') == 'no' )
  239. $j('.timeofday').remove();
  240. if ( gup('datestyle') != '' )
  241. apply_json_css('.ed', 'datestyle');
  242. if ( gup('itemstyle') != '' )
  243. apply_json_css('.bl', 'itemstyle');
  244. if ( gup('titlestyle') != '' )
  245. apply_json_css('.ttl', 'titlestyle');
  246. if ( gup('linkstyle') != '' )
  247. apply_json_css('.ttl a', 'linkstyle');
  248. if ( gup('dtstartstyle') != '' )
  249. apply_json_css('.st', 'dtstartstyle');
  250. if ( gup('sd') != '' )
  251. apply_json_css('.sd', 'sd');
  252. if ( gup('atc') != '' )
  253. apply_json_css('.atc', 'atc');
  254. if ( gup('cat') != '' )
  255. apply_json_css('.cat', 'cat');
  256. if ( gup('sourcestyle') != '' )
  257. apply_json_css('.src', 'sourcestyle');
  258. remember_or_forget_days();
  259. // find noncoalesced ttls, adjust tooltips
  260. var noncoalesced = $j('.ttl a[property="v:summary"]');
  261. noncoalesced.attr('title','see details')
  262. // and hrefs
  263. for ( var i = 0; i < noncoalesced.length; i++ )
  264. {
  265. var id = noncoalesced[i].parentNode.parentNode.getAttribute('id');
  266. $j('#' + id + ' .ttl a').attr('href','javascript:show_desc("' + id + '")');
  267. }
  268. // find coalesced ttls
  269. var coalesced = $j('.ttl span[property="v:summary"]');
  270. // activate their opener links
  271. for ( var i = 0; i < coalesced.length; i++ )
  272. {
  273. var ttl_span_summary = $j('.ttl span[property="v:summary"]')[i];
  274. var id = ttl_span_summary.parentNode.parentNode.getAttribute('id');
  275. var text = $j('.ttl span[property="v:summary"]')[i].innerHTML;
  276. var html = '<a href="javascript:show_desc(\'' + id + '\')">' + text + '</a>';
  277. $j(ttl_span_summary).html(html);
  278. }
  279. $j('.sd').remove();
  280. if ( is_sidebar ) {
  281. show_category_image_under_picker();
  282. setup_datepicker();
  283. if ( ! show_images )
  284. return;
  285. if ( $j.keys(category_images).length == 0 )
  286. return;
  287. var view = gup('view');
  288. if ( typeof(category_images[view]) != 'undefined' && category_images[view] != blobhost + 'admin/NoCurrentImage.jpg' )
  289. {
  290. var href = location.href;
  291. $j('#category_image')[0].innerHTML = '<img style="width:140px;border-width:thin;border-style:solid" src="' + category_images[view] + '">';
  292. }
  293. else
  294. $j('#category_image')[0].innerHTML = '';
  295. }
  296. });
  297. function apply_json_css(element,style)
  298. {
  299. try
  300. {
  301. var style = decodeURIComponent(gup(style));
  302. style = style.replace(/'/g,'"');
  303. $j(element).css(JSON.parse(style));
  304. }
  305. catch (e)
  306. {
  307. console.log(e.message);
  308. }
  309. }
  310. function scrollToElement(id)
  311. {
  312. window.scrollTo(0, $j('#' + id).offset().top);
  313. }
  314. function setDate(year,month,day)
  315. {
  316. // console.log("set_date");
  317. var date = $j('#datepicker').datepicker('getDate');
  318. var current_date = $j('td > a[class~=ui-state-active]');
  319. current_date.css('font-weight', 'normal');
  320. $j('#datepicker').datepicker('setDate', new Date(year, month-1, day));
  321. var td = $j('td[class=ui-datepicker-current-day] > a[class~=ui-state-active]');
  322. var td = $j('td > a[class~=ui-state-active]');
  323. current_date = $j('td > a[class~=ui-state-active]');
  324. current_date.css('font-weight', 'bold');
  325. }
  326. function maybeShowDay(date)
  327. {
  328. var year = date.getFullYear();
  329. var month = date.getMonth() + 1;
  330. var day = date.getDate();
  331. month = maybeZeroPad(month.toString());
  332. day = maybeZeroPad(day.toString());
  333. var date_str = "d" + year + month + day;
  334. show = $j.inArray( date_str, anchor_names ) == -1 ? false : true;
  335. var style = ( show == false ) ? "ui-datepicker-unselectable ui-state-disabled" : "";
  336. return [show, style]
  337. }
  338. function goDay(date_str)
  339. {
  340. var parsed = parse_mm_dd_yyyy(date_str)
  341. var year = parsed['year'];
  342. var month = parsed['month'];
  343. var day = parsed['day'];
  344. var id = 'd' + year + month + day;
  345. scrollToElement(id);
  346. // location.href = '#d' + year + month + day;
  347. // setDate(year, month, day);
  348. }
  349. function goMonth(year, month)
  350. {
  351. month = maybeZeroPad(month.toString());
  352. var id = $j('h1[id^="d' + year + month + '"]').attr('id')
  353. scrollToElement(id);
  354. // location.href = '#ym' + year + month;
  355. // setDate(year, parseInt(month), 1);
  356. }
  357. function maybeZeroPad(str)
  358. {
  359. if ( str.length == 1 ) str = '0' + str;
  360. return str;
  361. }
  362. function remove(array, str)
  363. {
  364. for(var i=0; i<array.length; i++)
  365. {
  366. if ( array[i] == str || array[i].startsWith(str) )
  367. {
  368. array.splice(i, 1);
  369. break;
  370. }
  371. }
  372. }
  373. Date.prototype.addDays = function(days) {
  374. this.setDate(this.getDate()+days);
  375. }
  376. String.prototype.replaceAt=function(index, char) {
  377. return this.substr(0, index) + char + this.substr(index+char.length);
  378. }
  379. String.prototype.startsWith = function (str){
  380. return this.indexOf(str) == 0;
  381. };
  382. String.prototype.contains = function (str){
  383. return this.indexOf(str) != -1;
  384. };
  385. String.prototype.endsWith = function (str){
  386. return this.indexOf(str) == this.length - str.length - 1;
  387. };
  388. if(!String.prototype.trim) {
  389. String.prototype.trim = function () {
  390. return this.replace(/^\s+|\s+$/g,'');
  391. };
  392. }
  393. function case_insensitive_sort(a, b)
  394. {
  395. var x = a.toLowerCase();
  396. var y = b.toLowerCase();
  397. return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  398. }
  399. function make_path(view)
  400. {
  401. var path;
  402. var elmcity_id = get_elmcity_id();
  403. if ( view == undefined )
  404. {
  405. var selected = $j('#tag_select option:selected').val();
  406. view = selected.replace(/\s*\((\d+)\)/,'');
  407. path = make_view_path_from_picklist(view, elmcity_id);
  408. }
  409. else
  410. {
  411. path = make_view_path_from_view(view, elmcity_id);
  412. }
  413. if ( gup('tags') != '')
  414. path = add_href_arg(path,'tags',gup('tags') );
  415. if ( gup('test') != '')
  416. path = add_href_arg(path,'test',gup('test') );
  417. if ( gup('theme') != '')
  418. path = add_href_arg(path,'theme',gup('theme') );
  419. if ( gup('count') != '')
  420. path = add_href_arg(path,'count',gup('count') );
  421. if ( gup('hubtitle') != '')
  422. path = add_href_arg(path,'hubtitle',gup('hubtitle') );
  423. if ( gup('eventsonly') != '')
  424. path = add_href_arg(path,'eventsonly',gup('eventsonly') );
  425. if ( gup('template') != '')
  426. path = add_href_arg(path,'template',gup('template') );
  427. if ( gup('jsurl') != '')
  428. path = add_href_arg(path,'jsurl',gup('jsurl') );
  429. if ( gup('hub') != '')
  430. path = add_href_arg(path,'hub', get_selected_hub() );
  431. try
  432. {
  433. var days_cookie_name = make_cookie_name_from_view(view);
  434. var days_cookie_value = $j.cookie(days_cookie_name);
  435. if ( typeof(days_cookie_value)!='undefined' )
  436. {
  437. var days = days_cookie_value;
  438. path = add_href_arg( path, 'days', days );
  439. }
  440. }
  441. catch (e)
  442. {
  443. console.log(e.message);
  444. }
  445. return path;
  446. }
  447. function show_view(view)
  448. {
  449. var elmcity_id = get_elmcity_id();
  450. var path = make_path(view);
  451. location.href = path;
  452. }
  453. function make_view_path_from_view(view, elmcity_id)
  454. {
  455. var path;
  456. if ( redirected_hubs.indexOf(elmcity_id) == -1 )
  457. path = '/' + elmcity_id + '/?view=' + encodeURIComponent(view);
  458. else
  459. path = '/html?view=' + encodeURIComponent(view);
  460. return path;
  461. }
  462. function make_view_path_from_picklist(view, elmcity_id)
  463. {
  464. var path;
  465. if ( redirected_hubs.indexOf(elmcity_id) == -1 )
  466. {
  467. if ( view == 'all' )
  468. path = '/' + elmcity_id + '/';
  469. else
  470. path = '/' + elmcity_id + '/?view=' + encodeURIComponent(view);
  471. }
  472. else
  473. {
  474. if ( view == 'all' )
  475. path = '/html' + '/';
  476. else
  477. path = '/html?view=' + encodeURIComponent(view);
  478. }
  479. return path;
  480. }
  481. function remove_href_arg(href, name)
  482. {
  483. var pat = eval('/[\?&]*' + name + '=[^&]*/');
  484. href = href.replace(pat,'');
  485. if ( (! href.contains('?')) && href.contains('&') )
  486. href = href.replaceAt(href.indexOf('&'),'?');
  487. return href;
  488. }
  489. function add_href_arg(href, name, value)
  490. {
  491. href = remove_href_arg(href,name);
  492. if ( href.contains('?') )
  493. href = href + '&' + name + '=' + value;
  494. else
  495. {
  496. href = href + '?' + name + '=' + value;
  497. }
  498. return href;
  499. }
  500. function dismiss_menu(id)
  501. {
  502. var elt = $j('#' + id);
  503. elt.find('.menu').remove();
  504. }
  505. function get_add_to_cal_url(id,flavor)
  506. {
  507. var elt = $j('#' + id);
  508. var start = elt.find('.st').attr('content');
  509. var end = ''; // for now
  510. var url = elt.find('.ttl').find('a').attr('href');
  511. var summary = get_summary(id);
  512. var description = elt.find('.src').text();
  513. var location = ''; // for now
  514. var elmcity_id = get_elmcity_id();
  515. var service_url = host + 'add_to_cal?elmcity_id=' + elmcity_id +
  516. '&flavor=' + flavor +
  517. '&start=' + encodeURIComponent(start) +
  518. '&end=' + end +
  519. '&summary=' + encodeURIComponent(summary) +
  520. '&url=' + encodeURIComponent(url) +
  521. '&description=' + encodeURIComponent(description) +
  522. '&location=' + location;
  523. return service_url;
  524. }
  525. function add_to_google(id)
  526. {
  527. try
  528. {
  529. var service_url = get_add_to_cal_url(id, 'google');
  530. $j('.menu').remove();
  531. // console.log('redirecting to ' + service_url);
  532. // location.href = service_url;
  533. window.open(service_url, "add to google");
  534. }
  535. catch (e)
  536. {
  537. console.log(e.message);
  538. }
  539. }
  540. function add_to_hotmail(id)
  541. {
  542. var service_url = get_add_to_cal_url(id, 'hotmail');
  543. $j('.menu').remove();
  544. location.href = service_url;
  545. }
  546. function add_to_ical(id)
  547. {
  548. var service_url = get_add_to_cal_url(id, 'ical');
  549. $j('.menu').remove();
  550. location.href = service_url;
  551. }
  552. function add_to_facebook(id)
  553. {
  554. var service_url = get_add_to_cal_url(id, 'facebook');
  555. $j('.menu').remove();
  556. location.href = service_url;
  557. }
  558. function add_to_cal(id)
  559. {
  560. elt = $j('#' + id);
  561. quoted_id = '\'' + id + '\'';
  562. elt.find('.menu').remove();
  563. elt.append(
  564. '<ul class="menu">' +
  565. '<li><a title="add this event to your Google calendar" href="javascript:add_to_google(' + quoted_id + ')">add to Google Calendar</a></li>' +
  566. '<li><a title="add this event to your Hotmail calendar" href="javascript:add_to_hotmail(' + quoted_id + ')">add to Hotmail Calendar</a></li>' +
  567. '<li><a title="add to your Outlook, Apple iCal, or other iCalendar-aware desktop calendar" href="javascript:add_to_ical(' + quoted_id + ')">add to iCal</a></li>' +
  568. '<li><a title="add to Facebook (remind yourself and invite friends with 1 click!)" href="javascript:add_to_facebook(' + quoted_id + ')">add to Facebook</a></li>' +
  569. '<li><a title="dismiss this menu" href="javascript:dismiss_menu(' + quoted_id + ')">cancel</a></li>' +
  570. '</ul>'
  571. );
  572. }
  573. var current_id;
  574. var current_source;
  575. function active_description(description) {
  576. var template = '<div id="__ID___desc" style="overflow:hidden;text-indent:0;border-style:solid;border-width:thin;padding:8px;margin:8px">__CLOSER__ __IMAGES__ <div style="clear:both"><hr width="100%"><span class="desc">__LOCATION_AND_DESCRIPTION__</span>__MAP__<div>__UPCOMING__</div>__SOURCE__</div></div>';
  577. if ( $j('#' + current_id + '_desc').length > 0 )
  578. return;
  579. template = template.replace('__ID__', current_id);
  580. var orig_length = description.length;
  581. description = description.replace(/<br>\s+/g, '<br>')
  582. description = description.replace(/(<br>)\1+/g, '<br><br>')
  583. template = template.replace('__LOCATION_AND_DESCRIPTION__',description);
  584. quoted_id = '\'' + current_id + '\'';
  585. var cat_images = "";
  586. var source_image = "";
  587. var all_images = new Array();
  588. // build html for source image
  589. try {
  590. var img_url = source_images[current_source];
  591. if ( typeof (img_url) != 'undefined' && img_url.contains('NoCurrentImage') == false )
  592. source_image += '<a title="source: ' + current_source + '"><img alt="' + current_source + '" style="float:left;margin:8px;width:100px" src="' + img_url + '"></a>'
  593. }
  594. catch (e) {
  595. console.log(e.message);
  596. }
  597. // build html for catgory images
  598. try {
  599. var cats = $j('#' + current_id + ' .cat a').slice(0,2);
  600. for ( i = 0; i < cats.length; i++ )
  601. {
  602. var cat = cats[i].firstChild.textContent;
  603. if ( cat == 'facebook' )
  604. continue;
  605. var img_url = category_images[cat];
  606. if ( typeof (img_url) != 'undefined' && img_url.contains('NoCurrentImage') == false )
  607. {
  608. var href = location.href;
  609. href = add_href_arg(href, 'view', cat);
  610. href = remove_href_arg(href, 'show_desc');
  611. var message;
  612. var href_html;
  613. if ( gup('view') != cat )
  614. {
  615. message = 'switch to the ' + cat + ' category';
  616. href_html = 'href="' + href + '" ';
  617. }
  618. else
  619. {
  620. message = 'current category: ' + cat;
  621. href_html = ' ';
  622. }
  623. cat_images += '<a title="' + message + '"' + href_html + '><img alt="' + cat + '" style="float:left;margin:8px;width:100px;border-style:solid;border-width:thin;border-color:slategray" src="' + img_url + '"></a>';
  624. }
  625. }
  626. }
  627. catch (e) {
  628. console.log(e.message);
  629. }
  630. if ( show_images && ( source_image != '' || category_images != '' ) ) {
  631. template = template.replace('__IMAGES__', source_image + cat_images);
  632. }
  633. //s.match( /(\d+-\d+-)(\d+)(T\d+:\d+)/ )
  634. //["2013-10-07T19:00", "2013-10-", "07", "T19:00"]
  635. try {
  636. var lat = $j('#' + current_id + ' span[property="v:latitude"]').attr('content');
  637. var lon = $j('#' + current_id + ' span[property="v:longitude"]').attr('content');
  638. if ( typeof (lat) != 'undefined' && typeof(lon) != 'undefined' ) {
  639. var date = get_md(current_id) + ' ' + get_st2(current_id);
  640. var title = get_summary(current_id);
  641. var map_url = 'http://elmcity.cloudapp.net/get_blob?id=admin&path=map_detail.html?lat=' + lat + '&lon=' + lon + '&title=' + encodeURIComponent(title) + '&date=' + encodeURIComponent(date);
  642. var map_display = 'none';
  643. if ( ! hide_maps )
  644. map_display = "block";
  645. var iframe = '<iframe style="display:__DISPLAY__;margin-top:20px" src="' + map_url + '" width="100%" height="400" scrolling="no" seamless="seamless"></iframe>';
  646. iframe = iframe.replace('__DISPLAY__', map_display);
  647. var map_opener = '<p class="elmcity_info_para"><b>Map:</b></p><p class="elmcity_info_para" id="' + current_id + '_map_opener' + '"><a href="javascript:reveal_map(current_id)"><img style="border-style:solid;border-width:thin" title="click to enlarge map" src="' + blobhost + 'admin/map_icon.jpg' + '"></a></p>';
  648. if ( ! hide_maps )
  649. map_opener = '';
  650. template = template.replace('__MAP__', map_opener + iframe);
  651. }
  652. else
  653. template = template.replace('__MAP__', '');
  654. }
  655. catch (e) {
  656. console.log(e.message);
  657. }
  658. // build the closer
  659. var x = '<span style="font-size:larger;float:right;"><a title="hide description" href="javascript:hide_desc(' + quoted_id + ')"> [X] </a> </span>';
  660. template = template.replace('__CLOSER__', x);
  661. // acquire upcoming events from source
  662. var elmcity_id = get_elmcity_id();
  663. var from_dt = get_dtstart(current_id);
  664. var to_dt = '3000-01-01T00:00'; // just a date far in future, the count arg will trim the results
  665. if ( $j('#' + current_id + ' .src').text() != '' ) { // skip if coalesced
  666. template = template.replace('__UPCOMING__', '<p style="display:none" id="' + current_id + '_upcoming"></p>');
  667. var redirected_host = get_redirected_host();
  668. var url= redirected_host + '/json?source=' + current_source + '&from=' + from_dt + '&to=' + to_dt + '&count=4';
  669. try {
  670. $j.ajax({
  671. url: url,
  672. cache: false,
  673. complete: function(xhr, status) {
  674. try {
  675. var upcoming = JSON.parse(xhr.responseText);
  676. if ( $j.keys(upcoming).length > 0 ) {
  677. var fn = 'show_upcoming_html("' + current_id + '",' + xhr.responseText + ')';
  678. window.setTimeout(fn, 100);
  679. }
  680. }
  681. catch (e) {
  682. console.log('cannot process upcoming events' + e.message);
  683. }
  684. }
  685. });
  686. }
  687. catch (e) {
  688. console.log('cannot get upcoming events' + e.message);
  689. }
  690. }
  691. else
  692. template = template.replace('__UPCOMING__','');
  693. // build link to source calendar
  694. var url = $j('#' + current_id + ' span[rel="v:url"]').attr('href');
  695. var src = $j('#' + current_id + ' span[property="v:description"]').text()
  696. var link = '<p style="font-size:larger"><a target="origin" title="visit the source calendar in a new window or tab" href="' + url + '">visit the source calendar</a></p>';
  697. template = template.replace('__SOURCE__',link);
  698. elt = $j('#' + current_id);
  699. elt.append(template);
  700. }
  701. function get_redirected_host() {
  702. var elmcity_id = get_elmcity_id();
  703. var redirected_host = host;
  704. if ( redirected_hubs.indexOf(elmcity_id) != -1 )
  705. redirected_host = 'http://' + redirected_hubs_dict[elmcity_id] + '/';
  706. else
  707. redirected_host = host + elmcity_id;
  708. return redirected_host;
  709. }
  710. function reveal_map(id) {
  711. $j('#' + id + ' iframe').css('display','block');
  712. $j('#' + id + '_map_opener').remove();
  713. }
  714. function show_upcoming_html(id, obj) {
  715. if ( $j.keys(obj).length == 1 ) // only the current event
  716. return;
  717. obj = obj.splice(1); // the query matches the current event so exclude it
  718. var upcoming = $j('#' + id + '_upcoming');
  719. // var upcoming_html = '<p class="elmcity_info_para"><b>Next on the </b>' + '<span class="src">' + current_source + '</span>' + ' <b>calendar</b>:</b></p>';
  720. var upcoming_html = '<p class="elmcity_info_para"><b>Next on the <u>' + current_source + '</u>' + ' calendar:</b></p>';
  721. upcoming_html += '<div style="margin-left:5%">';
  722. for ( i in obj ) {
  723. var dtstart = new Date(obj[i]['dtstart']).toLocaleString();
  724. upcoming_html += '<p class="elmcity_info_para">' + '<i>' + obj[i]['title'] + '</i>' + ', ' + '<b>' + dtstart + '</b>';
  725. var upcoming_location = obj[i]['location'];
  726. if ( upcoming_location != '' ) {
  727. upcoming_html += ', ' + upcoming_location;
  728. }
  729. upcoming_html += '</p>';
  730. }
  731. upcoming_html += '</div>';
  732. upcoming.html(upcoming_html);
  733. upcoming.css('display','block');
  734. }
  735. function hide_desc(id)
  736. {
  737. quoted_id = '\'' + id + '\'';
  738. $j('#' + id + '_desc').remove();
  739. $j('#' + id + ' .sd').css('display','inline');
  740. $j('#' + id + ' .atc').css('display','inline');
  741. }
  742. function show_more(id)
  743. {
  744. $j('div.' + id).show();
  745. $j('span.' + id).remove();
  746. }
  747. function show_desc(id)
  748. {
  749. quoted_id = '\'' + id + '\'';
  750. $j('#' + id + ' .sd').css('display','none');
  751. $j('#' + id + ' .atc').css('display','none');
  752. var uid = get_uid(id);
  753. var elmcity_id = get_elmcity_id();
  754. var url = host + elmcity_id + '/description_from_uid?uid=' + uid + '&jsonp=active_description';
  755. current_id = id;
  756. current_source = get_source(id);
  757. $j.getScript(url);
  758. if ( ! gup('open_at_top').startsWith('n') )
  759. scrollToElement(id);
  760. }
  761. function find_id_of_last_event()
  762. {
  763. var events = $j('.bl');
  764. var last = events[events.length-1];
  765. return last.attributes['id'].value;
  766. }
  767. function get_summary(id)
  768. {
  769. var elt = $j('#' + id);
  770. var summary = $j('#' + id + ' .ttl span').text();
  771. if ( summary == '')
  772. summary = $j('#' + id + ' .ttl a').text();
  773. return summary;
  774. }
  775. function get_uid(id)
  776. {
  777. return $j('#' + id + ' .uid').text();
  778. }
  779. function get_dtstart(id)
  780. {
  781. return $j('#' + id + ' .st').attr('content');
  782. }
  783. function get_md(id)
  784. {
  785. return $j('#' + id + ' .md').text();
  786. }
  787. function get_st(id)
  788. {
  789. return $j('#' + id + ' .st').attr('content');
  790. }
  791. function get_st2(id)
  792. {
  793. return $j('#' + id + ' .st').text();
  794. }
  795. function get_source(id)
  796. {
  797. return $j('#' + id + ' .src').text();
  798. }
  799. $j.extend({
  800. keys: function(obj){
  801. var a = [];
  802. $j.each(obj, function(k){ a.push(k) });
  803. return a;
  804. }
  805. });
  806. function remember_or_forget_days()
  807. {
  808. var view = gup('view');
  809. var days = gup('days');
  810. if ( days != '' )
  811. remember_days(view, days);
  812. else
  813. forget_days(view);
  814. }
  815. function remember_days(view, days)
  816. {
  817. try
  818. {
  819. var cookie_name = make_cookie_name_from_view(view);
  820. $j.cookie(cookie_name, days);
  821. }
  822. catch (e)
  823. {
  824. console.log(e.message);
  825. }
  826. }
  827. function forget_days(view)
  828. {
  829. try
  830. {
  831. var cookie_name = make_cookie_name_from_view(view);
  832. $j.removeCookie(cookie_name);
  833. }
  834. catch (e)
  835. {
  836. console.log(e.message);
  837. }
  838. }
  839. function make_cookie_name_from_view(view)
  840. {
  841. if ( view == 'all' )
  842. view = '';
  843. view = view.replace(',' , '_');
  844. view = view.replace('-' , '_minus_');
  845. var cookie_name = 'elmcity_' + view + '_days';
  846. return cookie_name;
  847. }
  848. function load_category_images(id)
  849. {
  850. if ( $j.keys(category_images).length > 0 )
  851. return;
  852. $j.ajax({
  853. url: 'http://elmcity.cloudapp.net/get_blob?id=' + id + '&path=category_images.json',
  854. cache: false,
  855. complete: function(xhr, status) {
  856. try {
  857. category_images = JSON.parse(xhr.responseText);
  858. }
  859. catch (e) {
  860. console.log('no category images' + e.message);
  861. }
  862. }
  863. });
  864. }
  865. function load_source_images(id)
  866. {
  867. if ( $j.keys(source_images).length > 0 )
  868. return;
  869. $j.ajax({
  870. url: 'http://elmcity.cloudapp.net/get_blob?id=' + id + '&path=source_images.json',
  871. cache: false,
  872. complete: function(xhr, status) {
  873. try {
  874. source_images = JSON.parse(xhr.responseText);
  875. }
  876. catch (e) {
  877. console.log('no source images' + e.message);
  878. }
  879. }
  880. });
  881. }
  882. function show_category_image_under_picker() {
  883. if ( $j.keys(category_images).length == 0 )
  884. return;
  885. if ( ! show_images )
  886. return;
  887. var view = gup('view');
  888. if ( typeof(category_images[view]) != 'undefined' && category_images[view] != blobhost + 'admin/NoCurrentImage.jpg' )
  889. {
  890. var href = location.href;
  891. $j('#category_image')[0].innerHTML = '<img style="width:140px;border-width:thin;border-style:solid" src="' + category_images[view] + '">';
  892. }
  893. else
  894. $j('#category_image')[0].innerHTML = '';
  895. }