Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VTreeviewNode.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  3. // Components
  4. import { VExpandTransition } from '../transitions';
  5. import { VIcon } from '../VIcon';
  6. import VTreeviewNode from './VTreeviewNode';
  7. // Mixins
  8. import { inject as RegistrableInject } from '../../mixins/registrable';
  9. // Utils
  10. import mixins from '../../util/mixins';
  11. import { getObjectValueByPath } from '../../util/helpers';
  12. export var VTreeviewNodeProps = {
  13. activatable: Boolean,
  14. activeClass: {
  15. type: String,
  16. default: 'v-treeview-node--active'
  17. },
  18. selectable: Boolean,
  19. selectedColor: {
  20. type: String,
  21. default: 'accent'
  22. },
  23. indeterminateIcon: {
  24. type: String,
  25. default: '$vuetify.icons.checkboxIndeterminate'
  26. },
  27. onIcon: {
  28. type: String,
  29. default: '$vuetify.icons.checkboxOn'
  30. },
  31. offIcon: {
  32. type: String,
  33. default: '$vuetify.icons.checkboxOff'
  34. },
  35. expandIcon: {
  36. type: String,
  37. default: '$vuetify.icons.subgroup'
  38. },
  39. loadingIcon: {
  40. type: String,
  41. default: '$vuetify.icons.loading'
  42. },
  43. itemKey: {
  44. type: String,
  45. default: 'id'
  46. },
  47. itemText: {
  48. type: String,
  49. default: 'name'
  50. },
  51. itemChildren: {
  52. type: String,
  53. default: 'children'
  54. },
  55. loadChildren: Function,
  56. openOnClick: Boolean,
  57. transition: Boolean
  58. };
  59. export default mixins(RegistrableInject('treeview')
  60. /* @vue/component */
  61. ).extend({
  62. name: 'v-treeview-node',
  63. inject: {
  64. treeview: {
  65. default: null
  66. }
  67. },
  68. props: _extends({
  69. item: {
  70. type: Object,
  71. default: function _default() {
  72. return null;
  73. }
  74. }
  75. }, VTreeviewNodeProps),
  76. data: function data() {
  77. return {
  78. isOpen: false,
  79. isSelected: false,
  80. isIndeterminate: false,
  81. isActive: false,
  82. isLoading: false,
  83. hasLoaded: false
  84. };
  85. },
  86. computed: {
  87. key: function key() {
  88. return getObjectValueByPath(this.item, this.itemKey);
  89. },
  90. children: function children() {
  91. return getObjectValueByPath(this.item, this.itemChildren);
  92. },
  93. text: function text() {
  94. return getObjectValueByPath(this.item, this.itemText);
  95. },
  96. scopedProps: function scopedProps() {
  97. return {
  98. item: this.item,
  99. leaf: !this.children,
  100. selected: this.isSelected,
  101. indeterminate: this.isIndeterminate,
  102. active: this.isActive,
  103. open: this.isOpen
  104. };
  105. },
  106. computedIcon: function computedIcon() {
  107. if (this.isIndeterminate) return this.indeterminateIcon;else if (this.isSelected) return this.onIcon;else return this.offIcon;
  108. },
  109. hasChildren: function hasChildren() {
  110. return !!this.children && (!!this.children.length || !!this.loadChildren);
  111. }
  112. },
  113. created: function created() {
  114. this.treeview.register(this);
  115. },
  116. beforeDestroy: function beforeDestroy() {
  117. this.treeview.unregister(this);
  118. },
  119. methods: {
  120. checkChildren: function checkChildren() {
  121. var _this = this;
  122. return new Promise(function (resolve) {
  123. // TODO: Potential issue with always trying
  124. // to load children if response is empty?
  125. if (!_this.children || _this.children.length || !_this.loadChildren || _this.hasLoaded) return resolve();
  126. _this.isLoading = true;
  127. resolve(_this.loadChildren(_this.item));
  128. }).then(function () {
  129. _this.isLoading = false;
  130. _this.hasLoaded = true;
  131. });
  132. },
  133. open: function open() {
  134. this.isOpen = !this.isOpen;
  135. this.treeview.updateOpen(this.key, this.isOpen);
  136. this.treeview.emitOpen();
  137. },
  138. genLabel: function genLabel() {
  139. var children = [];
  140. if (this.$scopedSlots.label) children.push(this.$scopedSlots.label(this.scopedProps));else children.push(this.text);
  141. return this.$createElement('div', {
  142. slot: 'label',
  143. staticClass: 'v-treeview-node__label'
  144. }, children);
  145. },
  146. genContent: function genContent() {
  147. var children = [this.$scopedSlots.prepend && this.$scopedSlots.prepend(this.scopedProps), this.genLabel(), this.$scopedSlots.append && this.$scopedSlots.append(this.scopedProps)];
  148. return this.$createElement('div', {
  149. staticClass: 'v-treeview-node__content'
  150. }, children);
  151. },
  152. genToggle: function genToggle() {
  153. var _this2 = this;
  154. return this.$createElement(VIcon, {
  155. staticClass: 'v-treeview-node__toggle',
  156. class: {
  157. 'v-treeview-node__toggle--open': this.isOpen,
  158. 'v-treeview-node__toggle--loading': this.isLoading
  159. },
  160. slot: 'prepend',
  161. on: {
  162. click: function click(e) {
  163. e.stopPropagation();
  164. if (_this2.isLoading) return;
  165. _this2.checkChildren().then(function () {
  166. return _this2.open();
  167. });
  168. }
  169. }
  170. }, [this.isLoading ? this.loadingIcon : this.expandIcon]);
  171. },
  172. genCheckbox: function genCheckbox() {
  173. var _this3 = this;
  174. return this.$createElement(VIcon, {
  175. staticClass: 'v-treeview-node__checkbox',
  176. props: {
  177. color: this.isSelected ? this.selectedColor : undefined
  178. },
  179. on: {
  180. click: function click(e) {
  181. e.stopPropagation();
  182. if (_this3.isLoading) return;
  183. _this3.checkChildren().then(function () {
  184. // We nextTick here so that items watch in VTreeview has a chance to run first
  185. _this3.$nextTick(function () {
  186. _this3.isSelected = !_this3.isSelected;
  187. _this3.isIndeterminate = false;
  188. _this3.treeview.updateSelected(_this3.key, _this3.isSelected);
  189. _this3.treeview.emitSelected();
  190. });
  191. });
  192. }
  193. }
  194. }, [this.computedIcon]);
  195. },
  196. genNode: function genNode() {
  197. var _this4 = this;
  198. var children = [this.genContent()];
  199. if (this.selectable) children.unshift(this.genCheckbox());
  200. if (this.hasChildren) children.unshift(this.genToggle());
  201. return this.$createElement('div', {
  202. staticClass: 'v-treeview-node__root',
  203. class: _defineProperty({}, this.activeClass, this.isActive),
  204. on: {
  205. click: function click() {
  206. if (_this4.openOnClick && _this4.children) {
  207. _this4.open();
  208. } else if (_this4.activatable) {
  209. _this4.isActive = !_this4.isActive;
  210. _this4.treeview.updateActive(_this4.key, _this4.isActive);
  211. _this4.treeview.emitActive();
  212. }
  213. }
  214. }
  215. }, children);
  216. },
  217. genChild: function genChild(item) {
  218. return this.$createElement(VTreeviewNode, {
  219. key: getObjectValueByPath(item, this.itemKey),
  220. props: {
  221. activatable: this.activatable,
  222. activeClass: this.activeClass,
  223. item: item,
  224. selectable: this.selectable,
  225. selectedColor: this.selectedColor,
  226. expandIcon: this.expandIcon,
  227. indeterminateIcon: this.indeterminateIcon,
  228. offIcon: this.offIcon,
  229. onIcon: this.onIcon,
  230. loadingIcon: this.loadingIcon,
  231. itemKey: this.itemKey,
  232. itemText: this.itemText,
  233. itemChildren: this.itemChildren,
  234. loadChildren: this.loadChildren,
  235. transition: this.transition,
  236. openOnClick: this.openOnClick
  237. },
  238. scopedSlots: this.$scopedSlots
  239. });
  240. },
  241. genChildrenWrapper: function genChildrenWrapper() {
  242. if (!this.isOpen || !this.children) return null;
  243. var children = [this.children.map(this.genChild)];
  244. return this.$createElement('div', {
  245. staticClass: 'v-treeview-node__children'
  246. }, children);
  247. },
  248. genTransition: function genTransition() {
  249. return this.$createElement(VExpandTransition, [this.genChildrenWrapper()]);
  250. }
  251. },
  252. render: function render(h) {
  253. var children = [this.genNode()];
  254. if (this.transition) children.push(this.genTransition());else children.push(this.genChildrenWrapper());
  255. return h('div', {
  256. staticClass: 'v-treeview-node',
  257. class: {
  258. 'v-treeview-node--leaf': !this.hasChildren,
  259. 'v-treeview-node--click': this.openOnClick,
  260. 'v-treeview-node--selected': this.isSelected,
  261. 'v-treeview-node--excluded': this.treeview.isExcluded(this.key)
  262. }
  263. }, children);
  264. }
  265. });
  266. //# sourceMappingURL=VTreeviewNode.js.map