diy.menu.js 14 KB

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