Widget.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <draggable
  3. ref="draggable"
  4. :scale="scale"
  5. :top="widgetsTop"
  6. :left="widgetsLeft"
  7. :width="widgetsWidth"
  8. :height="widgetsHeight"
  9. v-on="$listeners"
  10. @blur="handleBlur"
  11. >
  12. <component
  13. :is="widgetType"
  14. :node="node"
  15. />
  16. </draggable>
  17. </template>
  18. <script>
  19. import Draggable from '../components/Draggable'
  20. import CText from './CText.vue'
  21. import CMarquee from './CMarquee.vue'
  22. import CImage from './CImage.vue'
  23. import CVideo from './CVideo.vue'
  24. import CTime from './CTime.vue'
  25. import CWeather from './CWeather.vue'
  26. import CWeb from './CWeb.vue'
  27. import CLive from './CLive.vue'
  28. export default {
  29. name: 'Widget',
  30. components: {
  31. Draggable,
  32. CText,
  33. CMarquee,
  34. CImage,
  35. CVideo,
  36. CTime,
  37. CWeather,
  38. CWeb,
  39. CLive
  40. },
  41. props: {
  42. scale: {
  43. type: Number,
  44. default: -1
  45. },
  46. node: {
  47. type: Object,
  48. default: null
  49. },
  50. root: {
  51. type: Object,
  52. default () {
  53. return {}
  54. }
  55. }
  56. },
  57. computed: {
  58. widgetType () {
  59. return this.node.type
  60. },
  61. widgetsTop () {
  62. return this.node.top
  63. },
  64. widgetsLeft () {
  65. return this.node.left
  66. },
  67. widgetsWidth () {
  68. return this.node.width
  69. },
  70. widgetsHeight () {
  71. return this.node.height
  72. },
  73. disabled () {
  74. return this.step === -1
  75. }
  76. },
  77. watch: {
  78. 'node.top': 'check',
  79. 'node.left': 'check',
  80. 'node.width': 'check',
  81. 'node.height': 'check'
  82. },
  83. methods: {
  84. handleBlur ({ top, left, width, height }) {
  85. Object.assign(this.node, {
  86. top: top | 0,
  87. left: left | 0,
  88. width: width | 0,
  89. height: height | 0
  90. })
  91. this.$emit('blur')
  92. },
  93. check () {
  94. const { top, left, width, height } = this.node
  95. const { width: rWidth, height: rHeight } = this.root
  96. if (top + height > rHeight) {
  97. this.$nextTick(() => {
  98. this.node.top = Math.max(0, rHeight - height)
  99. })
  100. }
  101. if (left + width > rWidth) {
  102. this.$nextTick(() => {
  103. this.node.left = Math.max(0, rWidth - width)
  104. })
  105. }
  106. }
  107. }
  108. }
  109. </script>
  110. <style scoped lang="scss">
  111. </style>