TreeModal.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <template>
  2. <q-dialog v-model="modal"
  3. :persistent="persistent">
  4. <q-card class="tree-card">
  5. <div class="fit row wrap tree-inner-container">
  6. <div class="choose-tree-box question-details col-6">
  7. <div class="details-container-2 default-details-container">
  8. <div class="detail-box-container row">
  9. <div class="col-12 col-md-6 detail-box q-pr-sm">
  10. <div class="detail-box-title">پایه تحصیلی</div>
  11. <q-select v-model="grade"
  12. filled
  13. dense
  14. dropdown-icon="isax:arrow-down-1"
  15. option-label="title"
  16. :options="gradesList"
  17. @update:model-value="gradeSelected" />
  18. </div>
  19. <div class="col-12 col-md-6 detail-box q-pl-sm">
  20. <div class="detail-box-title"> نام درس</div>
  21. <q-select ref="lessonSelector"
  22. v-model="lesson"
  23. filled
  24. dense
  25. dropdown-icon="isax:arrow-down-1"
  26. option-label="title"
  27. :options="lessonsList"
  28. :disable="!doesHaveLessons"
  29. @update:model-value="lessonSelected" />
  30. </div>
  31. </div>
  32. <div class="question-tree">
  33. <tree ref="tree"
  34. :key="treeKey"
  35. :no-nodes-label="'لطفا یک پایه و درس را انتخاب کنید'"
  36. tick-strategy="strict"
  37. :get-node-by-id="getNodeById"
  38. @ticked="updateNodes"
  39. @lazy-loaded="syncAllCheckedIds" />
  40. </div>
  41. </div>
  42. </div>
  43. <div class="tree-result-box question-details col-6 ">
  44. <div class="details-container-2 default-details-container ">
  45. <div class="detail-box"
  46. style="padding-right:0;">
  47. <div class="box-btn-title flex justify-between">
  48. <div class="detail-box-title">انتخاب ها</div>
  49. <q-btn class="delete-all-btn"
  50. flat
  51. label="حذف همه"
  52. color="primary"
  53. @click="deleteAllNodes" />
  54. </div>
  55. <div class="tree-chips-box">
  56. <div v-if="getAllSubjects[0]">
  57. <q-chip v-for="item in getAllSubjects"
  58. :key="item"
  59. class="tree-chips"
  60. icon-remove="mdi-close"
  61. removable
  62. @remove="removeNode(item)">
  63. {{ item.title }}
  64. </q-chip>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. <div class="action-btn-box text-right">
  70. <slot name="tree-dialog-action-box">
  71. <q-btn v-close-popup
  72. class="close-btn"
  73. label="بستن"
  74. color="primary"
  75. :disable="persistent" />
  76. </slot>
  77. </div>
  78. </div>
  79. </div>
  80. <q-inner-loading :showing="dialogLoading"
  81. dark />
  82. </q-card>
  83. </q-dialog>
  84. </template>
  85. <script>
  86. import Tree from 'components/Tree/Tree.vue'
  87. import mixinTree from 'src/mixin/Tree.js'
  88. import { TreeNode } from 'src/models/TreeNode.js'
  89. export default {
  90. name: 'TreeModal',
  91. components: {
  92. Tree
  93. },
  94. mixins: [
  95. mixinTree
  96. ],
  97. props: {
  98. dialogValue: {
  99. type: Boolean
  100. },
  101. subjectsField: {
  102. type: Object
  103. },
  104. singleListChoiceMode: {
  105. type: Boolean,
  106. default () {
  107. return false
  108. }
  109. },
  110. initialLesson: {
  111. type: [Object, TreeNode],
  112. default () {
  113. return new TreeNode()
  114. }
  115. },
  116. persistent: {
  117. type: Boolean,
  118. default () {
  119. return false
  120. }
  121. }
  122. },
  123. emits: [
  124. 'gradeSelected',
  125. 'lessonSelected',
  126. 'update:dialogValue',
  127. 'update:subjectsField'
  128. ],
  129. data () {
  130. return {
  131. dialogLoading: false,
  132. lesson: '',
  133. grade: '',
  134. selectedNodesIDs: [],
  135. loading: false,
  136. currentTreeNode: [],
  137. lastTreeNodes: [],
  138. treeKey: 0,
  139. areNodesSynced: false,
  140. gradesList: [],
  141. lessonsList: []
  142. }
  143. },
  144. computed: {
  145. getAllSubjects () {
  146. const fieldText = []
  147. if (Object.keys(this.chosenSubjects).length === 0) {
  148. return fieldText
  149. }
  150. for (const key in this.chosenSubjects) {
  151. if (this.chosenSubjects[key].nodes && this.chosenSubjects[key].nodes.length > 0) {
  152. this.chosenSubjects[key].nodes.forEach(val => {
  153. fieldText.push(val)
  154. })
  155. }
  156. }
  157. return fieldText
  158. },
  159. doesHaveLessons () {
  160. return !!(this.lessonsList && this.lessonsList.length > 0)
  161. },
  162. modal: {
  163. get () {
  164. return this.dialogValue
  165. },
  166. set (value) {
  167. this.$emit('update:dialogValue', value)
  168. }
  169. },
  170. // main storage
  171. chosenSubjects: {
  172. get () {
  173. return this.subjectsField
  174. },
  175. set (value) {
  176. this.$emit('update:subjectsField', value)
  177. }
  178. }
  179. },
  180. watch: {
  181. modal (newVal) {
  182. this.updateChosenSubjects()
  183. if (!newVal) {
  184. this.lesson = ''
  185. }
  186. },
  187. currentTreeNode (newVal) {
  188. if (newVal.length > 0) {
  189. this.updateChosenSubjects()
  190. }
  191. },
  192. lesson (newVal) {
  193. if (newVal && this.singleListChoiceMode) {
  194. this.deleteAllNodes()
  195. }
  196. },
  197. initialLesson (newVal) {
  198. if (newVal.id) {
  199. this.lesson = newVal
  200. this.lessonSelected(newVal)
  201. }
  202. }
  203. },
  204. created () {},
  205. mounted () {
  206. if (this.initialLesson.id) {
  207. this.lesson = this.initialLesson
  208. }
  209. this.getGradesList()
  210. },
  211. updated () {},
  212. methods: {
  213. getGradesList () {
  214. this.dialogLoading = true
  215. this.$apiGateway.forrest.getGradesList()
  216. .then(res => {
  217. this.gradesList = res
  218. this.dialogLoading = false
  219. })
  220. .catch(() => {
  221. this.dialogLoading = false
  222. })
  223. },
  224. getLessonList (lessonId) {
  225. this.dialogLoading = true
  226. this.$apiGateway.forrest.getLessonList({
  227. data: {
  228. id: lessonId
  229. }
  230. })
  231. .then(res => {
  232. this.lessonsList = res
  233. this.dialogLoading = false
  234. })
  235. .catch(() => {
  236. this.dialogLoading = false
  237. })
  238. },
  239. getSelectedGradeLessons () {
  240. },
  241. updateNodes (values) {
  242. this.nodesUpdatedFromTree = values
  243. // if nodes are synced with response don't update currentTreeNode
  244. if (!this.areNodesSynced) {
  245. this.currentTreeNode = values
  246. }
  247. this.selectedNodesIDs = values.map(item => item.id)
  248. },
  249. removeNode (item) {
  250. const node = item.id ? item : { id: item }
  251. if (this.nodesUpdatedFromTree.find(item => item.id === node.id)) {
  252. this.setTickedMode('tree', node.id, false)
  253. }
  254. this.removeNodeFromChosenSubjects(node)
  255. this.updateChosenSubjects()
  256. },
  257. removeAllNodes () {
  258. this.setTickedMode('tree', this.selectedNodesIDs, false)
  259. this.chosenSubjects = {}
  260. },
  261. deleteAllNodes () {
  262. this.removeAllNodes()
  263. },
  264. removeNodeFromChosenSubjects (node) {
  265. for (const key in this.chosenSubjects) {
  266. this.chosenSubjects[key].nodes.forEach((value, index) => {
  267. if (value.id === node.id) {
  268. this.chosenSubjects[key].nodes.splice(index, 1)
  269. }
  270. })
  271. }
  272. },
  273. gradeSelected (item) {
  274. this.lesson = ''
  275. this.getLessonList(item.id)
  276. this.$emit('gradeSelected', item)
  277. },
  278. lessonSelected (lesson) {
  279. this.showTreeModalNode(lesson)
  280. this.$emit('lessonSelected', lesson)
  281. },
  282. showTreeModalNode (item) {
  283. this.treeKey += 1
  284. this.dialogLoading = true
  285. this.showTree('tree', this.getNode(item.id))
  286. .then(() => {
  287. this.syncAllCheckedIds()
  288. this.selectWantedTree(this.lesson)
  289. this.dialogLoading = false
  290. })
  291. .catch(() => {
  292. this.dialogLoading = false
  293. })
  294. },
  295. selectWantedTree (lesson) {
  296. if (this.chosenSubjects[lesson.id] && this.chosenSubjects[lesson.id].nodes) {
  297. this.switchToSelectedTree(lesson.id)
  298. return
  299. }
  300. this.createNewDataTree(lesson.id)
  301. },
  302. createNewDataTree (lessonId) {
  303. this.chosenSubjects[lessonId] = {}
  304. this.chosenSubjects[lessonId].nodes = []
  305. },
  306. switchToSelectedTree (lessonId) {
  307. this.currentTreeNode = this.chosenSubjects[lessonId].nodes
  308. },
  309. updateChosenSubjects () {
  310. if (this.lesson.id && this.chosenSubjects[this.lesson.id]) {
  311. this.chosenSubjects[this.lesson.id].nodes = this.currentTreeNode
  312. }
  313. },
  314. syncAllCheckedIds (syncedWithResponse) {
  315. if (syncedWithResponse) {
  316. this.areNodesSynced = true
  317. }
  318. if (this.lesson && this.chosenSubjects[this.lesson.id]) {
  319. const selectedNodesIds = this.chosenSubjects[this.lesson.id].nodes.map(item => item.id)
  320. if (selectedNodesIds.length > 0) {
  321. this.$refs.tree.setNodesTicked(selectedNodesIds, true)
  322. }
  323. this.areNodesSynced = false
  324. }
  325. }
  326. }
  327. }
  328. </script>
  329. <style scoped lang="scss">
  330. .action-btn-box {
  331. padding-top: 20px;
  332. .close-btn {
  333. color: #FFFFFF;
  334. font-weight: 500;
  335. font-size: 14px;
  336. line-height: 24px;
  337. background: #9690E4;
  338. border-radius: 10px;
  339. width: 96px;
  340. height: 40px;
  341. margin: auto 0 auto auto;
  342. }
  343. @media screen and (max-width: 880px) {
  344. display: flex;
  345. justify-content: center;
  346. align-items: center;
  347. }
  348. }
  349. .q-card {
  350. min-width: 830px;
  351. height: 580px;
  352. background: #FFFFFF;
  353. border-radius: 10px;
  354. padding: 30px;
  355. width: 830px;
  356. @media screen and (max-width: 880px) {
  357. min-width: 350px;
  358. width: 350px;
  359. .tree-inner-container {
  360. display: flex;
  361. flex-direction: column;
  362. height: auto !important;
  363. }
  364. }
  365. }
  366. .question-details {
  367. font-style: normal;
  368. font-weight: 500;
  369. font-size: 16px;
  370. line-height: 28px;
  371. text-align: right #{"/* rtl:ignore */"};
  372. color: #23263B;
  373. @media screen and (max-width: 880px) {
  374. width: 100%;
  375. }
  376. .tree-chips-box {
  377. height: 412px;
  378. max-width: 367px;
  379. background: #F4F5F6;
  380. border-radius: 10px;
  381. padding: 16px;
  382. overflow-x: scroll;
  383. .tree-chips {
  384. background: #FFFFFF;
  385. margin-right: 10px;
  386. }
  387. }
  388. .question-tree {
  389. height: 378px;
  390. overflow-x: scroll;
  391. margin-top: 10px;
  392. }
  393. .default-details-container {
  394. .detail-box {
  395. //margin-top: 10px;
  396. .detail-box-title, .delete-all-btn {
  397. margin-bottom: 5px;
  398. }
  399. }
  400. }
  401. .details-container-1 {
  402. .detail-box {
  403. padding-right: 12px #{"/* rtl:ignore */"};
  404. padding-left: 12px #{"/* rtl:ignore */"};
  405. }
  406. .detail-box-first {
  407. padding-right: 0 #{"/* rtl:ignore */"};
  408. }
  409. .detail-box-last {
  410. padding-left: 0 #{"/* rtl:ignore */"};
  411. }
  412. }
  413. .default-detail-btn {
  414. color: #65677F;
  415. width: 40px;
  416. height: 40px;
  417. border-radius: 10px;
  418. font-size: 14px;
  419. line-height: 24px;
  420. text-align: center;
  421. }
  422. .details-container-2 {
  423. .detail-box {
  424. .input-container {
  425. .input-box {
  426. width: 91%;
  427. }
  428. .icon-box {
  429. width: 40px;
  430. height: 40px;
  431. background: #FFFFFF;
  432. border-radius: 10px;
  433. display: flex;
  434. align-items: center;
  435. justify-content: center;
  436. margin-left: 16px ;
  437. .question-details-subject-img {
  438. height: 24px;
  439. max-width: 24px;
  440. }
  441. }
  442. }
  443. }
  444. .detail-box-first {
  445. padding-right: 0 #{"/* rtl:ignore */"};
  446. }
  447. .detail-box-last {
  448. padding-right: 0 #{"/* rtl:ignore */"};
  449. width: 200px;
  450. //margin-right: 132px #{"/* rtl:ignore */"};
  451. }
  452. .detail-box-last-of-row {
  453. padding-left: 0 #{"/* rtl:ignore */"};
  454. margin-top: 43px;
  455. text-align: end;
  456. display: flex;
  457. align-items: center;
  458. justify-content: center;
  459. .draft-btn {
  460. background: #FFFFFF;
  461. margin-left: 16px #{"/* rtl:ignore */"};
  462. font-weight: normal;
  463. color: #23263B;
  464. }
  465. .save-btn {
  466. background: #9690E4;
  467. font-weight: 500;
  468. color: #FFFFFF;
  469. }
  470. }
  471. }
  472. }
  473. @media screen and (min-width: 881px) {
  474. .choose-tree-box {
  475. padding-right: 15px;
  476. }
  477. .tree-result-box {
  478. padding-left: 15px;
  479. }
  480. }
  481. .tree-card {
  482. .question-details {
  483. .default-details-container {
  484. .detail-box {
  485. :deep(.q-field) {
  486. background: #FFFFFF;
  487. border-radius: 10px;
  488. line-height: 24px;
  489. height: 40px;
  490. min-height: 40px;
  491. .q-field__marginal {
  492. height: 40px;
  493. }
  494. .q-field__inner {
  495. margin-top: 9px;
  496. padding-right: 0 !important;
  497. padding-left: 0 !important;
  498. .q-field__control {
  499. border-radius: 10px;
  500. min-height: 40px;
  501. color: #65677F;
  502. background-color: #f4f5f6;
  503. &::before {
  504. display: none;
  505. }
  506. &::after {
  507. display: none;
  508. }
  509. }
  510. }
  511. }
  512. :deep(.q-field--auto-height){
  513. .q-field__native {
  514. min-height: 40px;
  515. color: #65677F;
  516. //background-color: #f4f5f6;
  517. &.q-field__prefix &.q-field__suffix &.q-field__input {
  518. color: #65677F;
  519. }
  520. }
  521. .q-field__control {
  522. border-radius: 10px;
  523. min-height: 40px;
  524. color: #65677F;
  525. background-color: #f4f5f6;
  526. ::before {
  527. display: none;
  528. }
  529. ::after {
  530. display: none;
  531. }
  532. }
  533. }
  534. }
  535. }
  536. }
  537. }
  538. </style>