define(['underscore', 'jquery.wookmark', 'jquery.jplayer'], function(_){ var material = { 'defaultoptions' : { callback : null, type : 'all', multiple : false, ignore : { 'basic' : false, 'wxcard' : true, 'image' : false, 'music' : false, 'news' : false, 'video' : false, 'voice' : false, 'keyword' : false, 'module' : false }, others : { 'basic' : { 'typeVal' : '' }, 'news' : { 'showwx' : true, 'showlocal' : true } } }, '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'); switch(type) { case 'basic': $('#material-Modal').addClass('basic'); break; case 'news': $('#material-Modal').addClass('news'); break; case 'music': $('#material-Modal').addClass('music'); break; case 'voice': $('#material-Modal').addClass('voice'); break; case 'video': $('#material-Modal').addClass('video'); break; case 'image': $('#material-Modal').addClass('image'); break; case 'keyword': $('#material-Modal').addClass('keyword'); break; case 'module': $('#material-Modal').addClass('module'); break; } $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'); initSelectEmotion(); return $this.modalobj; }, //newsmodel: 微信素材wx和本地素材local 'localPage' : function(type, page, newsmodel) { var $this = this; var page = page || 1; $('.checkMedia').removeClass('checkedMedia'); var $content = $this.modalobj.find('.material-content #' + type); $content.html('
数据加载中
'); if(type == 'basic') { var Dialog = type + 'Dialog'; $this.modalobj.find('#material-footer').show(); $content.html(_.template($this.buildHtml()[Dialog])); $this.modalobj.find('.modal-footer .btn-primary').unbind('click').click(function(){ var attachment = []; attachment.content = $('#basictext').val(); $this.options.callback(attachment); $this.modalobj.modal('hide'); }); return false; } if(type == 'music') { var Dialog = type + 'Dialog'; $this.modalobj.find('#material-footer').show(); $content.html(_.template($this.buildHtml()[Dialog])); $this.modalobj.find('.modal-footer .btn-primary').unbind('click').click(function(){ var attachment = []; attachment.title = $(':text[name="musicTitle"]').val(); attachment.url = $(':text[name="musicUrl"]').val(); attachment.HQUrl = $(':text[name="musicHQUrl"]').val(); attachment.description = $(':text[name="musicDescription"]').val(); $this.options.callback(attachment); $this.modalobj.modal('hide'); }); return false; } var url = './index.php?c=utility&a=material&do=list&type=' + type; if(type == 'wxcard') { url = './index.php?c=utility&a=coupon&do=wechat'; } if(type == 'keyword') { url = './index.php?c=utility&a=keyword&do=keyword&type=all'; } if(type == 'module') { url = './index.php?c=utility&a=modules&do=list'; } if (type == 'news') { var newsmodel = newsmodel == 'local' ? 'local' : 'wx'; url += newsmodel == 'local' ? '&newsmodel=local' : '&newsmodel=wx'; } $.getJSON(url, {'page': page}, function(data){ data = data.message.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.modalobj.find('.material-content .newsmodel-type').unbind('click').click(function(){ $(this).addClass('active').siblings().removeClass('active'); $this.localPage(type, 1, $(this).data('type')); return false; }); } $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'), newsmodel); return false; }); } else { if (type == 'news') { $('#news').html('
暂无数据
'); } else { $content.html('
暂无数据
'); } } if (type == 'news') { $this.modalobj.find('.material-content .newsmodel-type').unbind('click').click(function(){ $(this).addClass('active').siblings().removeClass('active'); $this.localPage(type, 1, $(this).data('type')); return false; }); } }); $this.modalobj.find('#btn-select .btn-primary').unbind('click').click(function(){ var attachment = []; $content.find('.checkedMedia').each(function(){ if (type == 'module') { attachment.push($content.data('attachment')[$(this).data('name')]); } else { attachment.push($content.data('attachment')[$(this).data('attachid')]); } }); $this.finish(attachment); }); return false; }, 'selectMedia' : function(){ var $this = this; $this.modalobj.find('.checkMedia').unbind('click').click(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('#btn-select .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'] = ''; dialog['basicDialog'] = '
'+ ' 您还可以使用表情和链接。'+ ' 表情'+ ' Emoji'+ '
'+ ' '; dialog['imageDialog'] = ''; dialog['musicDialog'] = '
'+ '
音乐标题
'+ '
'+ ' '+ '
'+ '
'+ '
'+ '
选择音乐
'+ '
'+ ' '+ ' 选择上传的音频文件或直接输入URL地址,常用格式:mp3'+ ' '+ ' '+ ' '+ ' '+ '
'+ '
'+ '
'+ '
'+ '
高品质链接
'+ '
'+ ' '+ ' 没有高品质音乐链接,请留空。高质量音乐链接,WIFI环境优先使用该链接播放音乐'+ '
'+ '
'+ '
'+ '
描述
'+ '
'+ ' '+ ' 描述内容将出现在音乐名称下方,建议控制在20个汉字以内最佳'+ '
'+ '
'; 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' + '
'+ '
'+ ' <%var index = 0;%>\n' + ' <%_.each(item.items, function(data) {%>\n' + ' <%index++;%>\n' + '
'+ ' <%if(index == 1) {%>\n' + '
'+ '
'+ ' 封面图片'+ ' '+ ' <%=data.title%>'+ '
'+ '
'+ ' <%} else {%>\n' + '
'+ '
'+ '

<%=data.title%>

'+ '
'+ '
'+ ' '+ ' 缩略图'+ '
'+ '
'+ ' <%}%>\n' + '
'+ ' <%});%>'+ '
'+ ' '+ '
'+ '
'+ ' <%});%>'+ '
'+ '
'+ '
'; dialog['keywordDialog'] = '
\n'+ '<%_.each(items, function(item) {%>' + '
'+ ' '+ ' '+ ' <%=item.content%>' + ' '+ ' '+ '
'+ '<%});%>'+ '
'; dialog['moduleDialog'] = ''; return dialog; } }; initSelectEmotion = function() { var $t = $('#basictext')[0]; var textbox = $("#basictext").val(); util.emotion($('.emotion-triggers'), $("#basictext"), function(txt, elm, target){ if ($t.selectionStart || $t.selectionStart == '0') { var startPos = $t.selectionStart; var endPos = $t.selectionEnd; var scrollTop = $t.scrollTop; $("#basictext").val($t.value.substring(0, startPos) + txt + $t.value.substring(endPos, $t.value.length)); $("#basictext").focus(); $t.selectionStart = startPos + txt.length; $t.selectionEnd = startPos + txt.length; $t.scrollTop = scrollTop; } else { $("#basictext").val(textbox+txt); $("#basictext").focus(); } }); }; initSelectEmoji = function() { var textbox = $("#basictext").val(); util.emojiBrowser(function(emoji){ var unshift = '[U+' + emoji.find("span").text() + ']'; $("#basictext").val(textbox+unshift); }); }; showAudioDialog = function(elm, base64options, options) { var btn = $(elm); var ipt = btn.parent().prev().prev(); var val = ipt.val(); util.audio(val, function(url){ if(url && url.attachment && url.url){ btn.prev().show(); ipt.val(url.attachment); ipt.attr("filename",url.filename); ipt.attr("url",url.url); setAudioPlayer(); } if(url && url.media_id){ ipt.val(url.media_id); } }, "" , {"direct": true, "multiple": false}); }; setAudioPlayer = function(){ $(function(){ $(".audio-player").each(function(){ $(this).prev().find("button").eq(0).click(function(){ var src = $(this).parent().prev().prev().val(); if($(this).find("i").hasClass("fa-stop")) { $(this).parent().parent().next().jPlayer("stop"); } else { if(src) { $(this).parent().parent().next().jPlayer("setMedia", {mp3: util.tomedia(src)}).jPlayer("play"); } } }); }); $(".audio-player").jPlayer({ playing: function() { $(this).prev().find("i").removeClass("fa-play").addClass("fa-stop"); }, pause: function (event) { $(this).prev().find("i").removeClass("fa-stop").addClass("fa-play"); }, swfPath: "resource/components/jplayer", supplied: "mp3" }); $(".audio-player-media").each(function(){ $(this).next().find(".audio-player-play").css("display", $(this).val() == "" ? "none" : ""); }); }); }; return material; });