diy.adv.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. define(['jquery.ui'], function (ui) {
  2. var modal = {itemid: ''};
  3. modal.init = function (params) {
  4. window.tpl = params.tpl;
  5. modal.attachurl = params.attachurl;
  6. modal.advs = params.menu;
  7. modal.id = params.id;
  8. modal.merch = params.merch;
  9. if (!modal.advs) {
  10. modal.advs = {
  11. name: '未命名启动广告',
  12. status: 0,
  13. params: {
  14. 'style': 'small-bot',
  15. 'showtype': '1',
  16. 'showtime': '60',
  17. 'autoclose': '10',
  18. 'canclose': '1'
  19. },
  20. style: {
  21. 'background': '#000000',
  22. 'opacity': '0.6'
  23. },
  24. data: {
  25. M0123456789101: {
  26. imgurl: '../addons/ewei_shopv2/plugin/diypage/static/images/default/adv-1.jpg',
  27. linkurl: '',
  28. click: '0'
  29. },
  30. M0123456789102: {
  31. imgurl: '../addons/ewei_shopv2/plugin/diypage/static/images/default/adv-2.jpg',
  32. linkurl: '',
  33. click: '0'
  34. },
  35. M0123456789103: {
  36. imgurl: '../addons/ewei_shopv2/plugin/diypage/static/images/default/adv-3.jpg',
  37. linkurl: '',
  38. click: '0'
  39. }
  40. }
  41. }
  42. }
  43. tpl.helper("imgsrc", function (src) {
  44. if (typeof src != 'string') {
  45. return ''
  46. }
  47. if (src.indexOf('http://') == 0 || src.indexOf('https://') == 0 || src.indexOf('../addons') == 0) {
  48. return src
  49. } else if (src.indexOf('images/') == 0) {
  50. return modal.attachurl + src
  51. }
  52. });
  53. tpl.helper("count", function (data) {
  54. return modal.length(data)
  55. });
  56. tpl.helper("link", function (link) {
  57. if (!link) {
  58. return
  59. }
  60. return '../app/' + link
  61. });
  62. tpl.helper("px", function (num) {
  63. return num / 20
  64. });
  65. modal.initItems();
  66. modal.initEditor();
  67. modal.initGotop();
  68. $(".btn-save").unbind('click').click(function () {
  69. var status = $(this).data('status');
  70. if (status) {
  71. tip.msgbox.err("正在保存,请稍候。。。");
  72. return
  73. }
  74. modal.save()
  75. })
  76. };
  77. modal.initItems = function () {
  78. var html = tpl("tpl_show_menu", modal.advs);
  79. $("#phone").html(html).show();
  80. var len = $(".diymenu .child").length;
  81. $(".diymenu .child").each(function (i) {
  82. var width = $(this).outerWidth();
  83. var margin = -(width / 2);
  84. var left = '50%';
  85. var pleft = $(this).position().left - width / 2;
  86. if(i==0 && pleft<2){
  87. left = 2;
  88. margin = 0;
  89. var pwidth = $(this).closest('.item').width();
  90. var arrowleft = pwidth / 2;
  91. var oldleft = parseFloat($(this).find('.arrow').css('left').replace('px', ''));
  92. $(this).find('.arrow').css({'left': arrowleft - 10, 'margin-left': 0})
  93. } else if (i + 1 == len) {
  94. var pwidth = $(this).closest('.item').width();
  95. if(width>pwidth){
  96. var left = - (width - pwidth) - 2;
  97. margin = 0;
  98. var c = $(this).closest('.item').width() / 2;
  99. var arrowleft = width - c;
  100. $(this).find('.arrow').css({'left': arrowleft - 8, 'margin-left': 0})
  101. }
  102. }
  103. $(this).css({'position': 'absolute', 'left': left, 'margin-left': margin, 'z-index': 0})
  104. })
  105. };
  106. modal.initSortable = function () {
  107. $("#diy-editor .inner").sortable({
  108. opacity: 0.8,
  109. placeholder: "highlight",
  110. items: '.item',
  111. revert: 100,
  112. scroll: false,
  113. cancel: '.goods-selector,input,.btn',
  114. start: function (event, ui) {
  115. var height = ui.item.height();
  116. $(".highlight").css({"height": height + 22 + "px"});
  117. $(".highlight").html('<div><i class="fa fa-plus"></i> 放置此处</div>');
  118. $(".highlight div").css({"line-height": height + 16 + "px"})
  119. },
  120. update: function (event, ui) {
  121. modal.sortItems()
  122. }
  123. });
  124. $("#diy-editor .inner .item-child").sortable({
  125. opacity: 0.8,
  126. placeholder: "highlight",
  127. items: '.item-body',
  128. revert: 100,
  129. scroll: false,
  130. cancel: '.goods-selector,input,.btn',
  131. start: function (event, ui) {
  132. var height = ui.item.height();
  133. $(".highlight").css({"height": height + "px"});
  134. $(".highlight").html('<div><i class="fa fa-plus"></i> 放置此处</div>');
  135. $(".highlight div").css({"line-height": height + 16 + "px"})
  136. },
  137. update: function (event, ui) {
  138. modal.sortChild()
  139. }
  140. })
  141. };
  142. modal.sortItems = function () {
  143. var newItems = {};
  144. $("#diy-editor .inner .item").each(function () {
  145. var thisid = $(this).data('id');
  146. newItems[thisid] = modal.advs.data[thisid]
  147. });
  148. modal.advs.data = newItems;
  149. modal.initItems()
  150. };
  151. modal.sortChild = function () {
  152. var newChild = {};
  153. var itemid = modal.itemid;
  154. $("#diy-editor .inner").find(".item[data-id='" + itemid + "'] .item-child .child").each(function () {
  155. var thisid = $(this).data('id');
  156. newChild[thisid] = modal.advs.data[itemid].child[thisid]
  157. });
  158. modal.advs.data[itemid].child = newChild;
  159. modal.initItems()
  160. };
  161. modal.initEditor = function () {
  162. var html = tpl("tpl_edit_menu", modal.advs);
  163. $("#diy-editor .inner").html(html);
  164. $("#diy-editor #addChild").unbind('click').click(function () {
  165. var itemid = $(this).closest('.item').data('id');
  166. var childid = modal.getId('C', 0);
  167. if (!modal.advs.data[itemid].child) {
  168. modal.advs.data[itemid].child = {}
  169. }
  170. modal.advs.data[itemid].child[childid] = {linkurl: '', text: '二级菜单'};
  171. modal.initItems();
  172. modal.initEditor()
  173. });
  174. $("#diy-editor #addItem").unbind('click').click(function () {
  175. var itemid = modal.getId('M', 0);
  176. var max = $(this).closest('.form-items').data('max');
  177. var num = modal.length(modal.advs.data);
  178. if (num >= max) {
  179. tip.msgbox.err("最大添加 " + max + " 个!");
  180. return
  181. }
  182. modal.advs.data[itemid] = {
  183. imgurl: '../addons/ewei_shopv2/plugin/diypage/static/images/default/menu-1.png',
  184. linkurl: '',
  185. iconclass: 'icon-home',
  186. text: '菜单文字'
  187. };
  188. modal.initItems();
  189. modal.initEditor()
  190. });
  191. $("#diy-editor .del-item").unbind('click').click(function () {
  192. var min = $(this).closest('.form-items').data('min');
  193. var itemid = $(this).closest('.item').data('id');
  194. if (min) {
  195. var length = modal.length(modal.advs.data);
  196. if (length <= min) {
  197. tip.msgbox.err("至少保留 " + min + " 个!");
  198. return
  199. }
  200. }
  201. tip.confirm("确定删除吗", function () {
  202. delete modal.advs.data[itemid];
  203. modal.initItems();
  204. modal.initEditor()
  205. })
  206. });
  207. $("#diy-editor .del-child").unbind('click').click(function () {
  208. var itemid = $(this).closest('.item').data('id');
  209. var childid = $(this).closest('.child').data('id');
  210. var item = modal.advs.data[itemid];
  211. if (item) {
  212. var child = modal.advs.data[itemid].child[childid];
  213. if (child) {
  214. tip.confirm("确定删除吗", function () {
  215. delete modal.advs.data[itemid].child[childid];
  216. modal.initItems();
  217. modal.initEditor()
  218. })
  219. }
  220. }
  221. });
  222. $("#diy-editor .fold").unbind('click').click(function () {
  223. var type = $(this).data('type');
  224. if (type == 1) {
  225. $(this).text('收起').data('type', 0).closest('.item').find('.item-child').show()
  226. } else {
  227. $(this).text('展开').data('type', 1).closest('.item').find('.item-child').hide()
  228. }
  229. });
  230. $(document).on('mousedown', "#diy-editor .item-child .child", function () {
  231. var itemid = $(this).closest('.item').data('id');
  232. modal.itemid = itemid
  233. });
  234. $("#diy-editor .slider").each(function () {
  235. var decimal = $(this).data('decimal');
  236. var multiply = $(this).data('multiply');
  237. var defaultValue = $(this).data("value");
  238. if (decimal) {
  239. defaultValue = defaultValue * decimal
  240. }
  241. $(this).slider({
  242. slide: function (event, ui) {
  243. var sliderValue = ui.value;
  244. if (decimal) {
  245. sliderValue = sliderValue / decimal
  246. }
  247. $(this).siblings(".input").val(sliderValue).trigger("propertychange");
  248. $(this).siblings(".count").find("span").text(sliderValue)
  249. }, value: defaultValue, min: $(this).data("min"), max: $(this).data("max")
  250. })
  251. });
  252. $("#diy-editor").find(".diy-bind").bind('input propertychange change', function () {
  253. var _this = $(this);
  254. var bind = _this.data("bind");
  255. var bindchild = _this.data('bind-child');
  256. var bindparent = _this.data('bind-parent');
  257. var bindthree = _this.data('bind-three');
  258. var initEditor = _this.data('bind-init');
  259. var value = '';
  260. var tag = this.tagName;
  261. if (tag == 'INPUT') {
  262. var placeholder = _this.data('placeholder');
  263. value = _this.val();
  264. value = value == '' ? placeholder : value
  265. } else if (tag == 'SELECT') {
  266. value = _this.find('option:selected').val()
  267. } else if (tag == 'TEXTAREA') {
  268. value = _this.val()
  269. }
  270. value = $.trim(value);
  271. if (bindchild) {
  272. if (bindparent) {
  273. if (bindthree) {
  274. modal.advs[bindchild][bindparent].child[bindthree][bind] = value
  275. } else {
  276. modal.advs[bindchild][bindparent][bind] = value
  277. }
  278. } else {
  279. modal.advs[bindchild][bind] = value
  280. }
  281. } else {
  282. modal.advs[bind] = value
  283. }
  284. modal.initItems();
  285. if (initEditor) {
  286. modal.initEditor()
  287. }
  288. });
  289. $("#phone").mouseenter(function () {
  290. $("#diy-editor").find('.diy-bind').blur()
  291. });
  292. $("#diy-editor").show();
  293. modal.initSortable()
  294. };
  295. modal.initGotop = function () {
  296. $(window).bind('scroll resize', function () {
  297. var scrolltop = $(window).scrollTop();
  298. if (scrolltop > 100) {
  299. $("#gotop").show()
  300. } else {
  301. $("#gotop").hide()
  302. }
  303. $("#gotop").unbind('click').click(function () {
  304. $('body').animate({scrollTop: "0px"}, 1000)
  305. })
  306. })
  307. };
  308. modal.length = function (json) {
  309. if (typeof(json) === 'undefined') {
  310. return 0
  311. }
  312. var jsonlen = 0;
  313. for (var item in json) {
  314. jsonlen++
  315. }
  316. return jsonlen
  317. };
  318. modal.getId = function (S, N) {
  319. var date = +new Date();
  320. var id = S + (date + N);
  321. return id
  322. };
  323. modal.save = function () {
  324. if (!modal.advs.data) {
  325. tip.msgbox.err("广告内容为空!");
  326. return
  327. }
  328. $(".btn-save").data('status', 1).text("保存中...");
  329. if (modal.id) {
  330. var posturl = biz.url("diypage/shop/adv/edit", null, modal.merch)
  331. } else {
  332. var posturl = biz.url("diypage/shop/adv/add", null, modal.merch)
  333. }
  334. $.post(posturl, {id: modal.id, advs: modal.advs}, function (ret) {
  335. if (ret.status == 0) {
  336. tip.msgbox.err(ret.result.message);
  337. $(".btn-save").text("保存广告").data("status", 0);
  338. return
  339. }
  340. tip.msgbox.suc("保存成功!");
  341. $(".btn-save").text("保存广告").data("status", 0);
  342. var menuid = ret.result.id;
  343. if (menuid != modal.id) {
  344. location.href = ret.result.url + '&id=' + menuid
  345. }
  346. }, 'json')
  347. };
  348. return modal
  349. });