IntroductionFinancialUnion.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <div class="list">
  3. <div id="toolbar">
  4. <input type="button" value="添加区域" class="btn" id="btn_add" />
  5. <input type="button" value="锁定区域" class="btn" id="btn_lock" />
  6. <input type="button" value="获取数据" class="btn" id="btn_save" />
  7. </div>
  8. <div id="canvas">
  9. <img src="@/assets/img/demo_picture.jpg" />
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. // import {layer} from "../../assets/js/js/layer/layer.js"
  15. // import {drag} from "../../assets/js/js/drag.js"
  16. export default {
  17. name: 'IntroductionFinancialUnion',
  18. data () {
  19. return {
  20. }
  21. },
  22. mounted(){
  23. this.getInitData()
  24. },
  25. methods:{
  26. getInitData(){
  27. $(function(){
  28. //初始化计数器
  29. var num = 0;
  30. //区块锁定标识
  31. var lock = false;
  32. //加载layer拓展
  33. layer.config({
  34. extend: 'extend/layer.ext.js'
  35. });
  36. //右键菜单参数
  37. context.init({
  38. fadeSpeed: 100,
  39. filter: function ($obj){},
  40. above: 'auto',
  41. preventDoubleContext: true,
  42. compress: false
  43. });
  44. //调试输出方法
  45. function debug(msg){
  46. $("#debug").text(msg);
  47. }
  48. function createBox(data){
  49. var dataId = data.id || '';
  50. var value = data.text || '';
  51. var color = data.color || '';
  52. var height = data.height || 0;
  53. var width = data.width || 0;
  54. var pageX = data.pageX || 0;
  55. var pageY = data.pageY || 0;
  56. //更新计数器并记录当前计数
  57. var curNum = num++;
  58. //创建区域块
  59. var pos = $("#canvas").position();
  60. var box = $('<div class="box" rel="'+curNum+'" dataId="'+dataId+'"><pre class="content">'+value+'</pre><div class="bg transparent" style="background-color:'+color+'"></div><div class="coor transparent"></div></div>').css({
  61. width : width,
  62. height : height,
  63. top : pageY > 0 ? pageY : (pos.top > 0 ? 0 : pos.top * -1 + 50),
  64. left : pageX > 0 ? pageX : (pos.left > 0 ? 0 : pos.left * -1 + 30)
  65. }).appendTo("#canvas");
  66. //计算文本位置
  67. box.find('.content').css({
  68. marginLeft : box.find('.content').width()/2*-1,
  69. marginTop : box.find('.content').height()/2*-1
  70. });
  71. //创建右键菜单
  72. context.attach('.box[rel='+curNum+']', [
  73. {header: '操作'},
  74. {text: '编辑区域说明', action: function(e){
  75. e.preventDefault();
  76. layer.prompt({
  77. title: '请输入区域说明',
  78. formType: 2,
  79. value: $('.box[rel='+curNum+'] .content').text()
  80. }, function(value, index, elem){
  81. layer.close(index);
  82. var curCont = $('.box[rel='+curNum+'] .content');
  83. curCont.text(value).css({
  84. marginLeft : curCont.width()/2*-1,
  85. marginTop : curCont.height()/2*-1
  86. });
  87. });
  88. }
  89. },
  90. {text: '更改区域尺寸', action: function(e){
  91. e.preventDefault();
  92. layer.prompt({
  93. title: '请输入区域尺寸(宽,高),最小值:30',
  94. formType: 0,
  95. value: $('.box[rel='+curNum+']').width()+","+$('.box[rel='+curNum+']').height()
  96. }, function(value, index, elem){
  97. var reg = /^[0-9]+,[0-9]+$/;
  98. if(!reg.test(value)){
  99. alert('输入格式不正确,例:100,200');
  100. return;
  101. }
  102. var size = value.split(',');
  103. var box = $('.box[rel='+curNum+']');
  104. box.css({
  105. width : size[0] < 30 ? 30 : size[0],
  106. height : size[1] < 30 ? 30 : size[1]
  107. });
  108. layer.close(index);
  109. });
  110. }
  111. },
  112. {text: '删除区域', action: function(e){
  113. e.preventDefault();
  114. $('.box[rel='+curNum+']').remove();
  115. }
  116. },
  117. {divider: true},
  118. {header: '更改颜色'},
  119. {text: '<font color="orange">橙色</font>', action: function(e){
  120. e.preventDefault();
  121. $('.box[rel='+curNum+'] .bg').css('background-color', 'orange');
  122. }
  123. },
  124. {text: '<font color="red">红色</font>', action: function(e){
  125. e.preventDefault();
  126. $('.box[rel='+curNum+'] .bg').css('background-color', 'red');
  127. }
  128. },
  129. {text: '<font color="blue">蓝色</font>', action: function(e){
  130. e.preventDefault();
  131. $('.box[rel='+curNum+'] .bg').css('background-color', 'blue');
  132. }
  133. },
  134. {text: '<font color="green">绿色</font>', action: function(e){
  135. e.preventDefault();
  136. $('.box[rel='+curNum+'] .bg').css('background-color', 'green');
  137. }
  138. },
  139. {text: '<font color="purple">紫色</font>', action: function(e){
  140. e.preventDefault();
  141. $('.box[rel='+curNum+'] .bg').css('background-color', 'purple');
  142. }
  143. },
  144. {text: '<font color="yellow">黄色</font>', action: function(e){
  145. e.preventDefault();
  146. $('.box[rel='+curNum+'] .bg').css('background-color', 'yellow');
  147. }
  148. }
  149. ]);
  150. }
  151. //添加区域
  152. $("#btn_add").click(function(){
  153. //弹出区域说明输入框
  154. layer.prompt({
  155. title: '请输入区域说明',
  156. formType: 2 //0:input,1:password,2:textarea
  157. }, function(value, index, elem){
  158. layer.close(index);
  159. createBox({
  160. text : value,
  161. width : 200,
  162. height : 100
  163. });
  164. });
  165. });
  166. //锁定区域
  167. $('#btn_lock').click(function(){
  168. if(lock){
  169. $(this).val("锁定区域");
  170. lock = false;
  171. $('.box .coor').show();
  172. }else{
  173. $(this).val("解锁区域");
  174. lock = true;
  175. $('.box .coor').hide();
  176. }
  177. });
  178. //获取所有区块
  179. $('#btn_save').click(function(){
  180. var data = [];
  181. $('.box').each(function(){
  182. var box = {};
  183. box['id'] = $(this).attr('dataId');
  184. box['text'] = $(this).find('.content').text();
  185. box['color'] = $(this).find('.bg').css('background-color');
  186. box['height'] = $(this).height();
  187. box['width'] = $(this).width();
  188. box['pageX'] = $(this).position().left;
  189. box['pageY'] = $(this).position().top;
  190. console.dir(box);
  191. data.push(box);
  192. });
  193. });
  194. //创建拖拽方法
  195. $("#canvas").mousedown(function(e){
  196. var canvas = $(this);
  197. e.preventDefault();
  198. var pos = $(this).position();
  199. this.posix = {'x': e.pageX - pos.left, 'y': e.pageY - pos.top};
  200. $.extend(document, {'move': true, 'move_target': this, 'call_down' : function(e, posix){
  201. canvas.css({
  202. 'cursor': 'move',
  203. 'top': e.pageY - posix.y,
  204. 'left': e.pageX - posix.x
  205. });
  206. }, 'call_up' : function(){
  207. canvas.css('cursor', 'default');
  208. }});
  209. }).on('mousedown', '.box', function(e) {
  210. if(lock) return;
  211. var pos = $(this).position();
  212. this.posix = {'x': e.pageX - pos.left, 'y': e.pageY - pos.top};
  213. $.extend(document, {'move': true, 'move_target': this});
  214. e.stopPropagation();
  215. }).on('mousedown', '.box .coor', function(e) {
  216. var $box = $(this).parent();
  217. var posix = {
  218. 'w': $box.width(),
  219. 'h': $box.height(),
  220. 'x': e.pageX,
  221. 'y': e.pageY
  222. };
  223. $.extend(document, {'move': true, 'call_down': function(e) {
  224. $box.css({
  225. 'width': Math.max(30, e.pageX - posix.x + posix.w),
  226. 'height': Math.max(30, e.pageY - posix.y + posix.h)
  227. });
  228. }});
  229. e.stopPropagation();
  230. });
  231. //测试加载
  232. var loadData = [{id : 1001,text : "C16\n16.5",color : "rgb(255, 0, 0)",height : 70,width : 77,pageX : 627,pageY : 364},
  233. {id : 1002,text : "C17\n16.18",color : "rgb(255, 255, 0)",height : 70,width : 77,pageX : 709,pageY : 364},
  234. {id : 1003,text : "C18\n16.08",color : "rgb(128, 0, 128)",height : 70,width : 77,pageX : 790,pageY : 364},
  235. {id : 1004,text : "C19\n16.08",color : "rgb(0, 128, 0)",height : 70,width : 77,pageX : 870,pageY : 364},
  236. {id : 1005,text : "C20\n16.5",color : "rgb(0, 0, 255)",height : 70,width : 77,pageX : 627,pageY : 439},
  237. {id : 1006,text : "C21\n16.18",color : "rgb(255, 165, 0)",height : 70,width : 77,pageX : 709,pageY : 439},
  238. {id : 1007,text : "C22\n16.08",color : "rgb(255, 165, 0)",height : 70,width : 77,pageX : 870,pageY : 439},
  239. {id : 1008,text : "C23\n16.08",color : "rgb(255, 165, 0)",height : 70,width : 77,pageX : 789,pageY : 439}];
  240. $.each(loadData, function(i, row){
  241. createBox(row);
  242. });
  243. });
  244. }
  245. }
  246. }
  247. </script>
  248. <style scoped>
  249. html,body{margin:0;padding:0;font:14px/1.5em simsun;overflow:hidden;}
  250. #canvas{position:absolute;left:0px;top:0px;z-index:9;border:2px dashed #ccc;padding:10px;background:#fff;}
  251. .transparent{filter:alpha(opacity=50);-moz-opacity:0.5;-khtml-opacity:0.5;opacity:0.5;}
  252. .box{width:200px;height:100px;cursor:move;position:absolute;top:30px;left:30px;z-index:99;}
  253. .box .bg{width:100%;height:100%;background-color:orange;}
  254. .box .coor{width:10px;height:10px;overflow:hidden;cursor:se-resize;position:absolute;right:0;bottom:0;background-color:red;}
  255. .box .content{position:absolute;left:50%;top:50%;z-index:99;text-align:center;font:bold 14px/1.5em simsun;}
  256. #debug{position:absolute;right:10px;top:10px;z-index:88;border:1px solid #ccc;width:100px;height:100px;background:#fff;}
  257. #toolbar{position:absolute;left:10px;top:10px;z-index:88;}
  258. </style>