PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.1
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / assets / src / js / vendor / vue / vue-draggable.js

vue-draggable.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.1, at assets/src/js/vendor/vue/vue-draggable.js

373 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 'use strict';
2 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; };
3
4 function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
5
6 (function () {
7 "use strict";
8
9 if (!Array.from) {
10 Array.from = function (object) {
11 return [].slice.call(object);
12 };
13 }
14
15 function buildDraggable(Sortable) {
16 function removeNode(node) {
17 node.parentElement.removeChild(node);
18 }
19
20 function insertNodeAt(fatherNode, node, position) {
21 var refNode = position === 0 ? fatherNode.children[0] : fatherNode.children[position - 1].nextSibling;
22 fatherNode.insertBefore(node, refNode);
23 }
24
25 function computeVmIndex(vnodes, element) {
26 return vnodes.map(function (elt) {
27 return elt.elm;
28 }).indexOf(element);
29 }
30
31 function _computeIndexes(slots, children, isTransition) {
32 if (!slots) {
33 return [];
34 }
35
36 var elmFromNodes = slots.map(function (elt) {
37 return elt.elm;
38 });
39 var rawIndexes = [].concat(_toConsumableArray(children)).map(function (elt) {
40 return elmFromNodes.indexOf(elt);
41 });
42 return isTransition ? rawIndexes.filter(function (ind) {
43 return ind !== -1;
44 }) : rawIndexes;
45 }
46
47 function emit(evtName, evtData) {
48 var _this = this;
49
50 this.$nextTick(function () {
51 return _this.$emit(evtName.toLowerCase(), evtData);
52 });
53 }
54
55 function delegateAndEmit(evtName) {
56 var _this2 = this;
57
58 return function (evtData) {
59 if (_this2.realList !== null) {
60 _this2['onDrag' + evtName](evtData);
61 }
62 emit.call(_this2, evtName, evtData);
63 };
64 }
65
66 var eventsListened = ['Start', 'Add', 'Remove', 'Update', 'End'];
67 var eventsToEmit = ['Choose', 'Sort', 'Filter', 'Clone'];
68 var readonlyProperties = ['Move'].concat(eventsListened, eventsToEmit).map(function (evt) {
69 return 'on' + evt;
70 });
71 var draggingElement = null;
72
73 var props = {
74 options: Object,
75 list: {
76 type: Array,
77 required: false,
78 default: null
79 },
80 value: {
81 type: Array,
82 required: false,
83 default: null
84 },
85 noTransitionOnDrag: {
86 type: Boolean,
87 default: false
88 },
89 clone: {
90 type: Function,
91 default: function _default(original) {
92 return original;
93 }
94 },
95 element: {
96 type: String,
97 default: 'div'
98 },
99 move: {
100 type: Function,
101 default: null
102 }
103 };
104
105 var draggableComponent = {
106 name: 'draggable',
107
108 props: props,
109
110 data: function data() {
111 return {
112 transitionMode: false,
113 componentMode: false
114 };
115 },
116 render: function render(h) {
117 var slots = this.$slots.default;
118 if (slots && slots.length === 1) {
119 var child = slots[0];
120 if (child.componentOptions && child.componentOptions.tag === "transition-group") {
121 this.transitionMode = true;
122 }
123 }
124 var children = slots;
125 var footer = this.$slots.footer;
126
127 if (footer) {
128 children = slots ? [].concat(_toConsumableArray(slots), _toConsumableArray(footer)) : [].concat(_toConsumableArray(footer));
129 }
130 return h(this.element, null, children);
131 },
132 mounted: function mounted() {
133 var _this3 = this;
134
135 this.componentMode = this.element.toLowerCase() !== this.$el.nodeName.toLowerCase();
136 if (this.componentMode && this.transitionMode) {
137 throw new Error('Transition-group inside component is not supported. Please alter element value or remove transition-group. Current element value: ' + this.element);
138 }
139 var optionsAdded = {};
140 eventsListened.forEach(function (elt) {
141 optionsAdded['on' + elt] = delegateAndEmit.call(_this3, elt);
142 });
143
144 eventsToEmit.forEach(function (elt) {
145 optionsAdded['on' + elt] = emit.bind(_this3, elt);
146 });
147
148 var options = _extends({}, this.options, optionsAdded, { onMove: function onMove(evt, originalEvent) {
149 return _this3.onDragMove(evt, originalEvent);
150 } });
151 !('draggable' in options) && (options.draggable = '>*');
152 this._sortable = new Sortable(this.rootContainer, options);
153 this.computeIndexes();
154 },
155 beforeDestroy: function beforeDestroy() {
156 this._sortable.destroy();
157 },
158
159
160 computed: {
161 rootContainer: function rootContainer() {
162 return this.transitionMode ? this.$el.children[0] : this.$el;
163 },
164 isCloning: function isCloning() {
165 return !!this.options && !!this.options.group && this.options.group.pull === 'clone';
166 },
167 realList: function realList() {
168 return !!this.list ? this.list : this.value;
169 }
170 },
171
172 watch: {
173 options: {
174 handler: function handler(newOptionValue) {
175 for (var property in newOptionValue) {
176 if (readonlyProperties.indexOf(property) == -1) {
177 this._sortable.option(property, newOptionValue[property]);
178 }
179 }
180 },
181
182 deep: true
183 },
184
185 realList: function realList() {
186 this.computeIndexes();
187 }
188 },
189
190 methods: {
191 getChildrenNodes: function getChildrenNodes() {
192 if (this.componentMode) {
193 return this.$children[0].$slots.default;
194 }
195 var rawNodes = this.$slots.default;
196 return this.transitionMode ? rawNodes[0].child.$slots.default : rawNodes;
197 },
198 computeIndexes: function computeIndexes() {
199 var _this4 = this;
200
201 this.$nextTick(function () {
202 _this4.visibleIndexes = _computeIndexes(_this4.getChildrenNodes(), _this4.rootContainer.children, _this4.transitionMode);
203 });
204 },
205 getUnderlyingVm: function getUnderlyingVm(htmlElt) {
206 var index = computeVmIndex(this.getChildrenNodes() || [], htmlElt);
207 if (index === -1) {
208 //Edge case during move callback: related element might be
209 //an element different from collection
210 return null;
211 }
212 var element = this.realList[index];
213 return { index: index, element: element };
214 },
215 getUnderlyingPotencialDraggableComponent: function getUnderlyingPotencialDraggableComponent(_ref) {
216 var __vue__ = _ref.__vue__;
217
218 if (!__vue__ || !__vue__.$options || __vue__.$options._componentTag !== "transition-group") {
219 return __vue__;
220 }
221 return __vue__.$parent;
222 },
223 emitChanges: function emitChanges(evt) {
224 var _this5 = this;
225
226 this.$nextTick(function () {
227 _this5.$emit('change', evt);
228 });
229 },
230 alterList: function alterList(onList) {
231 if (!!this.list) {
232 onList(this.list);
233 } else {
234 var newList = [].concat(_toConsumableArray(this.value));
235 onList(newList);
236 this.$emit('input', newList);
237 }
238 },
239 spliceList: function spliceList() {
240 var _arguments = arguments;
241
242 var spliceList = function spliceList(list) {
243 return list.splice.apply(list, _arguments);
244 };
245 this.alterList(spliceList);
246 },
247 updatePosition: function updatePosition(oldIndex, newIndex) {
248 var updatePosition = function updatePosition(list) {
249 return list.splice(newIndex, 0, list.splice(oldIndex, 1)[0]);
250 };
251 this.alterList(updatePosition);
252 },
253 getRelatedContextFromMoveEvent: function getRelatedContextFromMoveEvent(_ref2) {
254 var to = _ref2.to,
255 related = _ref2.related;
256
257 var component = this.getUnderlyingPotencialDraggableComponent(to);
258 if (!component) {
259 return { component: component };
260 }
261 var list = component.realList;
262 var context = { list: list, component: component };
263 if (to !== related && list && component.getUnderlyingVm) {
264 var destination = component.getUnderlyingVm(related);
265 if (destination) {
266 return _extends(destination, context);
267 }
268 }
269
270 return context;
271 },
272 getVmIndex: function getVmIndex(domIndex) {
273 var indexes = this.visibleIndexes;
274 var numberIndexes = indexes.length;
275 return domIndex > numberIndexes - 1 ? numberIndexes : indexes[domIndex];
276 },
277 getComponent: function getComponent() {
278 return this.$slots.default[0].componentInstance;
279 },
280 resetTransitionData: function resetTransitionData(index) {
281 if (!this.noTransitionOnDrag || !this.transitionMode) {
282 return;
283 }
284 var nodes = this.getChildrenNodes();
285 nodes[index].data = null;
286 var transitionContainer = this.getComponent();
287 transitionContainer.children = [];
288 transitionContainer.kept = undefined;
289 },
290 onDragStart: function onDragStart(evt) {
291 this.context = this.getUnderlyingVm(evt.item);
292 evt.item._underlying_vm_ = this.clone(this.context.element);
293 draggingElement = evt.item;
294 },
295 onDragAdd: function onDragAdd(evt) {
296 var element = evt.item._underlying_vm_;
297 if (element === undefined) {
298 return;
299 }
300 removeNode(evt.item);
301 var newIndex = this.getVmIndex(evt.newIndex);
302 this.spliceList(newIndex, 0, element);
303 this.computeIndexes();
304 var added = { element: element, newIndex: newIndex };
305 this.emitChanges({ added: added });
306 },
307 onDragRemove: function onDragRemove(evt) {
308 insertNodeAt(this.rootContainer, evt.item, evt.oldIndex);
309 if (this.isCloning) {
310 removeNode(evt.clone);
311 return;
312 }
313 var oldIndex = this.context.index;
314 this.spliceList(oldIndex, 1);
315 var removed = { element: this.context.element, oldIndex: oldIndex };
316 this.resetTransitionData(oldIndex);
317 this.emitChanges({ removed: removed });
318 },
319 onDragUpdate: function onDragUpdate(evt) {
320 removeNode(evt.item);
321 insertNodeAt(evt.from, evt.item, evt.oldIndex);
322 var oldIndex = this.context.index;
323 var newIndex = this.getVmIndex(evt.newIndex);
324 this.updatePosition(oldIndex, newIndex);
325 var moved = { element: this.context.element, oldIndex: oldIndex, newIndex: newIndex };
326 this.emitChanges({ moved: moved });
327 },
328 computeFutureIndex: function computeFutureIndex(relatedContext, evt) {
329 if (!relatedContext.element) {
330 return 0;
331 }
332 var domChildren = [].concat(_toConsumableArray(evt.to.children)).filter(function (el) {
333 return el.style['display'] !== 'none';
334 });
335 var currentDOMIndex = domChildren.indexOf(evt.related);
336 var currentIndex = relatedContext.component.getVmIndex(currentDOMIndex);
337 var draggedInList = domChildren.indexOf(draggingElement) != -1;
338 return draggedInList || !evt.willInsertAfter ? currentIndex : currentIndex + 1;
339 },
340 onDragMove: function onDragMove(evt, originalEvent) {
341 var onMove = this.move;
342 if (!onMove || !this.realList) {
343 return true;
344 }
345
346 var relatedContext = this.getRelatedContextFromMoveEvent(evt);
347 var draggedContext = this.context;
348 var futureIndex = this.computeFutureIndex(relatedContext, evt);
349 _extends(draggedContext, { futureIndex: futureIndex });
350 _extends(evt, { relatedContext: relatedContext, draggedContext: draggedContext });
351 return onMove(evt, originalEvent);
352 },
353 onDragEnd: function onDragEnd(evt) {
354 this.computeIndexes();
355 draggingElement = null;
356 }
357 }
358 };
359 return draggableComponent;
360 }
361
362 if (typeof exports == "object") {
363 // var Sortable = require("sortablejs");
364 // module.exports = buildDraggable(Sortable);
365 } else if (typeof define == "function" && define.amd) {
366 define(['sortablejs'], function (Sortable) {
367 return buildDraggable(Sortable);
368 });
369 } else if (window && window.Vue && window.Sortable) {
370 var draggable = buildDraggable(window.Sortable);
371 Vue.component('draggable', draggable);
372 }
373 })();