index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <schedule-wrapper
  3. class="c-schedule-swiper"
  4. :name="name"
  5. :hide-header="hideHeader"
  6. :editable="editable"
  7. :dirty="dirty"
  8. @submit="submit"
  9. @save="onSave"
  10. >
  11. <template v-if="editable">
  12. <div class="l-flex__auto l-flex">
  13. <div class="l-flex__none l-flex--row has-top-padding">
  14. <schema-table
  15. ref="table"
  16. class="c-schedule-swiper__table"
  17. :schema="programSchema"
  18. @row-click="onRowClick"
  19. @selection-change="onSelectionChange"
  20. />
  21. </div>
  22. <div class="l-flex__none l-flex--row c-schedule-swiper__btn">
  23. <el-button
  24. type="primary"
  25. :disabled="!programs.length"
  26. @click="onAdd"
  27. >
  28. <i class="el-icon-arrow-right" />
  29. </el-button>
  30. </div>
  31. <el-empty
  32. v-if="isEmpty"
  33. class="l-flex__auto l-flex--row center"
  34. description="请添加节目"
  35. />
  36. <draggable
  37. v-else
  38. v-model="scheduleOptions.events"
  39. class="l-flex__auto c-schedule-swiper__list has-padding--v"
  40. :class="{ disabled }"
  41. handle=".mover"
  42. animation="300"
  43. :disabled="disabled"
  44. >
  45. <div
  46. v-for="(program, index) in events"
  47. :key="program.key"
  48. class="l-flex--row c-schedule-swiper__item"
  49. >
  50. <div class="l-flex__auto l-flex--row mover">
  51. <i class="l-flex__none c-schedule-swiper__mover el-icon-sort" />
  52. <div
  53. class="l-flex__auto c-schedule-swiper__program u-ellipsis u-pointer"
  54. @click="onView(program)"
  55. >
  56. {{ program.name }}
  57. </div>
  58. </div>
  59. <span class="l-flex__none u-color--info">持续时间(秒):</span>
  60. <el-input-number
  61. v-model="program.duration"
  62. class="l-flex__none c-schedule-swiper__seconds"
  63. controls-position="right"
  64. :min="1"
  65. :max="2592000"
  66. :step="1"
  67. step-strictly
  68. />
  69. <i
  70. class="l-flex__none c-schedule-swiper__del o-icon--active el-icon-delete u-pointer"
  71. @click="onDel(program, index)"
  72. />
  73. </div>
  74. </draggable>
  75. </div>
  76. </template>
  77. <template v-else>
  78. <el-empty
  79. v-if="isEmpty"
  80. description="暂无节目"
  81. />
  82. <div class="c-schedule-swiper__list">
  83. <div
  84. v-for="program in events"
  85. :key="program.key"
  86. class="l-flex--row c-schedule-swiper__item readonly u-pointer"
  87. @click="onView(program)"
  88. >
  89. <div class="c-schedule-swiper__program u-ellipsis">
  90. {{ program.name }}
  91. </div>
  92. <span class="l-flex__none">持续{{ program.duration }}</span>
  93. </div>
  94. </div>
  95. </template>
  96. </schedule-wrapper>
  97. </template>
  98. <script>
  99. import { getPrograms } from '@/api/program'
  100. import { State } from '@/constant'
  101. import scheduleMixin from '../mixins/schedule'
  102. import Draggable from 'vuedraggable'
  103. import ScheduleWrapper from '../components/ScheduleWrapper'
  104. export default {
  105. name: 'ScheduleSwiper',
  106. components: {
  107. Draggable,
  108. ScheduleWrapper
  109. },
  110. mixins: [scheduleMixin],
  111. data () {
  112. return {
  113. record: null,
  114. programs: [],
  115. programSchema: {
  116. pagination: { small: true, layout: 'prev,pager,next' },
  117. condition: { status: State.AVAILABLE, resolutionRatio: this.ratio, name: '' },
  118. list: getPrograms,
  119. filters: [
  120. { key: 'name', type: 'search', placeholder: '节目名称' }
  121. ],
  122. cols: [
  123. { type: 'selection' },
  124. { prop: 'name', label: '节目名称' },
  125. { type: 'invoke', width: 80, render: [
  126. { label: '查看', on: this.onView }
  127. ] }
  128. ]
  129. }
  130. }
  131. },
  132. computed: {
  133. disabled () {
  134. return this.events.length < 2
  135. },
  136. dirty () {
  137. if (this.editable && this.record) {
  138. const { events, count } = this.record
  139. const currEvents = this.events
  140. if (count !== currEvents.length) {
  141. return true
  142. }
  143. for (let i = 0; i < events.length; i++) {
  144. if (events[i].id !== currEvents[i].id || events[i].duration !== currEvents[i].duration) {
  145. return true
  146. }
  147. }
  148. }
  149. return false
  150. }
  151. },
  152. created () {
  153. this.init()
  154. },
  155. methods: {
  156. transformEvents (events) {
  157. return events.sort((a, b) => a.index - b.index).map(this.transformEvent)
  158. },
  159. transformEvent (event) {
  160. const { programId, programName, programUrl, spend } = event
  161. return {
  162. key: Math.random().toString(16).slice(2),
  163. id: programId,
  164. name: programName,
  165. url: programUrl,
  166. duration: this.transformDuration(spend)
  167. }
  168. },
  169. getEvents () {
  170. return this.events.map(({ id, name, url, duration }, index) => {
  171. return {
  172. index,
  173. freq: 'SECONDLY',
  174. spend: duration,
  175. programId: id,
  176. programName: name,
  177. programUrl: url
  178. }
  179. })
  180. },
  181. transformDuration (duration) {
  182. if (this.editable) {
  183. return duration
  184. }
  185. const seconds = duration % 60
  186. let minutes = duration / 60 | 0
  187. let s = ''
  188. if (minutes > 60) {
  189. let hours = minutes / 60 | 0
  190. s += `${hours / 24 | 0}天`
  191. hours %= 24
  192. if (hours) {
  193. s += `${hours}小时`
  194. }
  195. minutes %= 60
  196. }
  197. if (minutes) {
  198. s += `${minutes}分`
  199. }
  200. if (seconds) {
  201. s += `${seconds}秒`
  202. }
  203. return s
  204. },
  205. init () {
  206. this.record = {
  207. events: this.events.map(({ id, duration }) => {
  208. return { id, duration }
  209. }),
  210. count: this.events.length
  211. }
  212. },
  213. onSave () {
  214. this.save().then(this.init)
  215. },
  216. onRowClick (row) {
  217. this.$refs.table.getInst().toggleRowSelection(row)
  218. },
  219. onSelectionChange (programs) {
  220. this.programs = programs
  221. },
  222. onAdd () {
  223. this.programs.forEach(this.add)
  224. this.$refs.table.getInst().clearSelection()
  225. },
  226. add ({ id, name, duration, buckets, itemConfigName }) {
  227. this.events.push({
  228. key: Math.random().toString(16).slice(2),
  229. id, name,
  230. url: `${buckets}/${itemConfigName}`,
  231. duration: duration || 60
  232. })
  233. },
  234. onView ({ id }) {
  235. this.$viewProgram(id)
  236. },
  237. onDel (event, index) {
  238. this.events.splice(index, 1)
  239. }
  240. }
  241. }
  242. </script>
  243. <style lang="scss" scoped>
  244. .c-schedule-swiper {
  245. color: $black;
  246. &__table {
  247. width: 318px;
  248. align-self: flex-start;
  249. max-height: 100%;
  250. }
  251. &__btn {
  252. padding: 0 $spacing $spacing;
  253. margin: 0 $spacing;
  254. border-left: 1px solid $border;
  255. border-right: 1px solid $border;
  256. }
  257. &__tip {
  258. line-height: 1;
  259. }
  260. &__list {
  261. display: flex;
  262. flex-direction: column;
  263. font-size: 14px;
  264. line-height: 1;
  265. overflow-y: auto;
  266. }
  267. &__list.disabled &__mover {
  268. visibility: hidden;
  269. }
  270. &__item {
  271. border: 1px solid $gray;
  272. border-radius: $radius--mini;
  273. &.sortable-chosen {
  274. border-color: #c6e2ff;
  275. background-color: $blue--light;
  276. }
  277. & + & {
  278. margin-top: 10px;
  279. }
  280. &.readonly {
  281. padding-right: 10px;
  282. &:hover {
  283. color: #409eff;
  284. border-color: #c6e2ff;
  285. background-color: $blue--light;
  286. }
  287. .c-schedule-swiper__program {
  288. font-size: 14px;
  289. }
  290. }
  291. }
  292. &__mover {
  293. padding: 10px 16px;
  294. font-size: 18px;
  295. cursor: move;
  296. }
  297. &__mover + &__program {
  298. padding-left: 0;
  299. }
  300. &__program {
  301. padding: 16px 10px;
  302. font-size: 16px;
  303. }
  304. &__seconds {
  305. width: 140px;
  306. }
  307. &__del {
  308. padding: 10px 16px;
  309. margin-left: 6px;
  310. color: $gray;
  311. font-size: 18px;
  312. &:hover {
  313. color: $primary;
  314. }
  315. }
  316. }
  317. </style>