/*!
* bootstrap-calendar plugin
* Original author: @ahmontero
* Licensed under the MIT license
*
* jQuery lightweight plugin boilerplate
* Original author: @ajpiano
* Further changes, comments: @addyosmani
* Licensed under the MIT license
*/
// the semi-colon before the function invocation is a safety
// net against concatenated scripts and/or other plugins
// that are not closed properly.
// ;
(function ($, window, document, undefined) {
(function () {
var cache = {};
this.tmpl = function tmpl(str, data) {
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ?
cache[str] = cache[str] ||
tmpl(document.getElementById(str).innerHTML) :
// Generate a reusable function that will serve as a template
// generator (and which will be cached).
new Function("obj",
"var p=[],print=function(){p.push.apply(p,arguments);};" +
// Introduce the data as local variables using with(){}
"with(obj){p.push('" +
// Convert the template into pure JavaScript
str
.replace(/[\r\t\n]/g, " ")
.split("<%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%>").join("p.push('")
.split("\r").join("\\'") + "');}return p.join('');");
// Provide some basic currying to the user
return data ? fn(data) : fn;
};
})();
String.prototype.suffix = function (len, end) {
if (this.length > len) {
this.substr(0, len);
return this + end;
} else {
return this;
}
}
var pluginName = 'LiveScheduler',
defaults = {
weekStart: 1,
msg_days: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
msg_months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
msg_today: 'Today',
msg_events_today: 'Events Today', // when today click.
url: "",
currentday: null,
events: null
},
// .aweek.btn-group>operator+date_id
timeline_template = tmpl(
'
' +
'<% if(seven_day_events.length ==0){%>' +
'
' +
'课程名称 | 学院 | 授课时间 | 授课地点 | 状态 | 操作 |
' +
'当天没有直播信息! | | | | |
' +
'<% }%>' +
'<% for (var ix = 0, lengthx = seven_day_events.length; ix < lengthx; ix ++) { %>' +
'
' + //end if
'<% } %>',// end for
daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
today = new Date();
// The actual plugin constructor
function Plugin(element, options) {
this.element = $(element);
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype.init = function () {
// Place initialization logic here
// You already have access to the DOM element and
// the options via the instance, e.g. this.element
// and this.options
var now = moment();
this.weekStart = this.options.weekStart || 1;
this.othersParams = this.options.otherParams || '';
this.url = this.options.url;
this.paramsData = getParameters(this.options.otherParams);
this.currentday = this.options.currentday ? this.options.currentday : now;
this.renderCalendar(getSevenDays(this.currentday));
};
Plugin.prototype.renderCalendar = function (daylist) {
$(this.element).contents().remove();
var that = this;
var elt = tmpl(events_list_template);
this.calendar = $(days_template({
seven_day: daylist
})).appendTo(this.element);
this.timeline = $(timeline_template({})).appendTo(this.element).on({
click: $.proxy(this.click, this)
});
this.calendar.on({ click: $.proxy(this.click, this) });
this.paramsData.days = _.map(daylist, function (k) {
return moment(k).utc().format();
});
$.ajax({
type: "POST",
url: this.url,
data: this.paramsData,
// crossDomain:true,
dataType: "json",
jsonp: false
}).done(function (results) {
var res_dates = results;
if (res_dates.length > 0) {
var req_days = _.map(daylist, function (k) {
return moment(k).utc().format();
});
that.events = _.map(req_days, function (req_date) {
return _.filter(res_dates, function (res) {
res.date = moment(res.date);
return res.date.date() == moment(req_date).date();
});
});
var v = { seven_day_events: that.events, r_days: daylist };
$(elt(v)).appendTo(that.element);
} else {
var v = { seven_day_events: [], r_days: [] };
$(elt(v)).appendTo(that.element);
}
$("#days_tabs .select_by_" + moment(this.currentday).format("D") + " a").tab("show");
$($("#days_tabs .select_by_" + moment(this.currentday).format("D") + " a").attr('href')).children('.live_table').dataTable({
"bRetrieve": true,
"sDom": "<'row'<'span4'l><'span6 pull-right'f>r>t<'row'<'span4'i><'span6 pull-right'p>>",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ 条记录 每一页"
}
});
}).fail(function (jqXHR, textStatus, errorThrown) {
});
};
Plugin.prototype.click = function (e) {
e.stopPropagation();
e.preventDefault();
var target = $(e.target).closest('li');
if (target.length == 1) {
if (target.is('.pre')) {
if (target.is('.fast')) {
this.currentday = moment(this.currentday).subtract('d', 7);
this.renderCalendar(getSevenDays(this.currentday));
} else {
this.currentday = moment(this.currentday).subtract('d', 1);
this.renderCalendar(getSevenDays(this.currentday));
}
} else if (target.is('.next')) {
if (target.is('.fast')) {
this.currentday = moment(this.currentday).add('d', 7);
this.renderCalendar(getSevenDays(this.currentday));
} else {
this.currentday = moment(this.currentday).add('d', 1);
this.renderCalendar(getSevenDays(this.currentday));
}
} else if (target.is('.select_day')) {
$(target).children('a').tab("show");
$($(target).children('a').attr("href")).children('.live_table').dataTable({
"bRetrieve": true,
"sDom": "<'row'<'span4'l><'span6 pull-right'f>r>t<'row'<'span4'i><'span6 pull-right'p>>",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ 条记录 每一页"
}
});
}
}
};
var getParameters = function (url) {
var params = url.split("&"), i, val, resultes = new Object;
for (i = 0; i < params.length; i++) {
val = params[i].split("=");
resultes[val[0]] = escape(val[1]);
}
return resultes;
};
var getSevenDays = function (middleDay) {
var daylist = new Array();
var startDay = moment(middleDay).subtract('d', 4);
_(7).times(function (n) {
var ss = startDay.add('d', 1);//
daylist.push(moment(ss));
});
return daylist;
};
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin(this, options));
}
});
}
})(jQuery, window, document);