var inkwell_timelines = { config : { data_loading : false, no_more_data : false, current_timestamp : null }, formatDate : function (dateTime) { var date = dateTime.getDate(); var month = dateTime.getMonth() + 1; var hour = dateTime.getHours(); var mins = dateTime.getMinutes(); return (date < 10 ? ('0' + date) : date) + '.' + (month < 10 ? ('0' + month) : month) + '.' + dateTime.getFullYear() + ' ' + (hour < 10 ? ('0' + hour) : hour) + ':' + (mins < 10 ? ('0' + mins) : mins); }, activeTimelineName : function () { return inkwell_timelines.timelineName($('.inkwell_timelines .tab-menu .active')); }, timelineName: function (timeline_obj) { var timeline_name = null; var classes = timeline_obj.attr('class').split(/\s+/); $(classes).each(function () { if (this.indexOf('timeline_') == 0) timeline_name = this.replace('timeline_', ''); }); return timeline_name; }, timelineBlockName : function () { return $('.inkwell_timelines').attr('id'); }, lastItemId : function () { var id = $('.inkwell_timelines .wall_item').last().attr('id'); if (id) { var splitted_id = id.split('_'); return splitted_id[splitted_id.length - 1] } return null; }, transferredParams : function (timeline) { return $('.inkwell_timelines input.transferred_params.' + timeline).attr('value'); }, selectorsValues : function () { var selectors = $('.inkwell_timelines .inkwell_multi_selector'); var result = []; selectors.each(function () { if (!inkwell_multi_selector.is_all_checked($('.inkwell_timelines .inkwell_multi_selector'))) { var data = {}; data['name'] = inkwell_multi_selector.get_selector_name($(this)); data['data'] = inkwell_multi_selector.top_checked_checkboxes_ids($(this)); result.push(data); } }); return (result.length > 0 ? result : null); }, getData : function (current_timeline, include_selectors) { var timestamp = $.now(); inkwell_timelines.config.current_timestamp = timestamp; $.ajax({ url: '<%= InkwellTimelines::Engine::config.autoload_path%>', type: 'POST', dataType: 'html', data: JSON.stringify({ block_name: inkwell_timelines.timelineBlockName(), timeline: current_timeline, last_item_id: inkwell_timelines.lastItemId(), transferred_params: inkwell_timelines.transferredParams(current_timeline), selectors_values: inkwell_timelines.selectorsValues(), include_selectors: include_selectors }), success: function (data) { if (timestamp == inkwell_timelines.config.current_timestamp) inkwell_timelines.buildTimeline(data); }, complete: function () { inkwell_timelines.config.data_loading = false; } }); }, buildTimeline : function (data) { if (data == "") { inkwell_timelines.config.no_more_data = true; return; } $('.inkwell_timelines').append(data); inkwell_timelines.formDate(); }, formDate : function () { $('.inkwell_timelines .date:hidden').each(function () { $(this).text(inkwell_timelines.formatDate(new Date(parseInt($(this).text())))); $(this).removeAttr('style'); }); }, removeTimelineData: function (include_selectors) { inkwell_timelines.config.no_more_data = false; if (include_selectors) $('.inkwell_timelines .inkwell_multi_selector').remove(); $('.inkwell_timelines .wall_item').remove(); }, timelineClickHandler: function (timeline_obj) { inkwell_timelines.config.data_loading = true; inkwell_timelines.removeTimelineData(true); if (!timeline_obj.hasClass('active')) { $('.inkwell_timelines .tab-menu .active').removeClass('active'); timeline_obj.addClass('active'); } inkwell_timelines.getData(inkwell_timelines.timelineName(timeline_obj), true); }, selectorChangedHandler: function (selector) { inkwell_timelines.config.data_loading = true; inkwell_timelines.removeTimelineData(false); var timeline_obj = $('.inkwell_timelines .tab-menu .active')[0]; inkwell_timelines.getData(inkwell_timelines.timelineName($(timeline_obj)), false); } }; $(document).ready(function () { inkwell_timelines.formDate(); $(window).on('scroll', function(e) { inkwell_multi_selector.hide_popup(); var scrHP = $(".inkwell_timelines").height() + $('.inkwell_timelines').offset().top; var scrH2 = $(this).height() + $(this).scrollTop(); if (scrHP - scrH2 < <%= InkwellTimelines::Engine::config.load_distance%>) { if (!(inkwell_timelines.config.data_loading || inkwell_timelines.config.no_more_data)) { inkwell_timelines.config.data_loading = true; inkwell_timelines.getData(inkwell_timelines.activeTimelineName(), false); } } }); $('.inkwell_timelines .tab-menu').on('click', 'span', function () { inkwell_timelines.timelineClickHandler($(this)); }); $(document).on('state_changed', '.inkwell_multi_selector', function () { inkwell_timelines.selectorChangedHandler($(this)); }); });