BusyBird
view release on metacpan or search on metacpan
share/www/static/timeline.js view on Meta::CPAN
"use strict";
// Javascript library specific to timeline view
bb.StatusContainer = (function() { var selfclass = $.extend(function(args) {
// @params: args.selectorContainer, args.timeline, args.apiBase = ""
var self = this;
var now_toggling_extension = false;
if(!defined(args.selectorContainer)) {
throw "selectorContainer param is mandatory";
}
if(!defined(args.timeline)) {
throw "timeline param is mandatory";
}
self.sel_container = args.selectorContainer;
self.timeline = args.timeline;
self.api_base = defined(args.apiBase) ? args.apiBase : "";
self.threshold_level = 0;
self.$cursor = null;
self.on_threshold_level_changed_callbacks = [];
$(self.sel_container).on("click", ".bb-status", function() {
self.setCursor(this);
});
$(self.sel_container).on("click", ".bb-status-extension-toggler", function(event) {
if(!now_toggling_extension && $(event.target).closest("a").size() === 0) {
now_toggling_extension = true;
self.toggleExtensionPane(this).fin(function() {
now_toggling_extension = false;
});
}
});
}, {
ADD_STATUSES_BLOCK_SIZE: 100,
ANIMATE_STATUS_MAX_NUM: 15,
ANIMATE_STATUS_DURATION: 400,
LOAD_STATUS_TRY_MAX: 3,
LOAD_UNACKED_STATUSES_COUNT_PER_PAGE: 100,
LOAD_UNACKED_STATUSES_MAX_PAGE_NUM: 6,
ACK_TRY_MAX: 3,
LOAD_MORE_STATUSES_COUNT: 20,
_getStatusID: function($status) {
return $status.find(".bb-status-id").text();
},
_formatHiddenStatusesHeader : function (invisible_num) {
var plural = invisible_num > 1 ? "es" : "";
return '<li class="bb-hidden-statuses-header"><span class="bb-hidden-statuses-num">'+ invisible_num +'</span> status'+plural+' hidden here.</li>';
},
_updateHiddenStatusesHeaders: function($statuses, hidden_header_list, window_adjuster) {
if(!defined(window_adjuster)) window_adjuster = function() {};
$statuses.filter(".bb-hidden-statuses-header").remove();
window_adjuster();
return bb.blockEach(hidden_header_list, 40, function(header_block) {
$.each(header_block, function(i, header_entry) {
if(defined(header_entry.$followed_by)) {
header_entry.$followed_by.before(selfclass._formatHiddenStatusesHeader(header_entry.entries.length));
}else {
$statuses.filter('.bb-status').last().after(selfclass._formatHiddenStatusesHeader(header_entry.entries.length));
}
});
window_adjuster();
});
},
_createWindowAdjuster: function(anchor_position_func) {
if(!anchor_position_func) {
return function() {};
}
var relative_position_of_anchor;
relative_position_of_anchor = anchor_position_func() - $(window).scrollTop();
return function() {
$(window).scrollTop(anchor_position_func() - relative_position_of_anchor);
};
},
_scanStatusesForDisplayActions: function($statuses, threshold_level, enable_animation, cursor_index) {
var ACTION_STAY_VISIBLE = 0;
var ACTION_STAY_INVISIBLE = 1;
var ACTION_BECOME_VISIBLE = 2;
var ACTION_BECOME_INVISIBLE = 3;
var final_result = { // ** return this struct from the promise
hiddenHeaderList: [],
share/www/static/timeline.js view on Meta::CPAN
}).then(function() {
return self._setDisplayImmediately($(added_statuses_dom));
});
},
_isValidForCursor: function($elem) {
var self = this;
return ($elem.hasClass("bb-status") && $elem.data("bb-status-level") >= self.threshold_level);
},
_adjustCursor: function() {
var self = this;
var $statuses;
var $next_candidate, $prev_candidate;
if(!defined(self.$cursor)) {
$statuses = self._getStatuses();
if($statuses.size() === 0) return;
self.setCursor($statuses.get(0));
}
if(self._isValidForCursor(self.$cursor)) {
return;
}
$next_candidate = self.$cursor;
$prev_candidate = self.$cursor;
while(true) {
$next_candidate = $next_candidate.next();
$prev_candidate = $prev_candidate.prev();
if($next_candidate.size() === 0 && $prev_candidate.size() === 0) {
return;
}
if($next_candidate.size() === 1 && self._isValidForCursor($next_candidate.eq(0))) {
self.setCursor($next_candidate.get(0));
return;
}
if($prev_candidate.size() === 1 && self._isValidForCursor($prev_candidate.eq(0))) {
self.setCursor($prev_candidate.get(0));
return;
}
}
},
_getCursorIndex: function() {
var self = this;
if(!defined(self.$cursor)) return -1;
var $statuses = self._getStatuses();
if($statuses.size() === 0) return -1;
return $statuses.index(self.$cursor);
},
appendStatuses: function(added_statuses_dom) {
// @returns: promise resolved when done.
return this._addStatuses(added_statuses_dom, false);
},
prependStatuses: function(added_statuses_dom) {
// @returns: promise resolved when done.
return this._addStatuses(added_statuses_dom, true);
},
setThresholdLevel: function(new_threshold) {
// @returns: promise resolved when done.
var self = this;
var old_threshold = self.threshold_level;
self.threshold_level = parseInt(new_threshold, 10);
self._adjustCursor();
if(old_threshold !== new_threshold) {
$.each(self.on_threshold_level_changed_callbacks, function(i, callback) {
callback(new_threshold);
});
}
return selfclass.setDisplayByThreshold({
$statuses: $(self.sel_container).children(),
threshold: self.threshold_level,
enableAnimation: true,
enableWindowAdjust: true,
cursorIndex: self._getCursorIndex()
});
},
getThresholdLevel: function() {
return this.threshold_level;
},
getTimelineName: function() { return this.timeline },
getAPIBase: function() { return this.api_base },
loadUnackedStatuses: function() {
// @returns: promise with the following object
// { maxReached: (boolean), statuses: (array of status DOM elements loaded) }
var self = this;
var load_result;
var $acked_new_statuses_label = $(self.sel_container).find(".bb-status-new-label");
return selfclass.loadStatuses({
apiURL: self._getLoadStatusesURL(), countPerPage: selfclass.LOAD_UNACKED_STATUSES_COUNT_PER_PAGE,
maxPageNum: selfclass.LOAD_UNACKED_STATUSES_MAX_PAGE_NUM, ackState: "unacked"
}).then(function(result) {
load_result = result;
return self._ackStatuses(load_result.statuses, load_result.maxReached);
}).then(function() {
return self.prependStatuses(load_result.statuses);
}).then(function() {
if(!defined(self.$cursor)) {
self._adjustCursor();
}
$acked_new_statuses_label.remove();
return {maxReached: load_result.maxReached, statuses: load_result.statuses};
});
},
loadMoreStatuses: function() {
// @returns: promise resolved when done
var self = this;
var start_id = null;
return Q.fcall(function() {
var $statuses = self._getStatuses();
if($statuses.size() > 0) {
start_id = selfclass._getStatusID($statuses.last());
}
return selfclass.loadStatuses({
apiURL: self._getLoadStatusesURL(),
ackState: "acked", countPerPage: selfclass.LOAD_MORE_STATUSES_COUNT,
startMaxID: start_id, maxPageNum: 1
});
}).then(function(result) {
var added_statuses = result.statuses;
if(defined(start_id) && added_statuses.length > 0
&& selfclass._getStatusID($(added_statuses[0])) === start_id) {
added_statuses.shift();
}
return self.appendStatuses(result.statuses);
}).then(function() {
if(!defined(self.$cursor)) {
self._adjustCursor();
}
});
},
loadInit: function() {
// @returns: promise with the following object
// { maxReached: (boolean), statuses: (array of unacked status DOM elements loaded) }
var self = this;
var unacked_load_result;
return self.loadUnackedStatuses().then(function(result) {
unacked_load_result = result;
if(!result.maxReached) {
return self.loadMoreStatuses();
}
}).then(function() {
return unacked_load_result;
});
},
setCursor: function(cursor_dom) {
var self = this;
if(defined(self.$cursor)) {
self.$cursor.removeClass("bb-status-cursor");
}
self.$cursor = $(cursor_dom);
self.$cursor.addClass("bb-status-cursor");
},
listenOnThresholdLevelChanged: function(callback) {
// @params: callback (function(new_threshold) returning anything)
this.on_threshold_level_changed_callbacks.push(callback);
},
_getWindowAdjusterForExtensionPane: function($pane) {
// @return: a window adjuster function that keeps the closing
// extension pane near the center of the screen as mush as
// possible.
var self = this;
var pane_init_height = $pane.height();
if(pane_init_height <= 0) {
return null;
}
var screen_center = $(window).scrollTop() + $(window).height() * 0.4; // a little above the center, actually
var pos_ratio = (screen_center - $pane.offset().top) / pane_init_height;
if(pos_ratio <= 0) {
return null;
}
if(pos_ratio > 1) {
pos_ratio = 1;
}
return selfclass._createWindowAdjuster(function() {
return $pane.offset().top + pos_ratio * $pane.height();
});
},
toggleExtensionPane: function(extension_container_dom) {
// @returns: promise that resolves when it finishes toggling.
var self = this;
var $container = $(extension_container_dom);
var $pane = $container.find(".bb-status-extension-pane");
var $icon_expander = $container.find(".bb-status-extension-expander");
var $icon_collapser = $container.find(".bb-status-extension-collapser");
var $anchor = null;
var window_adjuster = null;
if($pane.size() === 0) {
return;
}
if($pane.css("display") === "none") {
$icon_expander.hide();
$icon_collapser.show();
}else {
window_adjuster = self._getWindowAdjusterForExtensionPane($pane);
$icon_expander.show();
$icon_collapser.hide();
}
return bb.slideToggleElements($pane, selfclass.ANIMATE_STATUS_DURATION, window_adjuster);
},
}; return selfclass;})();
////////////////////////////////////////////////////////
bb.TimelineUnackedCountsPoller = (function() {
var selfclass = $.extend(function(args) {
// @params: args.statusContainer
var scon = args.statusContainer;
var self = this;
if(!defined(scon)) {
throw "statusContainer param is mandatory";
}
this.status_container = scon;
this.on_change_listeners = [];
this.poller = new bb.EventPoller({
url: scon.getAPIBase() + "/timelines/" + scon.getTimelineName() + "/updates/unacked_counts.json",
( run in 3.057 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )