/*!
* 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) {
/**
* 微型模板引擎 tmpl 0.2
*
* 0.2 更新:
* 1. 修复转义字符与id判断的BUG
* 2. 放弃低效的 with 语句从而最高提升3.5倍的执行效率
* 3. 使用随机内部变量防止与模板变量产生冲突
*
* @author John Resig, Tang Bin
* @see http://ejohn.org/blog/javascript-micro-templating/
* @name tmpl
* @param {String} 模板内容或者装有模板内容的元素ID
* @param {Object} 附加的数据
* @return {String} 解析好的模板
*
* @example
* 方式一:在页面嵌入模板
*
* tmpl('tmpl-demo', {name: 'demo data', list: [202, 96, 133, 134]})
*
* 方式二:直接传入模板:
* var demoTmpl =
* '
' +
'<% for (var ix = 0, lengthx = seven_day_events.length; ix < lengthx; ix ++) { %>' +
'
' +
'<% if (seven_day_events[ix].length>0){ %>' +
'<% for (var jx = 0 ,sde=seven_day_events[ix][0], lengthy = seven_day_events[ix][0].LiveInfo.length; jx < lengthy; jx ++) { %>' +
'
' +
'- ' +
'<%= sde.LiveInfo[jx].title.substring(0,10) %>' +
'
- <%= sde.LiveInfo[jx].time%>
' +
'- <%= sde.LiveInfo[jx].classroom%>
' +
'- <%= sde.LiveInfo[jx].course %>
' +
'' +
' <% if( sde.LiveInfo[jx].status !=null ){ %>'+
' - ' +
' <%= sde.LiveInfo[jx].status %>' +
'
' +
'<% } %>' +
'
' +
'<% } %>' +
'<% } %>' +
'
' +
'<% } %>',
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);
// jQuery has an extend method that merges the
// contents of two or more objects, storing the
// result in the first object. The first object
// is generally empty because we don't want to alter
// the default options for future instances of the plugin
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
this.weekStart = this.options.weekStart || 1;
this.days = this.options.msg_days;
this.months = this.options.msg_months;
this.msg_today = this.options.msg_today;
this.msg_events_hdr = this.options.msg_events_header;
this.events = this.options.events;
this.url = this.options.url;
this.live_date = new Date();
var now = moment();
this.mm = now.month();
this.yy = now.year();
this.today = now.date();
this.middleDay = now;
this.renderCalendar(getSevenDays(now));
};
Plugin.prototype.renderCalendar = function (daylist) {
if (this.calendar) {
$(".timeline").remove();
$("#accordion_event_table").remove();
}
var that = this;
var elt = tmpl(events_list_template);
this.calendar = $(timeline_template({
seven_day: daylist
}))
.appendTo(this.element).on({
click: $.proxy(this.click, this)
});
$.ajax({
type: "POST",
url: this.url,
data: {
"days": _.map(daylist, function (k) {
return moment(k).utc().format();
})
},
// crossDomain:true,
dataType: "json",
jsonp: false
}).done(function (da) {
if (da.length > 0) {
var days = _.map(daylist, function (k) {
return moment(k).utc().format();
});
that.events = _.map(days, function (date) {
return _.filter(da, function (re) {
return moment(re.date).date() == moment(date).date();
}
);
});
var v = {
seven_day_events: that.events
}
// console.log(v);
$(elt(v)).appendTo(that.element)
}
}).fail(function (jqXHR, textStatus, errorThrown) {
// console.log(textStatus);
});
};
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.middleDay = moment(this.middleDay).subtract('d', 7);
this.renderCalendar(getSevenDays(this.middleDay));
} else {
this.middleDay = moment(this.middleDay).subtract('d', 1);
this.renderCalendar(getSevenDays(this.middleDay));
}
} else if (target.is('.next')) {
if (target.is('.fast')) {
this.middleDay = moment(this.middleDay).add('d', 7);
this.renderCalendar(getSevenDays(this.middleDay));
} else {
this.middleDay = moment(this.middleDay).add('d', 1);
this.renderCalendar(getSevenDays(this.middleDay));
}
}
}
};
var getSevenDays = function (middleDay) {
var daylist = new Array();
var startDay = moment(middleDay).subtract('d', Math.floor(7 / 2));
_(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);