media.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. {extend name="app/shop/view/base.html"/}
  2. {block name="resources"}
  3. <link rel="stylesheet" type="text/css" href="GIFTCARD_CSS/media_manager.css" />
  4. <style>
  5. body{background-color: #fff;}
  6. #iframeIcon{border: none;width: 100%;height: 449px;}
  7. .media-box .media-list-box{height: 380px;margin-right: 20px;display: flex;flex-direction: column;}
  8. .media-box .media-list-box .media-list {flex: 1;height: 0;overflow-y: scroll}
  9. .media-box .media-list-box .media-list::-webkit-scrollbar{display: none;}
  10. .media-box .media-img{max-height: 336px;}
  11. .media-box .media-img li{width: 185px;height: auto; margin: 0 7px 15px;}
  12. .media-box .media-img li div{height: 119.7px;line-height: 119.7px;background-size: cover;background-repeat: no-repeat;background-position: center;cursor: pointer;}
  13. .media-box .media-img li:nth-child(5n){margin-right: 0;}
  14. .layui-tab-content{padding: 0;height: 449px;}
  15. .layui-tab{margin: 5px 0 0;}
  16. .layui-tab .layui-tab-item{height: 449px;}
  17. .layui-laypage{margin: 10px 0 0;}
  18. .layui-tab-title {margin: 0 20px}
  19. #media {padding: 20px 20px 0;}
  20. .page {text-align: right}
  21. </style>
  22. {/block}
  23. {block name='body'}
  24. <div class="layui-tab layui-tab-brief media-tab">
  25. <ul class="layui-tab-title layui-hide">
  26. <li class="layui-this">选择图片</li>
  27. </ul>
  28. <div class="layui-tab-content">
  29. <div id="media" class="layui-tab-item">
  30. <div class="media-box">
  31. <div class="media-content">
  32. <ul class="media-img"></ul>
  33. <div id="paged" class="page"></div>
  34. </div>
  35. </div>
  36. </div>
  37. <div class="layui-tab-item">
  38. <iframe id="iframeIcon" src=""></iframe>
  39. </div>
  40. <input type="hidden" name="media_ids" value="{$media_ids}" />
  41. </div>
  42. </div>
  43. <!-- 图片展示 -->
  44. <script type="text/html" id="mediaList">
  45. {{# layui.each(d.list,function(index,item){ }}
  46. <li data-media-id="{{item.media_id}}" data-json_data='{{JSON.stringify(item)}}' class="media-list-item">
  47. <div class="bg-color-gray" style="background-image:url({{ ns.img(item.media_path) }})">
  48. {{# if( getActiveArrayIndex(item.media_id) != "-1"){ }}
  49. <div class="image-box-active border-color"><i class="active-index">{{ getActiveArrayIndex(item.media_id) }}</i></div>
  50. {{# } }}
  51. </div>
  52. </li>
  53. {{# }) }}
  54. {{# if(d.list.length === 0){ }}
  55. <div class="empty-data">暂无数据</div>
  56. {{# } }}
  57. </script>
  58. <script>
  59. var form, laytpl, laypage, upload,element,
  60. limit = 10,
  61. active_array = [],
  62. repeat_flag = false;
  63. layui.use(['form', 'laytpl', 'laypage', 'upload', 'element'], function() {
  64. form = layui.form;
  65. laytpl = layui.laytpl;
  66. laypage = layui.laypage;
  67. element = layui.element;
  68. upload = layui.upload;
  69. form.render();
  70. //初始化数据
  71. init();
  72. //监听搜索事件
  73. form.on('submit(search)', function() {
  74. mediaImgList(1, limit);
  75. });
  76. });
  77. /**
  78. * 素材图片加载
  79. * @param page
  80. * @param limit
  81. */
  82. function mediaImgList(page, limit) {
  83. $.ajax({
  84. url: ns.url("giftcard://shop/media/media"),
  85. type: "POST",
  86. dataType: "JSON",
  87. async: false,
  88. data: {
  89. limit,
  90. page,
  91. media_name: $(".media-img-sreach").val(),
  92. app_module:ns_url.appModule,
  93. site_id:ns_url.siteId
  94. },
  95. success: function(res) {
  96. laytpl($("#mediaList").html()).render(res.data, function(data) {
  97. $(".media-img").html(data);
  98. });
  99. laypage.render({
  100. elem: 'paged',
  101. count: res.data.count,
  102. limit,
  103. curr: page,
  104. jump: function(obj, first) {
  105. if (!first) {
  106. mediaImgList(obj.curr, obj.limit);
  107. }
  108. }
  109. })
  110. }
  111. })
  112. }
  113. /**
  114. * 选择个体
  115. */
  116. function checkItem() {
  117. $("#media").unbind('click').on("click", ".media-list-item", function() {
  118. var json_data = $(this).data("json_data");
  119. json_data.id = parseInt(json_data.media_id);
  120. if ($(this).find(".image-box-active").length > 0) {
  121. var active_index = getDelateActiveArrayIndex(json_data.id);
  122. sortActiveArrayIndex(active_index);
  123. $(this).find(".image-box-active").remove();
  124. } else {
  125. json_data.index = active_array.length + 1;
  126. if (json_data.index > imgNum) {
  127. $(".media-box .media-img li").each((index,item) => {
  128. if(active_array.length > 0 && $(item).attr("data-media-id") == active_array[0].media_id)
  129. $(item).find(".image-box-active").remove();
  130. });
  131. var active_index = active_array.length > 0 ? getDelateActiveArrayIndex(active_array[0].media_id) : 0;
  132. sortActiveArrayIndex(active_index);
  133. --json_data.index;
  134. }
  135. active_array.push(json_data);
  136. var active_html = '<div class="image-box-active border-color"><i class="active-index">' + active_array.length +
  137. '</i></div>';
  138. $(this).find("div").append(active_html);
  139. }
  140. });
  141. }
  142. //获取选择图片信息
  143. function getCheckItem(callback) {
  144. if (typeof callback != "function") return false;
  145. if($(".layui-tab-content .layui-tab-item").eq(0).hasClass("layui-show")){
  146. callback(active_array);
  147. }else{
  148. var currIcon = document.getElementById('iframeIcon').contentWindow.currIcon;
  149. callback(currIcon);
  150. }
  151. }
  152. //获取选中
  153. function getActiveArrayIndex(id) {
  154. var delete_index = -1;
  155. $.each(active_array, function(i, item) {
  156. if (item.media_id == id) {
  157. delete_index = item.index;
  158. return false;
  159. }
  160. });
  161. return delete_index;
  162. }
  163. //删除选中
  164. function getDelateActiveArrayIndex(id) {
  165. var delete_index;
  166. $.each(active_array, function(i, item) {
  167. if (item.id == id) {
  168. delete_index = item.index;
  169. active_array.splice(i, 1);
  170. return false;
  171. }
  172. });
  173. return delete_index;
  174. }
  175. //重新排序
  176. function sortActiveArrayIndex(index) {
  177. $.each(active_array, function(i, item) {
  178. var item_index = item.index;
  179. if (item_index > index) {
  180. active_array[i]["index"] = item_index - 1;
  181. if ($("#media").find(".media-list-item[data-media-id = '" + item["id"] + "']").length > 0) {
  182. $("#media").find(".media-list-item[data-media-id = '" + item["id"] + "']").find(".image-box-active i ").text(item["index"]);
  183. }
  184. }
  185. })
  186. }
  187. /**
  188. * 初始化数据
  189. */
  190. function init() {
  191. mediaImgList(1, limit); //分组图像
  192. checkItem(); //选择个体
  193. $(".layui-tab-content .layui-tab-item").eq(0).addClass('layui-show').siblings().removeClass('layui-show');
  194. }
  195. var search = window.location.search,
  196. imgNum = parseInt(getSearchString('imgNum', search));
  197. function getSearchString(key, Url) {
  198. var str = Url;
  199. str = str.substring(1, str.length); // 获取URL中?之后的字符(去掉第一位的问号)
  200. // 以&分隔字符串,获得类似name=xiaoli这样的元素数组
  201. var arr = str.split("&");
  202. var obj = new Object();
  203. // 将每一个数组元素以=分隔并赋给obj对象
  204. for (var i = 0; i < arr.length; i++) {
  205. var tmp_arr = arr[i].split("=");
  206. obj[decodeURIComponent(tmp_arr[0])] = decodeURIComponent(tmp_arr[1]);
  207. }
  208. return obj[key];
  209. }
  210. </script>
  211. {/block}