diy-rubik-cube.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. // use common\helpers\ComponentHelper;
  3. // ComponentHelper::loadComponentView('components/style-components/diy-img-setting', __DIR__);
  4. ?>
  5. <template id="diy-rubik-cube">
  6. <div class="diy-rubik-cube">
  7. <div class="decorate-cube">
  8. <ul class="cube-col" v-for="(n,nIndex) in densityNum" :key="nIndex">
  9. <li :class="['cube-item', {'item-selecting': isSelecting(n,i), 'item-selected':isSelected(n,i)}]"
  10. v-for="(i,iIndex) in densityNum" :key="iIndex+100"
  11. :style="{'width':cubeCellWidth+'px','height':cubeCellHeight+'px'}"
  12. :data-x="n" :data-y="i"
  13. @click="onClickCubeItem($event)"
  14. @mouseenter="onEnterCubeItem($event)">
  15. <i class="el-icon-plus" :style="{'line-height':cubeCellHeight+'px'}"></i>
  16. </li>
  17. </ul>
  18. <div class="cube-selected" v-for="(item, index) in selectedList" :key="index + 100"
  19. :style="{'width':getSelectedWidth(item)+'px','height':getSelectedHeight(item)+'px','top':getSelectedTop(item)+'px','left':getSelectedLeft(item)+'px'}"
  20. :class="{'click-cube-selected': index == selCubeIndex}"
  21. @click="selCubeInfo(index)"
  22. >
  23. <div class="cube-selected-text" v-if="item.img == ''">
  24. {{ Math.round(cubeHeight/density*((parseInt(item.end.x) - parseInt(item.start.x) + 1))) }}
  25. x
  26. {{ Math.round(cubeWidth/density*((parseInt(item.end.y) - parseInt(item.start.y) + 1))) }}
  27. 像素
  28. </div>
  29. <el-image style="width:100%;height: 100%;" :src="item.img" fit="cover" v-else></el-image>
  30. <div class="cube-del-icon" @click.stop="delCubeList(index)" v-show="index == selCubeIndex && styleArr.length == 0">
  31. <i class="el-icon-error del-sel-icon"></i>
  32. </div>
  33. </div>
  34. </div>
  35. <div :style="{'width':cubeExhibitWidth+'px','height':cubeExhibitHeight+'px'}"></div>
  36. <diy-img-setting
  37. v-show="selCubeIndex != -1"
  38. :img-infos="cubeImgInfo"
  39. def-img-count="1"
  40. :suggest-site="99"
  41. suggest-size-text=""
  42. :img-contain-style="{padding: 0,'margin-top': '20px'}"
  43. :show-title="false"
  44. @change="addCubeImg"
  45. ></diy-img-setting>
  46. </div>
  47. </template>
  48. <script>
  49. Vue.component('diy-rubik-cube', {
  50. template: '#diy-rubik-cube',
  51. name: 'diy-rubik-cube',
  52. props: {
  53. density: {//密度
  54. type: [String,Number],
  55. default: 4
  56. },
  57. cubeWidth: { //魔方宽度
  58. type: [String,Number],
  59. default: 750
  60. },
  61. cubeHeight: { //魔方高度
  62. type: [String,Number],
  63. default: 750
  64. },
  65. cubeExhibitWidth: { //魔方展示宽度。
  66. type: [String,Number],
  67. default: 320
  68. },
  69. cubeExhibitHeight: { //魔方展示宽度。
  70. type: [String,Number],
  71. default: 320
  72. },
  73. cubeList: { //魔方数据
  74. type: Array,
  75. default: () => {
  76. return [];
  77. }
  78. },
  79. styleArr: { //风格样式
  80. type: Array,
  81. default: () => {
  82. return [];
  83. }
  84. },
  85. styleBoo: { //用于装修切回去数据丢失问题
  86. type: Boolean,
  87. default: false,
  88. }
  89. },
  90. data() {
  91. return {
  92. selectingItem: {
  93. 'tempStart': null,
  94. 'tempEnd': null,
  95. 'start': null,
  96. 'end': null
  97. },
  98. selectedList: [], //已经生成的单元。
  99. selCubeIndex: -1,
  100. addGridArr: [], // 保存每个选中的格子
  101. cubeImgInfo: [], //图片信息
  102. maxHeight: 0,//魔方以选中的最高高度
  103. }
  104. },
  105. watch: {
  106. cubeList: {
  107. deep:true,
  108. immediate: true,
  109. handler(val) {
  110. this.selectedList = val;
  111. this.addGridArr = [];
  112. let that = this;
  113. this.selectedList.forEach(v => {
  114. let gridData = that.getGridSeat(v.start,v.end);
  115. that.addGridArr.push(...gridData);
  116. })
  117. }
  118. },
  119. styleArr: {
  120. deep:true,
  121. immediate: true,
  122. handler(val) {
  123. if(!val.length) return;
  124. let that = this;
  125. this.addGridArr = [];
  126. let newList = [];
  127. val.forEach(v => {
  128. let gridData = this.getGridSeat(v.start,v.end);
  129. let dataObj = {
  130. 'start': v.start,
  131. 'end': v.end,
  132. 'img': '',
  133. 'url': '',
  134. 'originalData': [],// 图片组件传过来的数据
  135. }
  136. // +判断是否为切回来的
  137. if(this.selectedList.length > 0 && this.styleBoo){
  138. let dataIndex = this.selectedList.findIndex(listVal => listVal.start.x == v.start.x && listVal.start.y == v.start.y && listVal.end.x == v.end.x && listVal.end.y == v.end.y);
  139. if(dataIndex != -1){
  140. dataObj = this.selectedList[dataIndex];
  141. }
  142. console.info(dataIndex);
  143. }
  144. let style = that.getCubeStyle(v.start,v.end);
  145. that.$set(dataObj,"style",style);
  146. that.addGridArr.push(...gridData);
  147. newList.push(dataObj);
  148. })
  149. this.selCubeIndex = -1;
  150. this.cubeImgInfo = [];
  151. this.selectedList = newList;
  152. this.getCubeMaxHeight();
  153. this.updateData();
  154. }
  155. }
  156. },
  157. computed: {
  158. //密度值。
  159. densityNum: function () {
  160. return parseInt(this.density);
  161. },
  162. //单元魔方高度。
  163. cubeCellHeight: function () {
  164. return this.cubeExhibitHeight / this.density;
  165. },
  166. //单元魔方宽度。
  167. cubeCellWidth: function () {
  168. return this.cubeExhibitWidth / this.density;
  169. },
  170. },
  171. methods: {
  172. //获取开始和结束之间的所有魔方单元。
  173. updateSelecting() {
  174. var tempStart = this.selectingItem.tempStart;
  175. var tempEnd = this.selectingItem.tempEnd;
  176. this.selectingItem.start = {
  177. x: Math.min(tempStart.x, tempEnd.x),
  178. y: Math.min(tempStart.y, tempEnd.y)
  179. };
  180. this.selectingItem.end = {
  181. x: Math.max(tempStart.x, tempEnd.x),
  182. y: Math.max(tempStart.y, tempEnd.y)
  183. };
  184. },
  185. //清除正在选择的。
  186. clearSelecting() {
  187. this.selectingItem.tempStart = null;
  188. this.selectingItem.tempEnd = null;
  189. this.selectingItem.start = null;
  190. this.selectingItem.end = null;
  191. },
  192. // 获取鼠标移动的位置
  193. coordFromCubeEvent(event) {
  194. var el = event.currentTarget;
  195. var x = el.getAttribute('data-x');
  196. var y = el.getAttribute('data-y');
  197. // console.log('track_x_y_[' + x + ', ' + y + ']');
  198. return { x: x, y: y };
  199. },
  200. isContain(x, y, item) {
  201. return (item.start.x <= x && x <= item.end.x && item.start.y <= y && y <= item.end.y);
  202. },
  203. //魔方点击事件。
  204. onClickCubeItem: function (event) {
  205. var domclass = event.currentTarget.getAttribute('class');
  206. console.info(domclass);
  207. if (-1 !== domclass.indexOf('item-selected')) {
  208. console.log("已经被占用");
  209. return;
  210. }
  211. var coord = this.coordFromCubeEvent(event);
  212. // 开始点击
  213. if (null == this.selectingItem.tempStart) {
  214. this.selectingItem.tempStart = coord;
  215. this.selectingItem.tempEnd = coord;
  216. this.selectingItem.start = coord;
  217. this.selectingItem.end = coord;
  218. return;
  219. }
  220. this.selectingItem.tempEnd = coord;
  221. this.updateSelecting();
  222. // 添加以选中的格子
  223. let gridData = this.getGridSeat(this.selectingItem.start,this.selectingItem.end);
  224. // 用于判断格子是否被选中
  225. let boo = this.verifyGridExist(this.selectingItem.start,this.selectingItem.end);
  226. if(boo){
  227. return;
  228. }
  229. this.addGridArr.push(...gridData);
  230. this.getCubeMaxHeight();
  231. //加入选中的。
  232. var selectedItem = {
  233. 'start': this.selectingItem.start,
  234. 'end': this.selectingItem.end,
  235. 'img': '',
  236. 'url': '',
  237. 'originalData': [],// 图片组件传过来的数据
  238. }
  239. //计算选中层的宽度。
  240. let style = this.getCubeStyle(selectedItem.start,selectedItem.end);
  241. this.$set(selectedItem,"style",style);
  242. this.selectedList.push(selectedItem);
  243. this.selCubeInfo(this.selectedList.length - 1);
  244. // 清楚状态
  245. this.clearSelecting();
  246. this.updateData();
  247. },
  248. // 获取每个各种的位置 type: 1 添加 2:删除
  249. getGridSeat(start,end,type=1){
  250. let gridData = [];
  251. // 添加以选中的格子
  252. console.info(this.selectingItem);
  253. for(let i = start.x;i<=end.x;i++){
  254. let arr = [];
  255. for(let j = start.y;j<=end.y;j++){
  256. arr.push({
  257. x: i,
  258. y: j
  259. })
  260. }
  261. if(type == 1){
  262. gridData.push(...arr);
  263. } else {
  264. arr.forEach(val => {
  265. let startIndex = this.addGridArr.findIndex(v => (v.x == val.x && v.y == val.y));
  266. if(startIndex != -1){
  267. this.addGridArr.splice(startIndex,1)
  268. }
  269. })
  270. this.getCubeMaxHeight();
  271. }
  272. }
  273. return gridData;
  274. },
  275. // 判断是否在已选中的范围中
  276. verifyGridExist(start,end){
  277. let gridData = this.getGridSeat(start,end);
  278. // 用于判断格子是否被选中
  279. let mergeArr = [...this.addGridArr];
  280. mergeArr.push(...gridData);
  281. let delRepeatArr = [];
  282. let repeatCount = 0;
  283. mergeArr.forEach(function(a) {
  284. let boo = delRepeatArr.every(function(b) {
  285. return a.x != b.x || a.y != b.y;
  286. })
  287. boo ? delRepeatArr.push(a) : repeatCount++;
  288. })
  289. if(repeatCount > 0){
  290. this.$message.error('该位置无法选择');
  291. return true;
  292. }
  293. return false;
  294. },
  295. // 计算魔方已选中的最高高度
  296. getCubeMaxHeight(){
  297. let size = this.cubeWidth/this.density; //每个格子的大小
  298. let maxHeight = 0;
  299. let maxY = 0;
  300. for(let i = 1;i<=this.density;i++){
  301. let yInfo = this.addGridArr.filter(v => v.y == i);
  302. if(yInfo.length){
  303. let y = yInfo[yInfo.length -1].y;
  304. y>maxY?maxY = y:'';
  305. }
  306. }
  307. maxHeight = maxY * size;
  308. this.maxHeight = maxHeight;
  309. this.$emit("max",this.maxHeight);
  310. },
  311. // 计算魔方样式
  312. getCubeStyle(start,end){
  313. //计算选中层的宽度。
  314. let infoWidth = Math.round(this.cubeHeight/this.density*((parseInt(end.x) - parseInt(start.x) + 1)));
  315. let infoHeight = Math.round(this.cubeWidth/this.density*((parseInt(end.y) - parseInt(start.y) + 1)));
  316. let infoTop = Math.round(this.cubeWidth/this.density*((start.y - 1) ));
  317. let infoLeft = Math.round(this.cubeWidth/this.density*((start.x - 1)));
  318. let style = {
  319. top: (infoTop/2) + 'px',
  320. left: (infoLeft/2) + 'px',
  321. width: (infoWidth/2) + 'px',
  322. height: (infoHeight/2) + 'px',
  323. }
  324. return style;
  325. },
  326. // 鼠标移动触发
  327. onEnterCubeItem: function (event) {
  328. // console.log("track_onEnterCube");
  329. if (this.selectingItem.tempStart) {
  330. var coord = this.coordFromCubeEvent(event);
  331. this.selectingItem.tempEnd = coord;
  332. // console.info(this.selectingItem.tempEnd);
  333. this.updateSelecting();
  334. }
  335. },
  336. //判断是否正在选择
  337. isSelecting: function (x, y) {
  338. var item = this.selectingItem;
  339. if (item.tempStart) {
  340. return this.isContain(x, y, item);
  341. }
  342. return false;
  343. },
  344. //判断是否已经选择。
  345. isSelected: function (x, y) {
  346. var list = this.selectedList;
  347. for (var i = 0; i < list.length; i++) {
  348. if (this.isContain(x, y, list[i])) {
  349. return true;
  350. }
  351. }
  352. return false;
  353. },
  354. // 选中
  355. selCubeInfo(index){
  356. this.selCubeIndex = index;
  357. if(this.selectedList[this.selCubeIndex]){
  358. this.cubeImgInfo = this.selectedList[this.selCubeIndex].originalData;
  359. }
  360. },
  361. // 删除
  362. delCubeList(index){
  363. this.getGridSeat(this.selectedList[index].start,this.selectedList[index].end,2);
  364. this.selectedList.splice(index, 1);
  365. if(index == this.selCubeIndex){
  366. this.selCubeInfo(this.selCubeIndex-1);
  367. }
  368. this.updateData();
  369. },
  370. // 通信
  371. updateData(){
  372. this.$emit("chang",this.selectedList)
  373. },
  374. // 添加图片
  375. addCubeImg(imgArr){
  376. console.info(this.selCubeIndex,"0",this.selectedList);
  377. if(this.selCubeIndex != -1){
  378. this.selectedList[this.selCubeIndex].img = imgArr[0].imgUrl;
  379. this.selectedList[this.selCubeIndex].url = imgArr[0].url;
  380. this.selectedList[this.selCubeIndex].originalData = imgArr;
  381. }
  382. this.updateData();
  383. },
  384. //计算选中层的宽度。
  385. getSelectedWidth: function (item) {
  386. return (parseInt(item.end.x) - parseInt(item.start.x) + 1) * this.cubeCellWidth;
  387. },
  388. //计算选中层的高度。
  389. getSelectedHeight: function (item) {
  390. return (parseInt(item.end.y) - parseInt(item.start.y) + 1) * this.cubeCellHeight;
  391. },
  392. //计算选中层的右边距离。
  393. getSelectedTop: function (item) {
  394. return (item.start.y - 1) * this.cubeCellHeight;
  395. },
  396. //计算选中层的左边距离。
  397. getSelectedLeft: function (item) {
  398. return (item.start.x - 1) * this.cubeCellWidth;
  399. }
  400. }
  401. })
  402. </script>
  403. <style>
  404. .diy-rubik-cube{
  405. display: inline-block;
  406. width: 100%;
  407. }
  408. .decorate-cube {
  409. position: relative;
  410. }
  411. .decorate-cube .cube-item {
  412. border-right: 1px solid #B7B7B7;
  413. }
  414. .decorate-cube .cube-selected {
  415. position: absolute;
  416. background-color: #E8F7FD;
  417. border: 1px solid #BBDDFF;
  418. text-align: center;
  419. color: #88C4DC;
  420. cursor: pointer;
  421. box-sizing: border-box;
  422. }
  423. .decorate-cube .click-cube-selected{
  424. border: 1px solid #3388FF;
  425. }
  426. /* .decorate-cube .cube-selected.cube-selected-click {
  427. background: #e0edff;
  428. border: 1px solid #155bd4;
  429. color: #155bd4;
  430. z-index: 2;
  431. cursor: auto;
  432. } */
  433. .decorate-cube .cube-selected-text {
  434. font-size: 12px;
  435. width: 100%;
  436. position: absolute;
  437. top: 50%;
  438. left: 50%;
  439. transform: translateX(-50%) translateY(-50%);
  440. }
  441. .decorate-cube .cube-col {
  442. float: left;
  443. list-style: none;
  444. padding: 0;
  445. margin: 0;
  446. }
  447. .decorate-cube .cube-item:first-child {
  448. border-top: 1px solid #B7B7B7;
  449. }
  450. .decorate-cube .cube-item {
  451. background: #FFF;
  452. border-left: 1px solid #B7B7B7;
  453. border-bottom: 1px solid #B7B7B7;
  454. cursor: pointer;
  455. text-align: center;
  456. box-sizing: border-box;
  457. }
  458. .decorate-cube .cube-item.item-selecting {
  459. background: #e0edff;
  460. }
  461. .decorate-cube .cube-item.item-selected {
  462. background: #e0edff;
  463. visibility: hidden;
  464. }
  465. .cube-del-icon{
  466. position: absolute;
  467. right: -8px;
  468. top: -8px;
  469. z-index: 10;
  470. }
  471. .del-sel-icon{
  472. font-size: 19px;
  473. color: #666;
  474. background: #FFF;
  475. border-radius: 50%;
  476. }
  477. </style>