人人商城

material.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. define(['jquery', 'underscore', 'bootstrap', 'jquery.wookmark', 'jquery.jplayer'], function($, _){
  2. var material = {
  3. 'defaultoptions' : {
  4. callback : null,
  5. type : 'all',
  6. multiple : false,
  7. ignore : {
  8. 'wxcard' : true,
  9. 'image' : false,
  10. 'news' : false,
  11. 'video' : false,
  12. 'voice' : false
  13. }
  14. },
  15. 'init' : function(callback, options) {
  16. var $this = this;
  17. $this.options = $.extend({}, $this.defaultoptions, options);
  18. $this.options.callback = callback;
  19. $('#material-Modal').remove();
  20. $(document.body).append($this.buildHtml().mainDialog);
  21. $this.modalobj = $('#material-Modal');
  22. $this.modalobj.find('.modal-header .nav li a').click(function(){
  23. var type = $(this).data('type');
  24. $this.localPage(type, 1);
  25. $(this).tab('show')
  26. return false;
  27. });
  28. if (!$(this).data('init')) {
  29. if($this.options.type && $this.options.type != 'all') {
  30. $this.modalobj.find('.modal-header .nav li.' + $this.options.type + ' a').trigger('click');
  31. } else {
  32. $this.modalobj.find('.modal-header .nav li.show:first a').trigger('click');
  33. }
  34. }
  35. $this.modalobj.modal('show');
  36. return $this.modalobj;
  37. },
  38. 'localPage' : function(type, page) {
  39. var $this = this;
  40. var page = page || 1;
  41. $('.checkMedia').removeClass('checkedMedia');
  42. var $content = $this.modalobj.find('.material-content #' + type);
  43. $content.html('<div class="info text-center"><i class="fa fa-spinner fa-pulse fa-lg"></i> 数据加载中</div>');
  44. var url = './index.php?c=utility&a=material&do=list&type=' + type;
  45. if(type == 'wxcard') {
  46. url = './index.php?c=utility&a=coupon&do=wechat';
  47. }
  48. $.getJSON(url, {'page': page}, function(data){
  49. data = data.message;
  50. $this.modalobj.find('#material-list-pager').html('');
  51. if(!_.isEmpty(data.items)) {
  52. $this.modalobj.find('#btn-select').show();
  53. $content.data('attachment', data.items);
  54. $content.empty();
  55. var Dialog = type + 'Dialog';
  56. $content.html(_.template($this.buildHtml()[Dialog])(data));
  57. if(type == 'news') {
  58. setTimeout(function(){
  59. $('.water').wookmark({
  60. align: 'center',
  61. autoResize: false,
  62. container: $('#news'),
  63. autoResize :true
  64. });
  65. }, 100);
  66. }
  67. $this.selectMedia();
  68. $this.playaudio();
  69. $this.modalobj.find('#material-list-pager').html(data.pager);
  70. $this.modalobj.find('#material-list-pager .pagination a').click(function(){
  71. $this.localPage(type, $(this).attr('page'));
  72. });
  73. } else {
  74. $content.html('<div class="info text-center"><i class="fa fa-info-circle fa-lg"></i> 暂无数据</div>');
  75. }
  76. });
  77. $this.modalobj.find('.modal-footer .btn-primary').unbind('click').click(function(){
  78. var attachment = [];
  79. $content.find('.checkedMedia').each(function(){
  80. attachment.push($content.data('attachment')[$(this).data('attachid')]);
  81. });
  82. $this.finish(attachment);
  83. });
  84. return false;
  85. },
  86. 'selectMedia' : function(){
  87. var $this = this;
  88. $this.modalobj.on('click', '.checkMedia', function(){
  89. if(!$this.options.multiple) {
  90. $('.checkMedia').removeClass('checkedMedia');
  91. }
  92. $(this).addClass('checkedMedia');
  93. var type = $(this).data('type');
  94. if(type == 'news') {
  95. if(!$this.options.multiple) {
  96. $('#news .panel-group').removeClass('selected');
  97. }
  98. $(this).addClass('selected');
  99. } else if(type == 'image') {
  100. if(!$this.options.multiple) {
  101. $('#image div').removeClass('img-item-selected');
  102. }
  103. $(this).addClass('img-item-selected');
  104. } else {
  105. if(!$this.options.multiple) {
  106. $('.checkMedia').removeClass('btn-primary');
  107. }
  108. $(this).addClass('btn-primary');
  109. }
  110. if(!$this.options.multiple) {
  111. $this.modalobj.find('.modal-footer .btn-primary').trigger('click');
  112. }
  113. });
  114. },
  115. 'playaudio' : function(){
  116. $("#voice, .panel").on('click', '.audio-player-play', function(){
  117. var src = $(this).data("attach");
  118. if(!src) {
  119. return;
  120. }
  121. if ($("#player")[0]) {
  122. var player = $("#player");
  123. } else {
  124. var player = $('<div id="player"></div>');
  125. $(document.body).append(player);
  126. }
  127. player.data('control', $(this));
  128. player.jPlayer({
  129. playing: function() {
  130. $(this).data('control').find("i").removeClass("fa-play").addClass("fa-stop");
  131. },
  132. pause: function (event) {
  133. $(this).data('control').find("i").removeClass("fa-stop").addClass("fa-play");
  134. },
  135. swfPath: "resource/components/jplayer",
  136. supplied: "mp3,wma,wav,amr",
  137. solution: "html, flash"
  138. });
  139. player.jPlayer("setMedia", {mp3: $(this).data("attach")}).jPlayer("play");
  140. if($(this).find("i").hasClass("fa-stop")) {
  141. player.jPlayer("stop");
  142. } else {
  143. $('.audio-msg').find('.fa-stop').removeClass("fa-stop").addClass("fa-play");
  144. player.jPlayer("setMedia", {mp3: $(this).data("attach")}).jPlayer("play");
  145. }
  146. });
  147. },
  148. 'finish' : function(attachment) {
  149. var $this = this;
  150. if($.isFunction($this.options.callback)) {
  151. if ($this.options.multiple == false) {
  152. $this.options.callback(attachment[0]);
  153. } else {
  154. $this.options.callback(attachment);
  155. }
  156. $this.modalobj.modal('hide');
  157. }
  158. },
  159. 'buildHtml' : function() {
  160. var dialog = {};
  161. dialog['mainDialog'] = '<div id="material-Modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">\n' +
  162. ' <div class="modal-dialog">\n' +
  163. ' <div class="modal-content modal-lg">\n' +
  164. ' <div class="modal-header">\n' +
  165. ' <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\n' +
  166. ' <h3>'+
  167. ' <ul role="tablist" class="nav nav-pills" style="font-size:14px; margin-top:-20px;">'+
  168. ' <li role="presentation" class="news ' + (this.options.ignore.news ? 'hide' : 'show') + '">'+
  169. ' <a data-toggle="tab" data-type="news" role="tab" aria-controls="news" href="#news">图文</a>'+
  170. ' </li>'+
  171. ' <li role="presentation" class="image ' + (this.options.ignore.image ? 'hide' : 'show') + '">'+
  172. ' <a data-toggle="tab" data-type="image" role="tab" aria-controls="image" href="#image">图片</a>'+
  173. ' </li>'+
  174. ' <li role="presentation" class="voice ' + (this.options.ignore.voice ? 'hide' : 'show') + '">'+
  175. ' <a data-toggle="tab" data-type="voice" role="tab" aria-controls="voice" href="#voice">语音</a>'+
  176. ' </li>'+
  177. ' <li role="presentation" class="video ' + (this.options.ignore.video ? 'hide' : 'show') + '">'+
  178. ' <a data-toggle="tab" data-type="video" role="tab" aria-controls="video" href="#video">视频</a>'+
  179. ' </li>'+
  180. ' <li role="presentation" class="wxcard ' + (this.options.ignore.wxcard ? 'hide' : 'show') + '">'+
  181. ' <a data-toggle="tab" data-type="wxcard" role="tab" aria-controls="wxcard" href="#wxcard">微信卡券</a>'+
  182. ' </li>'+
  183. ' </ul>'+
  184. ' </h3>'+
  185. ' </div>\n' +
  186. ' <div class="modal-body material-content">\n' +
  187. ' <div class="tab-content">'+
  188. ' <div id="news" class="tab-pane material clearfix" class="active" role="tabpanel" style="position:relative"></div>'+
  189. ' <div id="image" class="tab-pane history" role="tabpanel"></div>'+
  190. ' <div id="voice" class="tab-pane" role="tabpanel"></div>'+
  191. ' <div id="video" class="tab-pane" role="tabpanel"></div>'+
  192. ' <div id="wxcard" class="tab-pane" role="tabpanel"></div>'+
  193. ' </div>' +
  194. ' </div>\n' +
  195. ' <div class="modal-footer">\n' +
  196. ' <div style="float: left;">\n' +
  197. ' <nav id="material-list-pager">\n' +
  198. ' </nav>\n' +
  199. ' </div>\n' +
  200. ' <div id="btn-select" style="float: right; display: none">\n' +
  201. ' <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>\n' +
  202. ' <button type="button" class="btn btn-primary">确认</button>\n' +
  203. ' </div>\n' +
  204. ' </div>\n'+
  205. ' </div>\n' +
  206. ' </div>\n' +
  207. '</div>';
  208. dialog['imageDialog'] = '<ul class="img-list clearfix">\n' +
  209. '<%var items = _.sortBy(items, function(item) {return -item.id;});%>' +
  210. '<%_.each(items, function(item) {%> \n' +
  211. '<div class="checkMedia" data-media="<%=item.media_id%>" data-type="image" data-attachid="<%=item.id%>">' +
  212. ' <li class="img-item" style="padding:5px">\n' +
  213. ' <div class="img-container" style="background-image: url(\'<%=item.attach%>\');">\n' +
  214. ' <div class="select-status"><span></span></div>\n' +
  215. ' </div>\n' +
  216. ' </li>\n' +
  217. '</div>\n' +
  218. '<%});%>\n' +
  219. '</ul>';
  220. dialog['voiceDialog'] ='<table class="table table-hover table-bordered" style="margin-bottom:0">'+
  221. ' <thead class="navbar-inner">'+
  222. ' <tr>'+
  223. ' <th>标题</th>'+
  224. ' <th style="width:20%;text-align:right">创建时间</th>'+
  225. ' <th style="width:20%;text-align:right"></th>'+
  226. ' </tr>'+
  227. ' </thead>'+
  228. ' <tbody class="history-content">'+
  229. ' <%var items = _.sortBy(items, function(item) {return -item.createtime;});%>' +
  230. ' <%_.each(items, function(item) {%> \n' +
  231. ' <tr>'+
  232. ' <td><%=item.filename%></td>'+
  233. ' <td align="right"><%=item.createtime_cn%></td>'+
  234. ' <td align="right">'+
  235. ' <div class="btn-group">'+
  236. ' <a href="javascript:;" class="btn btn-default btn-sm audio-player-play audio-msg" data-attach="<%=item.attach%>"><i class="fa fa-play"></i></a>'+
  237. ' <a href="javascript:;" class="btn btn-default btn-sm checkMedia" data-media="<%=item.media_id%>" data-type="voice" data-attachid="<%=item.id%>">选取</a>'+
  238. ' </div>'+
  239. ' </td>'+
  240. ' </tr>'+
  241. ' <%});%>' +
  242. ' </tbody>'+
  243. ' </table>';
  244. dialog['videoDialog'] ='<table class="table table-hover table-bordered" style="margin-bottom:0">'+
  245. ' <thead class="navbar-inner">'+
  246. ' <tr>'+
  247. ' <th>标题</th>'+
  248. ' <th style="width:20%;text-align:right">创建时间</th>'+
  249. ' <th style="width:20%;text-align:right"></th>'+
  250. ' </tr>'+
  251. ' </thead>'+
  252. ' <tbody class="history-content">'+
  253. ' <%var items = _.sortBy(items, function(item) {return -item.createtime;});%>' +
  254. ' <%_.each(items, function(item) {%> \n' +
  255. ' <tr>'+
  256. ' <%if(item.tag.title) {var title = item.tag.title} else {var title =item.filename}%>'+
  257. ' <td><%=title%></td>'+
  258. ' <td align="right"><%=item.createtime_cn%></td>'+
  259. ' <td align="right">'+
  260. ' <div class="btn-group">'+
  261. ' <a href="javascript:;" class="btn btn-default btn-sm checkMedia" data-media="<%=item.media_id%>" data-type="video" data-attachid="<%=item.id%>">选取</a>'+
  262. ' </div>'+
  263. ' </td>'+
  264. ' </tr>'+
  265. ' <%});%>' +
  266. ' </tbody>'+
  267. ' </table>';
  268. dialog['wxcardDialog'] ='<table class="table table-hover table-bordered">\n'+
  269. ' <thead>\n'+
  270. ' <tr>\n'+
  271. ' <th width="130" class="text-center">标题</th>\n'+
  272. ' <th class="text-center">类型</th>\n'+
  273. ' <th width="250" class="text-center">卡券有效期</th>\n'+
  274. ' <th class="text-center">库存/每人限领</th>\n'+
  275. ' <th class="text-center">操作</th>\n'+
  276. ' </tr>'+
  277. ' </thead>'+
  278. ' <tbody>'+
  279. ' <%var items = _.sortBy(items, function(item) {return -item.couponid;});%>' +
  280. ' <%_.each(items, function(item) {%> \n' +
  281. ' <tr title="<%=item.title%>">' +
  282. ' <td><%=item.title%></td>' +
  283. ' <td><%if(item.ctype == "discount") {%><span class="label label-success">折扣券</span><%} else if(item.ctype == "cash") {%><span class="label label-danger">代金券</span><%} else if(item.ctype == "gift") {%><span class="label label-danger">礼品券</span><%} else if(item.ctype == "groupon") {%><span class="label label-danger">团购券</span><%} else if(item.ctype == "general_coupon") {%><span class="label label-danger">优惠券</span><%}%></td>' +
  284. ' <td><%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%>天有效期<%}%></td>' +
  285. ' <td><%=item.quantity%>/<strong class="text-danger"><%=item.get_limit%></strong></td>' +
  286. ' <td><a href="javascript:;" class="btn btn-default btn-sm checkMedia" data-title="<%=item.title%>" data-type="wxcard" data-media="<%=item.card_id%>" data-attachid="<%=item.id%>">选取</a></td>' +
  287. ' </tr>' +
  288. ' <%});%>' +
  289. ' </tbody>'+
  290. ' </table>';
  291. dialog['newsDialog'] = '<%var items = _.sortBy(items, function(item) {return -item.createtime;});%>' +
  292. ' <%_.each(items, function(item) {%> \n' +
  293. ' <div class="col-md-5 col-md-5 col-md-5 water" style="display:none">'+
  294. ' <div class="panel-group checkMedia" data-media="<%=item.media_id%>" data-type="news" data-attachid="<%=item.id%>">'+
  295. ' <%var index = 0;%>\n' +
  296. ' <%_.each(item.items, function(data) {%>\n' +
  297. ' <%index++;%>\n' +
  298. ' <div class="panel panel-default">'+
  299. ' <%if(index == 1) {%>\n' +
  300. ' <div class="panel-body">'+
  301. ' <div class="img">'+
  302. ' <i class="default">封面图片</i>'+
  303. ' <img src="<%=data.thumb_url%>">'+
  304. ' <span class="text-left"><%=data.title%></span>'+
  305. ' </div>'+
  306. ' </div>'+
  307. ' <%} else {%>\n' +
  308. ' <div class="panel-body">'+
  309. ' <div class="text">'+
  310. ' <h4><%=data.title%></h4>'+
  311. ' </div>'+
  312. ' <div class="img">'+
  313. ' <img src="<%=data.thumb_url%>">'+
  314. ' <i class="default">缩略图</i>'+
  315. ' </div>'+
  316. ' </div>'+
  317. ' <%}%>\n' +
  318. ' </div>'+
  319. ' <%});%>'+
  320. ' <div class="mask"></div>'+
  321. ' <i class="fa fa-check"></i>'+
  322. ' </div>'+
  323. ' </div>'+
  324. ' <%});%>';
  325. return dialog;
  326. }
  327. };
  328. return material;
  329. });