poster.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var imgsettimer = 0 ;
  3. var nametimer = 0;
  4. var bgtimer = 0 ;
  5. var qrtimer = 0 ;
  6. var Controller = {
  7. index: function () {
  8. // 初始化表格参数配置
  9. Table.api.init({
  10. extend: {
  11. index_url: 'poster/index' + location.search,
  12. add_url: 'poster/add',
  13. edit_url: 'poster/edit',
  14. del_url: 'poster/del',
  15. multi_url: 'poster/multi',
  16. table: 'poster',
  17. }
  18. });
  19. var table = $("#table");
  20. // 初始化表格
  21. table.bootstrapTable({
  22. url: $.fn.bootstrapTable.defaults.extend.index_url,
  23. pk: 'id',
  24. sortName: 'weigh',
  25. commonSearch: false,
  26. columns: [
  27. [
  28. {checkbox: true},
  29. {field: 'id', title: __('Id')},
  30. {field: 'title', title: __('Title')},
  31. {field: 'bg_image', title: __('Bg_image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
  32. {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
  33. {field: 'weigh', title: __('Weigh')},
  34. {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  35. {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  36. {
  37. field: 'datalist', title: __('Operate'), table: table,operate:false,
  38. buttons: [
  39. {
  40. name: 'content',
  41. text: __('Posterrecord'),
  42. extend: 'data-area=\'["1024px", "678px"]\'',
  43. classname: 'btn btn-xs btn-success btn-dialog',
  44. icon: 'fa fa-list-ul',
  45. url: 'poster/posterrecord/ids/{ids}'
  46. }
  47. ],
  48. formatter: Table.api.formatter.buttons
  49. },
  50. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  51. ]
  52. ]
  53. });
  54. // 为表格绑定事件
  55. Table.api.bindevent(table);
  56. table.on('post-body.bs.table', function () {
  57. $(".btn-editone").data("area", ["1024px", "678px"]);
  58. });
  59. },
  60. posterrecord: function () {
  61. // 初始化表格参数配置
  62. Table.api.init({
  63. extend: {
  64. index_url: 'poster/posterrecord' + location.search + '&ids=' + Config.ids,
  65. table: 'poster',
  66. }
  67. });
  68. var table = $("#table");
  69. // 初始化表格
  70. table.bootstrapTable({
  71. url: $.fn.bootstrapTable.defaults.extend.index_url,
  72. pk: 'id',
  73. sortName: 'id',
  74. commonSearch: false,
  75. columns: [
  76. [
  77. {field: 'id', title: __('Id')},
  78. {field: 'user.nickname', title: __('Nickname')},
  79. {field: 'image', title: __('Image'), events: Controller.api.events.image, formatter: Controller.api.image},
  80. {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  81. {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  82. ]
  83. ]
  84. });
  85. // 为表格绑定事件
  86. Table.api.bindevent(table);
  87. $('.btn-clear').click(function(){
  88. var url = $(this).data('url');
  89. Fast.api.ajax({
  90. url: url,
  91. }, function (data, ret) {
  92. table.bootstrapTable('refresh', {});
  93. }, function (data, ret) {
  94. });
  95. });
  96. },
  97. add: function () {
  98. Controller.api.bindevent();
  99. },
  100. edit: function () {
  101. Controller.api.bindevent();
  102. },
  103. api: {
  104. bindevent: function () {
  105. $("form[role=form]").data("validator-options", {ignore: ':hidden'});
  106. //表单提交事件绑定
  107. Form.api.bindevent($("form[role=form]"), function (data, ret) {}, function (data, ret) {}, function (success, error) {
  108. var data = [];
  109. $('.drag').each(function () {
  110. var obj = $(this);
  111. var type = obj.attr('type');
  112. var left = obj.css('left'), top = obj.css('top');
  113. var d = {
  114. left: left,
  115. top: top,
  116. type: obj.attr('type'),
  117. width: obj.css('width'),
  118. height: obj.css('height')
  119. };
  120. if (type == 'nickname') {
  121. d.size = obj.attr('size');
  122. d.color = obj.attr('color');
  123. } else if (type == 'qr') {
  124. d.size = obj.attr('size');
  125. d.qr_table = obj.attr('qr_table');
  126. d.qr_field = obj.attr('qr_field');
  127. d.qr_relation = obj.attr('qr_relation');
  128. } else if (type == 'img') {
  129. d.src = Fast.api.cdnurl(obj.attr('src'));
  130. }
  131. data.push(d);
  132. });
  133. $(':input[name="row[data]"]').val(JSON.stringify(data));
  134. });
  135. //图片地址变化
  136. $('#c-bg_image').on('change', function () {
  137. $('.bg').remove();
  138. $('#poster').prepend("<img src='" + Fast.api.cdnurl($(this).val()) + "' class='bg'/>");
  139. });
  140. $('#c-img').on('change', function () {
  141. $('.drag').each(function () {
  142. if ($(this).attr("type") == 'img') {
  143. $(this).find('img').attr('src', Fast.api.cdnurl($('#c-img').val()));
  144. }
  145. });
  146. });
  147. // 移除图片事件
  148. $(document.body).on("click", "#p-bg_image .btn-trash", function () {
  149. $('.bg').attr('src', '');
  150. });
  151. $(document.body).on("click", "#p-img .btn-trash", function () {
  152. $('.drag').each(function () {
  153. if ($(this).attr("type") == 'img') {
  154. $(this).attr('src', '');
  155. $(this).find('img').attr('src', '/assets/addons/poster/images/img.jpg');
  156. }
  157. });
  158. });
  159. require(['designer'], function () {
  160. //添加元素
  161. $('.btn-com').click(function(){
  162. var imgset = $('#imgset'), nameset = $("#nameset"),qrset = $('#qrset');
  163. imgset.hide(),nameset.hide(),qrset.hide();
  164. Controller.api.clearTimers();
  165. var type = $(this).data('type');
  166. $('.drag').each(function () {
  167. if ($(this).attr("type") == type) {
  168. type = false;
  169. }
  170. });
  171. if(type == false){
  172. Layer.msg('请不要重复添加');
  173. }else{
  174. var img = "";
  175. if(type=='qr'){
  176. img = '<img src="/assets/addons/poster/images/qr.jpg" />';
  177. } else if(type=='head'){
  178. img = '<img src="/assets/addons/poster/images/head.jpg" />';
  179. } else if(type=='img' || type=='thumb'){
  180. img = '<img src="/assets/addons/poster/images/img.jpg" />';
  181. } else if(type=='nickname'){
  182. img = '<div class=text>昵称</div>';
  183. }
  184. var index = $('#poster .drag').length + 1;
  185. var obj = $('<div class="drag" type="' + type +'" index="' + index +'" style="z-index:' + index+'">' + img+'<div class="rRightDown"> </div><div class="rLeftDown"> </div><div class="rRightUp"> </div><div class="rLeftUp"> </div><div class="rRight"> </div><div class="rLeft"> </div><div class="rUp"> </div><div class="rDown"></div></div>');
  186. $('#poster').append(obj);
  187. Controller.api.bindEvents(obj);
  188. }
  189. });
  190. $('.drag').each(function () {
  191. Controller.api.bindEvents($(this));
  192. Controller.api.bind($(this));
  193. });
  194. });
  195. //颜色
  196. Controller.api.color();
  197. //选择二维码数据表
  198. $(document).on('change', "select[name='qr_table']", function () {
  199. var that = this;
  200. Fast.api.ajax({
  201. url: "poster/get_field_list",
  202. data: {table: $(that).val()},
  203. }, function (data, ret) {
  204. mainfields = data.fieldlist;
  205. renderselect($("#c-qr_field"), mainfields, 'qr_field');//渲染数据
  206. renderselect($("#c-qr_relation"), mainfields, 'qr_relation');//渲染数据
  207. return false;
  208. });
  209. return false;
  210. });
  211. var renderselect = function (select, data, field_type) {
  212. var html = [];
  213. html.push("<option value=''>请选择</option>");
  214. for (var i = 0; i < data.length; i++) {
  215. html.push("<option value='" + data[i] + "'>" + data[i] + "</option>");
  216. }
  217. select.html(html.join(""));
  218. select.trigger("change");
  219. if (select.data("selectpicker")) {
  220. $('.drag').each(function () {
  221. var type = $(this).attr('type');
  222. if(type == 'qr'){
  223. if(field_type == 'qr_field'){
  224. var qr_field = $(this).attr('qr_field') || "";
  225. select.selectpicker('val',qr_field).trigger("change").trigger("validate");;
  226. } else {
  227. var qr_relation = $(this).attr('qr_relation') || "";
  228. select.selectpicker('val',qr_relation).trigger("change").trigger("validate");;
  229. }
  230. }
  231. });
  232. select.selectpicker('refresh').trigger("change");
  233. }
  234. return select;
  235. };
  236. $("select[name='qr_table']").trigger("change");
  237. },
  238. bindEvents: function (obj) {
  239. var index = obj.attr('index');
  240. var rs = new Resize(obj, { Max: true, mxContainer: "#poster" });
  241. rs.Set($(".rRightDown",obj), "right-down");
  242. rs.Set($(".rLeftDown",obj), "left-down");
  243. rs.Set($(".rRightUp",obj), "right-up");
  244. rs.Set($(".rLeftUp",obj), "left-up");
  245. rs.Set($(".rRight",obj), "right");
  246. rs.Set($(".rLeft",obj), "left");
  247. rs.Set($(".rUp",obj), "up");
  248. rs.Set($(".rDown",obj), "down");
  249. rs.Scale = true;
  250. var type = obj.attr('type');
  251. if(type=='nickname' || type=='img' || type=='title' || type=='marketprice' || type=='productprice'){
  252. rs.Scale = false;
  253. }
  254. new Drag(obj, { Limit: true, mxContainer: "#poster" });
  255. $('.drag .remove').unbind('click').click(function(){
  256. $(this).parent().remove();
  257. })
  258. require(['jquery.contextMenu'],function(){
  259. $.contextMenu({
  260. selector: '.drag[index=' + index + ']',
  261. callback: function(key, options) {
  262. var index = parseInt($(this).attr('zindex'));
  263. if(key=='next'){
  264. var nextdiv = $(this).next('.drag');
  265. if(nextdiv.length>0 ){
  266. nextdiv.insertBefore($(this));
  267. }
  268. } else if(key=='prev'){
  269. var prevdiv = $(this).prev('.drag');
  270. if(prevdiv.length>0 ){
  271. $(this).insertBefore(prevdiv);
  272. }
  273. } else if(key=='last'){
  274. var len = $('.drag').length;
  275. if(index >=len-1){
  276. return;
  277. }
  278. var last = $('#poster .drag:last');
  279. if(last.length>0){
  280. $(this).insertAfter(last);
  281. }
  282. }else if(key=='first'){
  283. var index = $(this).index();
  284. if(index<=1){
  285. return;
  286. }
  287. var first = $('#poster .drag:first');
  288. if(first.length>0){
  289. $(this).insertBefore(first);
  290. }
  291. }else if(key=='delete'){
  292. $(this).remove();
  293. }
  294. var n =1 ;
  295. $('.drag').each(function(){
  296. $(this).css("z-index",n);
  297. n++;
  298. })
  299. },
  300. items: {
  301. "last": {name: "调整到最顶层"},
  302. "first": {name: "调整到最低层"},
  303. "delete": {name: "删除元素"}
  304. }
  305. });
  306. obj.unbind('click').click(function () {
  307. Controller.api.bind($(this));
  308. });
  309. });
  310. },
  311. color: function () {
  312. var refreshStyle = function () {
  313. var style = [];
  314. if ($(".btn-color").hasClass("active")) {
  315. style.push($(".btn-color").data("color"));
  316. }
  317. $("#c-color").val(style.join("|"));
  318. };
  319. if ($(".btn-color").hasClass("active")) {
  320. style.push($(".btn-color").data("color"));
  321. }
  322. require(['jquery-colorpicker'], function () {
  323. $('.colorpicker').colorpicker({
  324. color: function () {
  325. var color = "#000000";
  326. var rgb = $("#c-color").css('color').match(/^rgb\(((\d+),\s*(\d+),\s*(\d+))\)$/);
  327. if (rgb) {
  328. color = rgb[1];
  329. }
  330. return color;
  331. }
  332. }, function (event, obj) {
  333. $("#c-color").css('color', '#' + obj.hex);
  334. $(event).addClass("active").data("color", '#' + obj.hex);
  335. refreshStyle();
  336. }, function (event) {
  337. $("#c-color").css('color', 'inherit');
  338. $(event).removeClass("active");
  339. refreshStyle();
  340. });
  341. });
  342. },
  343. bind: function (obj) {
  344. var imgset = $('.imgset'), nameset = $(".nameset"),qrset = $('.qrset');
  345. imgset.hide(),nameset.hide(),qrset.hide();
  346. Controller.api.clearTimers();
  347. var type = obj.attr('type');
  348. //图片
  349. if(type=='img'){
  350. imgset.show();
  351. var src = obj.attr('src');
  352. var input = imgset.find('#c-img');
  353. if(typeof(src)!='undefined' && src!=''){
  354. input.val(src).trigger("change").trigger("validate");
  355. }
  356. imgsettimer = setInterval(function(){
  357. if(input.val()!=src && input.val()!=''){
  358. var url = input.val();
  359. obj.attr('src',Fast.api.cdnurl(input.val()));
  360. }
  361. },50);
  362. //昵称
  363. } else if(type=='nickname'){
  364. nameset.show();
  365. var color = obj.attr('color') || "#000";
  366. var size = obj.attr('size') || "16";
  367. var input = nameset.find('input:first');
  368. var namesize = nameset.find('#namesize');
  369. var picker = nameset.find('.sp-preview-inner');
  370. input.val(color); namesize.val(size.replace("px",""));
  371. picker.css( {'background-color':color,'font-size':size});
  372. nametimer = setInterval(function(){
  373. obj.attr('color',input.val()).find('.text').css('color',input.val());
  374. obj.attr('size',namesize.val() +"px").find('.text').css('font-size',namesize.val() +"px");
  375. },50);
  376. //二维码
  377. } else if(type=='qr'){
  378. qrset.show();
  379. //得到属性上的值
  380. var qr_table = obj.attr('qr_table') || "";
  381. var qr_field = obj.attr('qr_field') || "";
  382. var qr_relation = obj.attr('qr_relation') || "";
  383. //表单
  384. var qrtable = qrset.find("select[name='qr_table']");
  385. var qrfield = qrset.find("#c-qr_field");
  386. var qrrelation = qrset.find("#c-qr_relation");
  387. //编辑的时候吧属性值设置到编辑框
  388. if(typeof(qr_table)!='undefined' && qr_table!=''){
  389. qrtable.val(qr_table).trigger("change").trigger("validate");
  390. }
  391. if(typeof(qr_relation)!='undefined' && qr_relation!=''){
  392. qrrelation.val(qr_relation).trigger("change").trigger("validate");
  393. }
  394. //修改内容的时候设置attr
  395. qrtimer = setInterval(function(){
  396. if(qrtable.val()!=qr_table && qrtable.val()!='' && qrtable.val() != 'null' && qrtable.val() != null){
  397. obj.attr('qr_table',qrtable.val());
  398. }
  399. if(qrfield.val()!=qr_field && qrfield.val()!='' && qrfield.val() != 'null' && qrfield.val() != null){
  400. obj.attr('qr_field',qrfield.val());
  401. }
  402. if(qrrelation.val()!=qr_relation && qrrelation.val()!='' && qrrelation.val() != 'null' && qrrelation.val() != null){
  403. obj.attr('qr_relation',qrrelation.val());
  404. }
  405. },50);
  406. }
  407. },
  408. clearTimers: function () {
  409. clearInterval(imgsettimer);
  410. clearInterval(nametimer);
  411. clearInterval(bgtimer);
  412. clearInterval(qrtimer);
  413. },
  414. image: function (value, row, index) {
  415. value = value ? value : '/assets/img/blank.gif';
  416. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  417. return '<a href="javascript:"><img class="' + classname + '" src="' + value + '" /></a>';
  418. },
  419. events: {
  420. //单元格图片预览
  421. image: {
  422. 'click .img-center': function (e, value, row, index) {
  423. var data = [];
  424. value = value.toString().split(",");
  425. $.each(value, function (index, value) {
  426. data.push({
  427. src: value,
  428. });
  429. });
  430. Layer.photos({
  431. photos: {
  432. "start": $(this).parent().index(),
  433. "data": data
  434. },
  435. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  436. });
  437. },
  438. }
  439. },
  440. }
  441. };
  442. return Controller;
  443. });