PanelCanvas.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. //初始化计数器
  2. var PanelCanvas_num = 0;
  3. var PanelCanvas_z = 100;
  4. var f_canvas2 = function(top,left,width,height,index_z,value_map,P_css) {
  5. PanelCanvas_num += 1;
  6. if(value_map.length < 2){
  7. value_map = "{}"
  8. }
  9. var obj = {
  10. PanelCanvas_id : "PanelCanvas"+PanelCanvas_num,
  11. PanelCanvas_list : [],
  12. PanelCanvas_curNum : [],
  13. PanelCanvas_width : width,
  14. PanelCanvas_height : height,
  15. PanelCanvas_css:P_css,
  16. PanelCanvas_z:index_z,
  17. PanelCanvas_map:JSON.parse(value_map),
  18. }
  19. //初始化计数器
  20. var basic_num = PanelCanvas_num*1000;
  21. var num = 0;
  22. console.log("obj.PanelCanvas_id:",obj.PanelCanvas_id)
  23. var PanelCanvas_P = $("<div onclick=\"V_PanelCanvas_id = \'"+obj.PanelCanvas_id+"\'\" id=\""+obj.PanelCanvas_id+"\" " +
  24. // " ondragenter =\"f_ondragenter(\'"+obj.PanelCanvas_id+"\')\" " +
  25. // " ondragover =\" f_ondragover(\'"+obj.PanelCanvas_id+"\') \" " +
  26. // " ondragleave =\"f_ondragleave(\'"+obj.PanelCanvas_id+"\')\" " +
  27. // " onmouseover =\"f_PanelCanvas_onmouseover()\" " +
  28. // " onmouseout =\"f_PanelCanvas_onmouseout()\" " +
  29. " class='PanelCanvas_div' style=\"left: "+left+"px;top: "+top+"px;z-index: "+obj.PanelCanvas_z+";width: "+width+"px;height: "+height+"px;" +
  30. " \">" +
  31. "</div>").appendTo("#PanelCanvas");
  32. console.log(PanelCanvas_P)
  33. // 更新 css 样式
  34. f_Up_css(obj.PanelCanvas_css,obj.PanelCanvas_id)
  35. // obj.PanelCanvas_id
  36. var pos = PanelCanvas_P.position();
  37. //加载layer拓展
  38. layer.config({
  39. extend: 'extend/layer.ext.js'
  40. });
  41. //右键菜单参数
  42. context.init({
  43. fadeSpeed: 100,
  44. filter: function ($obj) {},
  45. above: 'auto',
  46. preventDoubleContext: true,
  47. compress: false
  48. });
  49. //创建右键菜单
  50. if(!PanelCanvas_lock){
  51. context.attach('#'+obj.PanelCanvas_id, [
  52. {header: '操作'},
  53. {
  54. text: '数据源', action: function (e) {
  55. e.preventDefault();
  56. console.log("map_:",obj.PanelCanvas_id,obj.PanelCanvas_map)
  57. layer.opendynamicparameter({
  58. value: obj.PanelCanvas_map,
  59. value_map: obj.PanelCanvas_map
  60. }, function (value, index, elem) {
  61. console.log(value)
  62. // <!--data {"sn_sn":"866222050911984","tab_tab":"v_do_2","text1_text":"替换的文本"} data-->
  63. obj.PanelCanvas_map = JSON.parse(JSON.stringify(value))
  64. // 替换 所有 组件
  65. for (var i=0;i<obj.PanelCanvas_list.length;i++){
  66. console.log("替换 所有 组件:",i,obj.PanelCanvas_list[i],obj.PanelCanvas_curNum[i])
  67. sts_html = f_html_replace(obj,obj.PanelCanvas_list[i],obj.PanelCanvas_curNum[i])
  68. console.log("最终 HTML:",sts_html)
  69. $('.box[rel=' + obj.PanelCanvas_curNum[i] + '] .PanelCanvas_text').html(sts_html)
  70. }
  71. layer.close(index);
  72. });
  73. }
  74. },
  75. {divider: true},
  76. {
  77. text: '上一层', action: function (e) {
  78. e.preventDefault();
  79. console.log("obj.PanelCanvas_id:",PanelCanvas_P)
  80. PanelCanvas_P.css('z-index', '+=1');
  81. obj.PanelCanvas_z = parseInt(PanelCanvas_P.css('z-index'));
  82. }
  83. },
  84. {
  85. text: '当前层->', action: function (e) {
  86. e.preventDefault();
  87. layer.prompt({
  88. title: '请输入层 (数值越高在上面)',
  89. formType: 0,
  90. value: obj.PanelCanvas_z
  91. }, function (value, index, elem) {
  92. PanelCanvas_P.css('z-index', parseInt(value));
  93. obj.PanelCanvas_z = parseInt(PanelCanvas_P.css('z-index'));
  94. layer.close(Index);
  95. });
  96. }
  97. },
  98. {
  99. text: '下一层', action: function (e) {
  100. e.preventDefault();
  101. console.log("obj.PanelCanvas_id:",PanelCanvas_P)
  102. PanelCanvas_P.css('z-index', '-=1');
  103. obj.PanelCanvas_z = parseInt(PanelCanvas_P.css('z-index'));
  104. }
  105. },
  106. {
  107. text: '更改当前层尺寸', action: function (e) {
  108. e.preventDefault();
  109. layer.prompt({
  110. title: '请输入区域尺寸(宽,高),最小值:2',
  111. formType: 0,
  112. value: Math.round($('#'+obj.PanelCanvas_id).width()) + "," + Math.round($('#'+obj.PanelCanvas_id).height())
  113. }, function (value, index, elem) {
  114. var reg = /^[0-9]+,[0-9]+$/;
  115. if (!reg.test(value)) {
  116. alert('输入格式不正确,请输入整数 如:100,100');
  117. return;
  118. }
  119. var size = value.split(',');
  120. var box = $('#'+obj.PanelCanvas_id);
  121. box.css({
  122. width: size[0] < 2 ? 2 : size[0],
  123. height: size[1] < 2 ? 2 : size[1]
  124. });
  125. obj.PanelCanvas_width = size[0];
  126. obj.PanelCanvas_height = size[1];
  127. layer.close(Index);
  128. });
  129. }
  130. },
  131. {
  132. text: '删除区域', action: function (e) {
  133. e.preventDefault();
  134. PanelCanvas_P.remove();
  135. V_PanelCanvas_Map.delete(obj.PanelCanvas_id)
  136. }
  137. },
  138. {divider: true},
  139. {
  140. text: 'CSS样式', action: function (e) {
  141. e.preventDefault();
  142. $("#open_layer_css_layer_textarea").val(obj.PanelCanvas_css)
  143. f_attribute_css_inti() // 初始化 CSS 自定义样式
  144. console.log("PanelCanvas_css:",obj)
  145. layer.open({
  146. title: '自定义 CSS样式',
  147. type: 1,
  148. area: ['577px','690px'],
  149. fix: true, //不固定
  150. maxmin: false,
  151. content: $('#open_layer_css_layer'),
  152. shadeClose: true,
  153. anim: 1,
  154. shade: 0.4,
  155. isOutAnim: false,
  156. scrollbar: false,
  157. btn:["确定"],
  158. yes:function (Index) {
  159. obj.PanelCanvas_css = $("#open_layer_css_layer_textarea").val()
  160. // console.log("open_layer_css_layer_textarea:",obj.PanelCanvas_css)
  161. // // 更新 css 样式
  162. f_Up_css(obj.PanelCanvas_css,obj.PanelCanvas_id)
  163. layer.close(Index);
  164. }
  165. });
  166. }
  167. },
  168. {divider: true},
  169. {header: '高级'},
  170. {
  171. text: '自定义组件', action: function (e) {
  172. e.preventDefault();
  173. layer.prompt({
  174. title: '自定义 HTML',
  175. formType: 2,
  176. value: ""
  177. }, function (value, Index, elem) {
  178. value_json = {text:value, rotate: 0, color: "rgb(105,209,255)", height: 100, width: 100, pageX: 29, pageY: 64}
  179. obj.F_CreateBox(value_json)
  180. layer.close(Index);
  181. });
  182. }
  183. },
  184. {
  185. text: '保存为面板', action: function (e) {
  186. e.preventDefault();
  187. f_Save_layer(obj)
  188. }
  189. },
  190. {
  191. text: '新增数据源', action: function (e) {
  192. e.preventDefault();
  193. layer.prompt({
  194. title: '请输入 变量名',
  195. formType: 0,
  196. value: ""
  197. }, function (value, index, elem) {
  198. console.log("新增数据源:",value)
  199. obj.PanelCanvas_map[value] = ""
  200. layer.close(index);
  201. });
  202. }
  203. },
  204. ]);
  205. }
  206. // 添加组件
  207. obj.F_CreateBox = function (data) {
  208. var html = data.text || '';
  209. var rotate = data.rotate || '';
  210. var color = data.color || '';
  211. var height = data.height || 0;
  212. var width = data.width || 0;
  213. var pageX = data.pageX || 0;
  214. var pageY = data.pageY || 0;
  215. console.log("obj.PanelCanvas_id:",obj.PanelCanvas_id)
  216. //更新计数器并记录当前计数
  217. var curNum = basic_num + num;
  218. //创建区域块
  219. console.log("box:",curNum)
  220. coor = ""
  221. if(!PanelCanvas_lock){
  222. coor = "<div class=\"coor transparent\"></div>"
  223. }
  224. //加入到数据缓存 列表
  225. obj.PanelCanvas_list.push(html)
  226. // 组件 内容解析 替换
  227. sts_html = f_html_replace(obj,html,curNum)
  228. obj.PanelCanvas_curNum.push(curNum)
  229. // 增加 组件
  230. var box = $('<div id="rel_' + curNum + '" class="box PanelCanvas_rel_div" rel="' + curNum + '" num="' + num + '" style="user-select: none;transform:rotate('+rotate+'deg);overflow:hidden;" >' +
  231. '<div class="PanelCanvas_text" style="text-align: center;position: absolute;z-index: 99;width: 100%;height: 100%"> ' + sts_html + ' </div>' +
  232. '<div class="bg transparent" style="background-color:' + color + '"></div> ' +
  233. ''+coor+'' +
  234. '</div>').css({
  235. width: width,
  236. height: height,
  237. top: pageY > 0 ? pageY : (pos.top > 0 ? 0 : pos.top * -1 + 50),
  238. left: pageX > 0 ? pageX : (pos.left > 0 ? 0 : pos.left * -1 + 30)
  239. }).appendTo("#"+obj.PanelCanvas_id);
  240. $("#"+obj.PanelCanvas_id).trigger("create");
  241. //
  242. // //计算文本位置
  243. // box.find('.content').css({
  244. // marginLeft: box.find('.content').width() / 2 * -1,
  245. // marginTop: box.find('.content').height() / 2 * -1
  246. // });
  247. //创建右键菜单
  248. if(!PanelCanvas_lock){
  249. context.attach('.box[rel=' + curNum + ']', [
  250. {header: '操作'},
  251. {
  252. text: '数据源', action: function (e) {
  253. e.preventDefault();
  254. num_ = $('.box[rel=' + curNum + ']').attr("num")
  255. console.log("num_:",num_,obj.PanelCanvas_list[num_])
  256. sts_html = obj.PanelCanvas_list[num_]
  257. if(sts_html.indexOf("<!--data") != 0){
  258. return
  259. }
  260. str = sts_html.substring(sts_html.indexOf("<!--data") + 8,sts_html.indexOf("data-->"))
  261. console.log(str)
  262. str_json = JSON.parse(str)
  263. opendynamicparameterID = layer.opendynamicparameter({
  264. value: str_json,
  265. value_map: obj.PanelCanvas_map
  266. }, function (value, index, elem) {
  267. console.log(value)
  268. // <!--data {"sn_sn":"866222050911984","tab_tab":"v_do_2","text1_text":"替换的文本"} data-->
  269. json_v = JSON.stringify(value)
  270. json_v = "<!--data "+json_v+" data-->"
  271. console.log(json_v)
  272. num_ = $('.box[rel=' + curNum + ']').attr("num")
  273. json_split = obj.PanelCanvas_list[num_].split("\n")
  274. json_split[0] = json_v
  275. json_str = json_split.join("\n")
  276. console.log(json_str)
  277. // 组件 内容解析 替换
  278. obj.PanelCanvas_list[num_] = json_str
  279. sts_html = f_html_replace(obj,json_str,curNum)
  280. console.log(sts_html)
  281. $('.box[rel=' + curNum + '] .PanelCanvas_text').html(sts_html)
  282. console.log(index)
  283. layer.close(index);
  284. });
  285. console.log("opendynamicparameterID layer:",opendynamicparameterID)
  286. }
  287. },
  288. {
  289. text: '更改区域尺寸', action: function (e) {
  290. e.preventDefault();
  291. layer.prompt({
  292. title: '请输入区域尺寸(宽,高),最小值:2',
  293. formType: 0,
  294. value: Math.round($('.box[rel=' + curNum + ']').width()) + "," + Math.round($('.box[rel=' + curNum + ']').height())
  295. }, function (value, index, elem) {
  296. var reg = /^[0-9]+,[0-9]+$/;
  297. if (!reg.test(value)) {
  298. alert('输入格式不正确,例:100,200');
  299. return;
  300. }
  301. var size = value.split(',');
  302. var box = $('.box[rel=' + curNum + ']');
  303. box.css({
  304. width: size[0] < 2 ? 2 : size[0],
  305. height: size[1] < 2 ? 2 : size[1]
  306. });
  307. layer.close(index);
  308. });
  309. }
  310. },
  311. {
  312. text: '当前旋转->', action: function (e) {
  313. e.preventDefault();
  314. var deg = $('.box[rel=' + curNum + ']').css("transform")
  315. if(deg == "none"){
  316. deg = 0
  317. }else {
  318. deg = eval(''+$('.box[rel=' + curNum + ']').css('transform')+"");//构造getmatrix函数,返回上次旋转度数
  319. }
  320. console.log(deg)
  321. layer.prompt({
  322. title: '请输入旋转角度 (单位:度)',
  323. formType: 0,
  324. value: deg
  325. }, function (value, index, elem) {
  326. $('.box[rel=' + curNum + ']').css('transform', 'rotate('+value+'deg)');
  327. // layer.close(Index);
  328. });
  329. }
  330. },
  331. {
  332. text: '删除区域', action: function (e) {
  333. e.preventDefault();
  334. $('.box[rel=' + curNum + ']').remove();
  335. }
  336. },
  337. {divider: true},
  338. {header: '更改颜色'},
  339. {
  340. text: '透明', action: function (e) {
  341. e.preventDefault();
  342. $('.box[rel=' + curNum + '] .bg').css('background-color', 'transparent');
  343. }
  344. },
  345. {
  346. text: '自定义', action: function (e) {
  347. // e.preventDefault();
  348. // $('.box[rel=' + curNum + '] .bg')
  349. f_assembly_bcolor($('.box[rel=' + curNum + '] .bg'))
  350. }
  351. }
  352. ]);
  353. }else {
  354. // context.attach('.box[rel=' + curNum + ']', [
  355. // {header: '操作 '},
  356. // {
  357. // text: '编辑区域说明', action: function (e) {
  358. // e.preventDefault();
  359. // layer.prompt({
  360. // title: '请输入区域说明',
  361. // formType: 2,
  362. // value: $('.box[rel=' + curNum + '] .content').text()
  363. // }, function (value, index, elem) {
  364. // layer.close(Index);
  365. // var curCont = $('.box[rel=' + curNum + '] .content');
  366. // curCont.text(value).css({
  367. // marginLeft: curCont.width() / 2 * -1,
  368. // marginTop: curCont.height() / 2 * -1
  369. // });
  370. // });
  371. // }
  372. // },
  373. // ]);
  374. }
  375. num++; // 编号 ++
  376. }
  377. //获取所有区块
  378. obj.F_get_box_json = function () {
  379. bimg = ""
  380. console.log($("#"+obj.PanelCanvas_id).css('background-color'))
  381. console.log($("#"+obj.PanelCanvas_id+"-img").attr("src"))
  382. if($("#"+obj.PanelCanvas_id+"-img").attr("src") != undefined){
  383. bimg = $("#"+obj.PanelCanvas_id+"-img").attr("src")
  384. }else {
  385. bimg = $("#"+obj.PanelCanvas_id).css('background-color')
  386. }
  387. PanelCanvas_top = $("#"+obj.PanelCanvas_id).css("top")
  388. PanelCanvas_left = $("#"+obj.PanelCanvas_id).css("left")
  389. var data = {
  390. PanelCanvas_top : parseInt(PanelCanvas_top.substring(0, PanelCanvas_top.length - 2)),
  391. PanelCanvas_left : parseInt(PanelCanvas_left.substring(0, PanelCanvas_left.length - 2)),
  392. PanelCanvas_width : obj.PanelCanvas_width,
  393. PanelCanvas_height : obj.PanelCanvas_height,
  394. PanelCanvas_z: obj.PanelCanvas_z,
  395. PanelCanvas_map: obj.PanelCanvas_map,
  396. PanelCanvas_css: obj.PanelCanvas_css,
  397. Data_list : []
  398. };
  399. console.log("obj.PanelCanvas_id:",obj.PanelCanvas_id)
  400. $('#'+ obj.PanelCanvas_id +' .box').each(function () {
  401. var box = {};
  402. box['rotate'] = 0;
  403. console.log($(this).css('transform'))
  404. if($(this).css('transform') != "none"){
  405. box['rotate'] = eval(''+$(this).css('transform'));
  406. }
  407. // box['text'] = $(this).find('.PanelCanvas_text').html();
  408. box['text'] = obj.PanelCanvas_list[$(this).attr("num")] ;
  409. box['color'] = $(this).find('.bg').css('background-color');
  410. box['height'] = $(this).height();
  411. box['width'] = $(this).width();
  412. box['pageX'] = parseFloat($(this).css("left")) ;
  413. box['pageY'] = parseFloat($(this).css("top"));
  414. // console.dir(box);
  415. data.Data_list.push(box);
  416. });
  417. return data
  418. }
  419. //创建拖拽方法
  420. if(!PanelCanvas_lock){
  421. $("#"+obj.PanelCanvas_id).mousedown(function(e){
  422. console.log("111")
  423. console.log(e)
  424. var canvas = $(this);
  425. e.preventDefault();
  426. var pos = $(this).position();
  427. this.posix = {'x': e.pageX - pos.left, 'y': e.pageY - pos.top};
  428. $.extend(document, {'move': true, 'move_target': this, 'call_down' : function(e, posix){
  429. // console.log("posix:",e, posix)
  430. canvas.css({
  431. 'cursor': 'move',
  432. 'top': e.pageY - posix.y,
  433. 'left': e.pageX - posix.x
  434. });
  435. }, 'call_up' : function(){
  436. canvas.css('cursor', 'default');
  437. }});
  438. }).on('mousedown', '.box', function(e) {
  439. console.log("222 .box")
  440. if(PanelCanvas_lock) return;
  441. var pos = $(this).position();
  442. this.posix = {'x': e.pageX - parseFloat($(this).css("left")), 'y': e.pageY - parseFloat($(this).css("top"))};
  443. $.extend(document, {'move': true, 'move_target': this});
  444. e.stopPropagation();
  445. }).on('mousedown', '.box .coor', function(e) {
  446. console.log("333 .box .coor")
  447. var $box = $(this).parent();
  448. var posix = {
  449. 'w': $box.width(),
  450. 'h': $box.height(),
  451. 'x': e.pageX,
  452. 'y': e.pageY
  453. };
  454. $.extend(document, {'move': true, 'call_down': function(e) {
  455. $box.css({
  456. 'width': Math.max(10, e.pageX - posix.x + posix.w),
  457. 'height': Math.max(10, e.pageY - posix.y + posix.h)
  458. });
  459. }});
  460. e.stopPropagation();
  461. });
  462. }
  463. return obj
  464. };
  465. // 替换 函数
  466. function f_html_replace(obj,html,curNum) {
  467. var sts_html = html
  468. // 替换 全局ID
  469. reg = new RegExp("\\"+"[{ID}]","g");
  470. sts_html = sts_html.replace(reg,curNum);
  471. // 替换 全局 Tokey
  472. reg = new RegExp("\\"+"[{USER_UUID}]","g");
  473. sts_html = sts_html.replace(reg,USER_UUID);
  474. // 替换 全局 OSS 资源地址
  475. reg = new RegExp("\\"+"[{OSS_Static}]","g");
  476. sts_html = sts_html.replace(reg,OSS_Static);
  477. // 替换 全局变量
  478. for(var key in obj.PanelCanvas_map){
  479. console.log(key+':'+obj.PanelCanvas_map[key]);
  480. key_ = key
  481. if(key.charAt(0) == '_'){
  482. key_ = key_.substring(1,key.length-1)
  483. }
  484. key_list = key_.split("_");
  485. reg = new RegExp("\\"+"[-{"+key_list[0]+"}-]","g");
  486. sts_html = sts_html.replace(reg,obj.PanelCanvas_map[key]);
  487. }
  488. if(sts_html.indexOf("<!--data") == 0){
  489. str = sts_html.substring(sts_html.indexOf("<!--data") + 8,sts_html.indexOf("data-->"))
  490. console.log(str)
  491. str_json = JSON.parse(str)
  492. console.log("json:",str_json)
  493. for(var key in str_json){
  494. console.log(key+':'+str_json[key]);
  495. key_ = key
  496. if(key.charAt(0) == '_'){
  497. key_ = key_.substring(1,key.length-1)
  498. }
  499. key_list = key_.split("_");
  500. reg = new RegExp("\\"+"[{"+key_list[0]+"}]","g");
  501. sts_html = sts_html.replace(reg,str_json[key]);
  502. }
  503. }
  504. return sts_html
  505. }
  506. // 角度转换
  507. function matrix(a,b,c,d,e,f){
  508. var aa=Math.round(180*Math.asin(a)/ Math.PI);
  509. var bb=Math.round(180*Math.acos(b)/ Math.PI);
  510. var cc=Math.round(180*Math.asin(c)/ Math.PI);
  511. var dd=Math.round(180*Math.acos(d)/ Math.PI);
  512. var deg=0;
  513. if(aa==bb||-aa==bb){
  514. deg=dd;
  515. }else if(-aa+bb==180){
  516. deg=180+cc;
  517. }else if(aa+bb==180){
  518. deg=360-cc||360-dd;
  519. }
  520. return deg>=360?0:deg;
  521. //return (aa+','+bb+','+cc+','+dd);
  522. }
  523. function f_Up_css(css_str,div_id) {
  524. console.log("css_:",css_str,css_str)
  525. if(css_str == undefined) return;
  526. css_str = css_str.replace(/\n/g,"");
  527. var css_list = css_str.split(";");
  528. div_obj = $("#"+div_id)
  529. for (var i_css = 0;i_css < css_list.length;i_css++){
  530. css_ = css_list[i_css].split(" : ");
  531. console.log("css_:",css_list[i_css])
  532. if(css_.length == 2){
  533. console.log("css_add:",css_[0], css_[1])
  534. div_obj.css(css_[0], css_[1]);
  535. }
  536. }
  537. }