define(['jquery', 'underscore', 'bootstrap', 'jquery.wookmark', 'jquery.jplayer'], function($, _){
var material = {
'defaultoptions' : {
callback : null,
type : 'all',
multiple : false,
ignore : {
'wxcard' : true,
'image' : false,
'news' : false,
'video' : false,
'voice' : false
}
},
'init' : function(callback, options) {
var $this = this;
$this.options = $.extend({}, $this.defaultoptions, options);
$this.options.callback = callback;
$('#material-Modal').remove();
$(document.body).append($this.buildHtml().mainDialog);
$this.modalobj = $('#material-Modal');
$this.modalobj.find('.modal-header .nav li a').click(function(){
var type = $(this).data('type');
$this.localPage(type, 1);
$(this).tab('show')
return false;
});
if (!$(this).data('init')) {
if($this.options.type && $this.options.type != 'all') {
$this.modalobj.find('.modal-header .nav li.' + $this.options.type + ' a').trigger('click');
} else {
$this.modalobj.find('.modal-header .nav li.show:first a').trigger('click');
}
}
$this.modalobj.modal('show');
return $this.modalobj;
},
'localPage' : function(type, page) {
var $this = this;
var page = page || 1;
$('.checkMedia').removeClass('checkedMedia');
var $content = $this.modalobj.find('.material-content #' + type);
$content.html('
数据加载中
');
var url = './index.php?c=utility&a=material&do=list&type=' + type;
if(type == 'wxcard') {
url = './index.php?c=utility&a=coupon&do=wechat';
}
$.getJSON(url, {'page': page}, function(data){
data = data.message;
$this.modalobj.find('#material-list-pager').html('');
if(!_.isEmpty(data.items)) {
$this.modalobj.find('#btn-select').show();
$content.data('attachment', data.items);
$content.empty();
var Dialog = type + 'Dialog';
$content.html(_.template($this.buildHtml()[Dialog])(data));
if(type == 'news') {
setTimeout(function(){
$('.water').wookmark({
align: 'center',
autoResize: false,
container: $('#news'),
autoResize :true
});
}, 100);
}
$this.selectMedia();
$this.playaudio();
$this.modalobj.find('#material-list-pager').html(data.pager);
$this.modalobj.find('#material-list-pager .pagination a').click(function(){
$this.localPage(type, $(this).attr('page'));
});
} else {
$content.html(' 暂无数据
');
}
});
$this.modalobj.find('.modal-footer .btn-primary').unbind('click').click(function(){
var attachment = [];
$content.find('.checkedMedia').each(function(){
attachment.push($content.data('attachment')[$(this).data('attachid')]);
});
$this.finish(attachment);
});
return false;
},
'selectMedia' : function(){
var $this = this;
$this.modalobj.on('click', '.checkMedia', function(){
if(!$this.options.multiple) {
$('.checkMedia').removeClass('checkedMedia');
}
$(this).addClass('checkedMedia');
var type = $(this).data('type');
if(type == 'news') {
if(!$this.options.multiple) {
$('#news .panel-group').removeClass('selected');
}
$(this).addClass('selected');
} else if(type == 'image') {
if(!$this.options.multiple) {
$('#image div').removeClass('img-item-selected');
}
$(this).addClass('img-item-selected');
} else {
if(!$this.options.multiple) {
$('.checkMedia').removeClass('btn-primary');
}
$(this).addClass('btn-primary');
}
if(!$this.options.multiple) {
$this.modalobj.find('.modal-footer .btn-primary').trigger('click');
}
});
},
'playaudio' : function(){
$("#voice, .panel").on('click', '.audio-player-play', function(){
var src = $(this).data("attach");
if(!src) {
return;
}
if ($("#player")[0]) {
var player = $("#player");
} else {
var player = $('');
$(document.body).append(player);
}
player.data('control', $(this));
player.jPlayer({
playing: function() {
$(this).data('control').find("i").removeClass("fa-play").addClass("fa-stop");
},
pause: function (event) {
$(this).data('control').find("i").removeClass("fa-stop").addClass("fa-play");
},
swfPath: "resource/components/jplayer",
supplied: "mp3,wma,wav,amr",
solution: "html, flash"
});
player.jPlayer("setMedia", {mp3: $(this).data("attach")}).jPlayer("play");
if($(this).find("i").hasClass("fa-stop")) {
player.jPlayer("stop");
} else {
$('.audio-msg').find('.fa-stop').removeClass("fa-stop").addClass("fa-play");
player.jPlayer("setMedia", {mp3: $(this).data("attach")}).jPlayer("play");
}
});
},
'finish' : function(attachment) {
var $this = this;
if($.isFunction($this.options.callback)) {
if ($this.options.multiple == false) {
$this.options.callback(attachment[0]);
} else {
$this.options.callback(attachment);
}
$this.modalobj.modal('hide');
}
},
'buildHtml' : function() {
var dialog = {};
dialog['mainDialog'] = '\n' +
'
\n' +
'
\n' +
' \n' +
'
\n' +
'
'+
'
'+
'
'+
'
'+
'
'+
'
'+
'
' +
'
\n' +
' \n'+
'
\n' +
'
\n' +
'
';
dialog['imageDialog'] = '\n' +
'<%var items = _.sortBy(items, function(item) {return -item.id;});%>' +
'<%_.each(items, function(item) {%> \n' +
'\n' +
'<%});%>\n' +
'
';
dialog['voiceDialog'] =''+
' '+
' '+
' 标题 | '+
' 创建时间 | '+
' | '+
'
'+
' '+
' '+
' <%var items = _.sortBy(items, function(item) {return -item.createtime;});%>' +
' <%_.each(items, function(item) {%> \n' +
' '+
' <%=item.filename%> | '+
' <%=item.createtime_cn%> | '+
' '+
' '+
' | '+
'
'+
' <%});%>' +
' '+
'
';
dialog['videoDialog'] =''+
' '+
' '+
' 标题 | '+
' 创建时间 | '+
' | '+
'
'+
' '+
' '+
' <%var items = _.sortBy(items, function(item) {return -item.createtime;});%>' +
' <%_.each(items, function(item) {%> \n' +
' '+
' <%if(item.tag.title) {var title = item.tag.title} else {var title =item.filename}%>'+
' <%=title%> | '+
' <%=item.createtime_cn%> | '+
' '+
' '+
' | '+
'
'+
' <%});%>' +
' '+
'
';
dialog['wxcardDialog'] ='\n'+
' \n'+
' \n'+
' 标题 | \n'+
' 类型 | \n'+
' 卡券有效期 | \n'+
' 库存/每人限领 | \n'+
' 操作 | \n'+
'
'+
' '+
' '+
' <%var items = _.sortBy(items, function(item) {return -item.couponid;});%>' +
' <%_.each(items, function(item) {%> \n' +
' ' +
' <%=item.title%> | ' +
' <%if(item.ctype == "discount") {%>折扣券<%} else if(item.ctype == "cash") {%>代金券<%} else if(item.ctype == "gift") {%>礼品券<%} else if(item.ctype == "groupon") {%>团购券<%} else if(item.ctype == "general_coupon") {%>优惠券<%}%> | ' +
' <%if(item.date_info.time_type == 1) {%><%=item.date_info.time_limit_start%> ~ <%=item.date_info.time_limit_end%><%} else {%>领取后<%=item.date_info.date_info%>天后生效,<%=item.date_info.limit%>天有效期<%}%> | ' +
' <%=item.quantity%>/<%=item.get_limit%> | ' +
' 选取 | ' +
'
' +
' <%});%>' +
' '+
'
';
dialog['newsDialog'] = '<%var items = _.sortBy(items, function(item) {return -item.createtime;});%>' +
' <%_.each(items, function(item) {%> \n' +
' '+
' <%});%>';
return dialog;
}
};
return material;
});