diy.danmu.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. define(['jquery.ui'], function (ui) {
  2. var modal = {
  3. default: {
  4. params: {
  5. 'isopen': '0',
  6. 'style': '0',
  7. 'datatype': '0',
  8. 'starttime': '5',
  9. 'endtime': '60'
  10. },
  11. style: {
  12. 'background': '#000000',
  13. 'color': '#ffffff',
  14. 'opacity': '0.7'
  15. },
  16. data: [
  17. {
  18. 'imgurl': '../addons/ewei_shopv2/static/images/nopic100.jpg',
  19. 'nickname': '用户昵称',
  20. 'time': '5'
  21. }
  22. ]
  23. }
  24. };
  25. modal.init = function (params) {
  26. window.tpl = params.tpl;
  27. modal.attachurl = params.attachurl;
  28. modal.danmu = params.danmu;
  29. modal.merch = params.merch;
  30. if (!modal.danmu) {
  31. modal.danmu = modal.default
  32. }
  33. tpl.helper("imgsrc", function (src) {
  34. if (typeof src != 'string') {
  35. return ''
  36. }
  37. if (src.indexOf('http://') == 0 || src.indexOf('https://') == 0 || src.indexOf('../addons') == 0) {
  38. return src
  39. } else if (src.indexOf('images/') == 0) {
  40. return modal.attachurl + src
  41. }
  42. });
  43. modal.initItems();
  44. modal.initEditor();
  45. $(".btn-save").unbind('click').click(function () {
  46. var status = $(this).data('status');
  47. if (status) {
  48. tip.msgbox.err("正在保存,请稍候。。。");
  49. return
  50. }
  51. modal.save()
  52. });
  53. };
  54. modal.initItems = function () {
  55. var danmu = modal.danmu;
  56. danmu.merch = modal.merch;
  57. var html = tpl("tpl_show_danmu", modal.danmu);
  58. $("#phone").html(html).show();
  59. };
  60. modal.initSortable = function () {
  61. $(".diy-editor .form-items .inner").sortable({
  62. opacity: 0.8,
  63. placeholder: "highlight",
  64. items: '.item',
  65. revert: 100,
  66. scroll: false,
  67. cancel: '.goods-selector,input,select,.btn,.btn-del,.three',
  68. start: function (event, ui) {
  69. var height = ui.item.height();
  70. $(".highlight").css({"height": height + 22 + "px"});
  71. $(".highlight").html('<div><i class="fa fa-plus"></i> 放置此处</div>');
  72. $(".highlight div").css({"line-height": height + 16 + "px"})
  73. },
  74. update: function (event, ui) {
  75. var childType = ui.item.closest(".form-items").data('type');
  76. modal.sortChildItems(childType)
  77. }
  78. })
  79. };
  80. modal.sortChildItems = function () {
  81. var newArr = [];
  82. $("#form-items .item").each(function (i) {
  83. var index = $(this).data('index');
  84. var item = modal.danmu.data[index];
  85. if(item){
  86. newArr[i] = item;
  87. }
  88. });
  89. modal.danmu.data = newArr;
  90. modal.initItems();
  91. modal.initEditor()
  92. };
  93. modal.initEditor = function () {
  94. var html = tpl("tpl_edit_danmu", modal.danmu);
  95. $("#diy-editor .inner").html(html);
  96. $("#diy-editor .slider").each(function () {
  97. var decimal = $(this).data('decimal');
  98. var multiply = $(this).data('multiply');
  99. var defaultValue = $(this).data("value");
  100. if (decimal) {
  101. defaultValue = defaultValue * decimal
  102. }
  103. $(this).slider({
  104. slide: function (event, ui) {
  105. var sliderValue = ui.value;
  106. if (decimal) {
  107. sliderValue = sliderValue / decimal
  108. }
  109. $(this).siblings(".input").val(sliderValue).trigger("propertychange");
  110. $(this).siblings(".count").find("span").text(sliderValue)
  111. }, value: defaultValue, min: $(this).data("min"), max: $(this).data("max")
  112. })
  113. });
  114. $("#diy-editor").find(".diy-bind").bind('input propertychange change', function () {
  115. var _this = $(this);
  116. var bind = _this.data("bind");
  117. var bindchild = _this.data('bind-child');
  118. var bindparent = _this.data('bind-parent');
  119. var bindthree = _this.data('bind-three');
  120. var initEditor = _this.data('bind-init');
  121. var value = '';
  122. var tag = this.tagName;
  123. if (tag == 'INPUT') {
  124. var placeholder = _this.data('placeholder');
  125. value = _this.val();
  126. value = value == '' ? placeholder : value
  127. } else if (tag == 'SELECT') {
  128. value = _this.find('option:selected').val()
  129. } else if (tag == 'TEXTAREA') {
  130. value = _this.val()
  131. }
  132. value = $.trim(value);
  133. if (bindchild) {
  134. if (bindparent || bindparent==0) {
  135. if (bindthree) {
  136. modal.danmu[bindchild][bindparent].child[bindthree][bind] = value
  137. } else {
  138. modal.danmu[bindchild][bindparent][bind] = value
  139. }
  140. } else {
  141. modal.danmu[bindchild][bind] = value
  142. }
  143. } else {
  144. modal.danmu[bind] = value
  145. }
  146. modal.initItems();
  147. if (initEditor) {
  148. modal.initEditor()
  149. }
  150. });
  151. $("#phone").mouseenter(function () {
  152. $("#diy-editor").find('.diy-bind').blur()
  153. });
  154. $("#addChild").unbind('click').click(function () {
  155. var max = $(this).closest(".form-items").data('max');
  156. if(max){
  157. if(modal.danmu.data.length>=max){
  158. tip.msgbox.err("最大添加 "+max+" 个!");
  159. return
  160. }
  161. }
  162. var newChild = $.extend(true, {}, modal.default.data[0]);
  163. modal.danmu.data.push(newChild);
  164. modal.initItems();
  165. modal.initEditor();
  166. });
  167. $("#diy-editor .form-items .item .btn-del").unbind('click').click(function () {
  168. var index = $(this).closest(".item").data('index');
  169. var min = $(this).closest(".form-items").data("min");
  170. if (min) {
  171. if(!modal.danmu.data){
  172. modal.danmu.data = [];
  173. }
  174. var length = modal.danmu.data.length;
  175. if (length <= min) {
  176. tip.msgbox.err("至少保留 " + min + " 个!");
  177. return
  178. }
  179. }
  180. tip.confirm("确定删除吗", function () {
  181. delete modal.danmu.data.splice(index, 1);
  182. modal.initItems();
  183. modal.initEditor()
  184. })
  185. });
  186. $("#diy-editor").show();
  187. modal.initSortable();
  188. };
  189. modal.save = function () {
  190. if (!modal.danmu) {
  191. tip.msgbox.err("数据错误,请刷新页面重试!");
  192. return
  193. }
  194. $(".btn-save").data('status', 1).text("保存中...");
  195. var posturl = biz.url("diypage/shop/danmu", null, modal.merch);
  196. $.post(posturl, {data: modal.danmu}, function (ret) {
  197. if (ret.status == 0) {
  198. tip.msgbox.err(ret.result.message);
  199. $(".btn-save").text("保存并设置").data("status", 0);
  200. return
  201. }
  202. tip.msgbox.suc("操作成功!");
  203. $(".btn-save").text("保存并设置").data("status", 0)
  204. }, 'json')
  205. };
  206. return modal
  207. });