Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * A javascript module to handle list items drag and drop
18
 *
19
 * Example of usage:
20
 *
21
 * Create a list (for example `<ul>` or `<tbody>`) where each draggable element has a drag handle.
22
 * The best practice is to use the template core/drag_handle:
23
 * $OUTPUT->render_from_template('core/drag_handle', ['movetitle' => get_string('movecontent', 'moodle', ELEMENTNAME)]);
24
 *
25
 * Attach this JS module to this list:
26
 *
27
 * Space between define and ( critical in comment but not allowed in code in order to function
28
 * correctly with Moodle's requirejs.php
29
 *
30
 * For the full list of possible parameters see var defaultParameters below.
31
 *
32
 * The following jQuery events are fired:
33
 * - SortableList.EVENTS.DRAGSTART : when user started dragging a list element
34
 * - SortableList.EVENTS.DRAG : when user dragged a list element to a new position
35
 * - SortableList.EVENTS.DROP : when user dropped a list element
36
 * - SortableList.EVENTS.DROPEND : when user finished dragging - either fired right after dropping or
37
 *                          if "Esc" was pressed during dragging
38
 *
39
 * @example
40
 * define (['jquery', 'core/sortable_list'], function($, SortableList) {
41
 *     var list = new SortableList('ul.my-awesome-list'); // source list (usually <ul> or <tbody>) - selector or element
42
 *
43
 *     // Listen to the events when element is dragged.
44
 *     $('ul.my-awesome-list > *').on(SortableList.EVENTS.DROP, function(evt, info) {
45
 *         console.log(info);
46
 *     });
47
 *
48
 *     // Advanced usage. Overwrite methods getElementName, getDestinationName, moveDialogueTitle, for example:
49
 *     list.getElementName = function(element) {
50
 *         return $.Deferred().resolve(element.attr('data-name'));
51
 *     }
52
 * });
53
 *
54
 * @module     core/sortable_list
55
 * @class      core/sortable_list
56
 * @copyright  2018 Marina Glancy
57
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
58
 */
59
define(['jquery', 'core/log', 'core/autoscroll', 'core/str', 'core/modal_cancel', 'core/modal_events', 'core/notification'],
60
function($, log, autoScroll, str, ModalCancel, ModalEvents, Notification) {
61
 
62
    /**
63
     * Default parameters
64
     *
65
     * @private
66
     * @type {Object}
67
     */
68
    var defaultParameters = {
69
        targetListSelector: null,
70
        moveHandlerSelector: '[data-drag-type=move]',
71
        isHorizontal: false,
72
        autoScroll: true
73
    };
74
 
75
    /**
76
     * Class names for different elements that may be changed during sorting
77
     *
78
     * @private
79
     * @type {Object}
80
     */
81
    var CSS = {
82
        keyboardDragClass: 'dragdrop-keyboard-drag',
83
        isDraggedClass: 'sortable-list-is-dragged',
84
        isDroppedClass: 'sortable-list-is-dropped',
85
        currentPositionClass: 'sortable-list-current-position',
86
        sourceListClass: 'sortable-list-source',
87
        targetListClass: 'sortable-list-target',
88
        overElementClass: 'sortable-list-over-element'
89
    };
90
 
91
    /**
92
     * Test the browser support for options objects on event listeners.
93
     * @return {Boolean}
94
     */
95
    var eventListenerOptionsSupported = function() {
96
        var passivesupported = false,
97
            options,
98
            testeventname = "testpassiveeventoptions";
99
 
100
        // Options support testing example from:
101
        // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
102
 
103
        try {
104
            options = Object.defineProperty({}, "passive", {
105
                // eslint-disable-next-line getter-return
106
                get: function() {
107
                    passivesupported = true;
108
                }
109
            });
110
 
111
            // We use an event name that is not likely to conflict with any real event.
112
            document.addEventListener(testeventname, options, options);
113
            // We remove the event listener as we have tested the options already.
114
            document.removeEventListener(testeventname, options, options);
115
        } catch (err) {
116
            // It's already false.
117
            passivesupported = false;
118
        }
119
        return passivesupported;
120
    };
121
 
122
    /**
123
     * Allow to create non-passive touchstart listeners and prevent page scrolling when dragging
124
     * From: https://stackoverflow.com/a/48098097
125
     *
126
     * @param {string} eventname
127
     * @returns {object}
128
     */
129
    var registerNotPassiveListeners = function(eventname) {
130
        return {
131
            setup: function(x, ns, handle) {
132
                if (ns.includes('notPassive')) {
133
                    this.addEventListener(eventname, handle, {passive: false});
134
                    return true;
135
                } else {
136
                    return false;
137
                }
138
            }
139
        };
140
    };
141
 
142
    if (eventListenerOptionsSupported) {
143
        $.event.special.touchstart = registerNotPassiveListeners('touchstart');
144
        $.event.special.touchmove = registerNotPassiveListeners('touchmove');
145
        $.event.special.touchend = registerNotPassiveListeners('touchend');
146
    }
147
 
148
    /**
149
     * Initialise sortable list.
150
     *
151
     * @param {(String|jQuery|Element)} root JQuery/DOM element representing sortable list (i.e. <ul>, <tbody>) or CSS selector
152
     * @param {Object} config Parameters for the list. See defaultParameters above for examples.
153
     * @param {(String|jQuery|Element)} config.targetListSelector target lists, by default same as root
154
     * @param {String} config.moveHandlerSelector  CSS selector for a drag handle. By default '[data-drag-type=move]'
155
     * @param {String} config.listSelector   CSS selector for target lists. By default the same as root
156
     * @param {(Boolean|Function)} config.isHorizontal Set to true if the list is horizontal (can also be a callback
157
     *                                                 with list as an argument)
158
     * @param {Boolean} config.autoScroll Engages autoscroll module for automatic vertical scrolling of the whole page,
159
     *                                    by default true
160
     */
161
    var SortableList = function(root, config) {
162
 
163
        this.info = null;
164
        this.proxy = null;
165
        this.proxyDelta = null;
166
        this.dragCounter = 0;
167
        this.lastEvent = null;
168
 
169
        this.config = $.extend({}, defaultParameters, config || {});
170
        this.config.listSelector = root;
171
        if (!this.config.targetListSelector) {
172
            this.config.targetListSelector = root;
173
        }
174
        if (typeof this.config.listSelector === 'object') {
175
            // The root is an element on the page. Register a listener for this element.
176
            $(this.config.listSelector).on('mousedown touchstart.notPassive', $.proxy(this.dragStartHandler, this));
177
        } else {
178
            // The root is a CSS selector. Register a listener that picks up the element dynamically.
179
            $('body').on('mousedown touchstart.notPassive', this.config.listSelector, $.proxy(this.dragStartHandler, this));
180
        }
181
        if (this.config.moveHandlerSelector !== null) {
182
            $('body').on('click keypress', this.config.moveHandlerSelector, $.proxy(this.clickHandler, this));
183
        }
184
 
185
    };
186
 
187
    /**
188
     * Events fired by this entity
189
     *
190
     * @public
191
     * @type {Object}
192
     */
193
    SortableList.EVENTS = {
194
        DRAGSTART: 'sortablelist-dragstart',
195
        DRAG: 'sortablelist-drag',
196
        DROP: 'sortablelist-drop',
197
        DRAGEND: 'sortablelist-dragend'
198
    };
199
 
200
    /**
201
     * Resets the temporary classes assigned during dragging
202
     * @private
203
     */
204
     SortableList.prototype.resetDraggedClasses = function() {
205
        var classes = [
206
            CSS.isDraggedClass,
207
            CSS.currentPositionClass,
208
            CSS.overElementClass,
209
            CSS.targetListClass,
210
        ];
211
        for (var i in classes) {
212
            $('.' + classes[i]).removeClass(classes[i]);
213
        }
214
        if (this.proxy) {
215
            this.proxy.remove();
216
            this.proxy = $();
217
        }
218
    };
219
 
220
    /**
221
     * Calculates evt.pageX, evt.pageY, evt.clientX and evt.clientY
222
     *
223
     * For touch events pageX and pageY are taken from the first touch;
224
     * For the emulated mousemove event they are taken from the last real event.
225
     *
226
     * @private
227
     * @param {Event} evt
228
     */
229
    SortableList.prototype.calculatePositionOnPage = function(evt) {
230
 
231
        if (evt.originalEvent && evt.originalEvent.touches && evt.originalEvent.touches[0] !== undefined) {
232
            // This is a touchmove or touchstart event, get position from the first touch position.
233
            var touch = evt.originalEvent.touches[0];
234
            evt.pageX = touch.pageX;
235
            evt.pageY = touch.pageY;
236
        }
237
 
238
        if (evt.pageX === undefined) {
239
            // Information is not present in case of touchend or when event was emulated by autoScroll.
240
            // Take the absolute mouse position from the last event.
241
            evt.pageX = this.lastEvent.pageX;
242
            evt.pageY = this.lastEvent.pageY;
243
        } else {
244
            this.lastEvent = evt;
245
        }
246
 
247
        if (evt.clientX === undefined) {
248
            // If not provided in event calculate relative mouse position.
249
            evt.clientX = Math.round(evt.pageX - $(window).scrollLeft());
250
            evt.clientY = Math.round(evt.pageY - $(window).scrollTop());
251
        }
252
    };
253
 
254
    /**
255
     * Handler from dragstart event
256
     *
257
     * @private
258
     * @param {Event} evt
259
     */
260
    SortableList.prototype.dragStartHandler = function(evt) {
261
        if (this.info !== null) {
262
            if (this.info.type === 'click' || this.info.type === 'touchend') {
263
                // Ignore double click.
264
                return;
265
            }
266
            // Mouse down or touch while already dragging, cancel previous dragging.
267
            this.moveElement(this.info.sourceList, this.info.sourceNextElement);
268
            this.finishDragging();
269
        }
270
 
271
        if (evt.type === 'mousedown' && evt.which !== 1) {
272
            // We only need left mouse click. If this is a mousedown event with right/middle click ignore it.
273
            return;
274
        }
275
 
276
        this.calculatePositionOnPage(evt);
277
        var movedElement = $(evt.target).closest($(evt.currentTarget).children());
278
        if (!movedElement.length) {
279
            // Can't find the element user wants to drag. They clicked on the list but outside of any element of the list.
280
            return;
281
        }
282
 
283
        // Check that we grabbed the element by the handle.
284
        if (this.config.moveHandlerSelector !== null) {
285
            if (!$(evt.target).closest(this.config.moveHandlerSelector, movedElement).length) {
286
                return;
287
            }
288
        }
289
 
290
        evt.stopPropagation();
291
        evt.preventDefault();
292
 
293
        // Information about moved element with original location.
294
        // This object is passed to event observers.
295
        this.dragCounter++;
296
        this.info = {
297
            element: movedElement,
298
            sourceNextElement: movedElement.next(),
299
            sourceList: movedElement.parent(),
300
            targetNextElement: movedElement.next(),
301
            targetList: movedElement.parent(),
302
            type: evt.type,
303
            dropped: false,
304
            startX: evt.pageX,
305
            startY: evt.pageY,
306
            startTime: new Date().getTime()
307
        };
308
 
309
        $(this.config.targetListSelector).addClass(CSS.targetListClass);
310
 
311
        var offset = movedElement.offset();
312
        movedElement.addClass(CSS.currentPositionClass);
313
        this.proxyDelta = {x: offset.left - evt.pageX, y: offset.top - evt.pageY};
314
        this.proxy = $();
315
        var thisDragCounter = this.dragCounter;
316
        setTimeout($.proxy(function() {
317
            // This mousedown event may in fact be a beginning of a 'click' event. Use timeout before showing the
318
            // dragged object so we can catch click event. When timeout finishes make sure that click event
319
            // has not happened during this half a second.
320
            // Verify dragcounter to make sure the user did not manage to do two very fast drag actions one after another.
321
            if (this.info === null || this.info.type === 'click' || this.info.type === 'keypress'
322
                    || this.dragCounter !== thisDragCounter) {
323
                return;
324
            }
325
 
326
            // Create a proxy - the copy of the dragged element that moves together with a mouse.
327
            this.createProxy();
328
        }, this), 500);
329
 
330
        // Start drag.
331
        $(window).on('mousemove touchmove.notPassive mouseup touchend.notPassive', $.proxy(this.dragHandler, this));
332
        $(window).on('keypress', $.proxy(this.dragcancelHandler, this));
333
 
334
        // Start autoscrolling. Every time the page is scrolled emulate the mousemove event.
335
        if (this.config.autoScroll) {
336
            autoScroll.start(function() {
337
                $(window).trigger('mousemove');
338
            });
339
        }
340
 
341
       this.executeCallback(SortableList.EVENTS.DRAGSTART);
342
    };
343
 
344
    /**
345
     * Creates a "proxy" object - a copy of the element that is being moved that always follows the mouse
346
     * @private
347
     */
348
    SortableList.prototype.createProxy = function() {
349
        this.proxy = this.info.element.clone();
350
        this.info.sourceList.append(this.proxy);
351
        this.proxy.removeAttr('id').removeClass(CSS.currentPositionClass)
352
            .addClass(CSS.isDraggedClass).css({position: 'fixed'});
353
        this.proxy.offset({top: this.proxyDelta.y + this.lastEvent.pageY, left: this.proxyDelta.x + this.lastEvent.pageX});
354
    };
355
 
356
    /**
357
     * Handler for click event - when user clicks on the drag handler or presses Enter on keyboard
358
     *
359
     * @private
360
     * @param {Event} evt
361
     */
362
    SortableList.prototype.clickHandler = function(evt) {
363
        if (evt.type === 'keypress' && evt.originalEvent.keyCode !== 13 && evt.originalEvent.keyCode !== 32) {
364
            return;
365
        }
366
        if (this.info !== null) {
367
            // Ignore double click.
368
            return;
369
        }
370
 
371
        // Find the element that this draghandle belongs to.
372
        var clickedElement = $(evt.target).closest(this.config.moveHandlerSelector),
373
            sourceList = clickedElement.closest(this.config.listSelector),
374
            movedElement = clickedElement.closest(sourceList.children());
375
        if (!movedElement.length) {
376
            return;
377
        }
378
 
379
        evt.preventDefault();
380
        evt.stopPropagation();
381
 
382
        // Store information about moved element with original location.
383
        this.dragCounter++;
384
        this.info = {
385
            element: movedElement,
386
            sourceNextElement: movedElement.next(),
387
            sourceList: sourceList,
388
            targetNextElement: movedElement.next(),
389
            targetList: sourceList,
390
            dropped: false,
391
            type: evt.type,
392
            startTime: new Date().getTime()
393
        };
394
 
395
        this.executeCallback(SortableList.EVENTS.DRAGSTART);
396
        this.displayMoveDialogue(clickedElement);
397
    };
398
 
399
    /**
400
     * Finds the position of the mouse inside the element - on the top, on the bottom, on the right or on the left\
401
     *
402
     * Used to determine if the moved element should be moved after or before the current element
403
     *
404
     * @private
405
     * @param {Number} pageX
406
     * @param {Number} pageY
407
     * @param {jQuery} element
408
     * @returns {(Object|null)}
409
     */
410
    SortableList.prototype.getPositionInNode = function(pageX, pageY, element) {
411
        if (!element.length) {
412
            return null;
413
        }
414
        var node = element[0],
415
            offset = 0,
416
            rect = node.getBoundingClientRect(),
417
            y = pageY - (rect.top + window.scrollY),
418
            x = pageX - (rect.left + window.scrollX);
419
        if (x >= -offset && x <= rect.width + offset && y >= -offset && y <= rect.height + offset) {
420
            return {
421
                x: x,
422
                y: y,
423
                xRatio: rect.width ? (x / rect.width) : 0,
424
                yRatio: rect.height ? (y / rect.height) : 0
425
            };
426
        }
427
        return null;
428
    };
429
 
430
    /**
431
     * Check if list is horizontal
432
     *
433
     * @param {jQuery} element
434
     * @return {Boolean}
435
     */
436
    SortableList.prototype.isListHorizontal = function(element) {
437
        var isHorizontal = this.config.isHorizontal;
438
        if (isHorizontal === true || isHorizontal === false) {
439
            return isHorizontal;
440
        }
441
        return isHorizontal(element);
442
    };
443
 
444
    /**
445
     * Handler for events mousemove touchmove mouseup touchend
446
     *
447
     * @private
448
     * @param {Event} evt
449
     */
450
    SortableList.prototype.dragHandler = function(evt) {
451
 
452
        evt.preventDefault();
453
        evt.stopPropagation();
454
 
455
        this.calculatePositionOnPage(evt);
456
 
457
        // We can not use evt.target here because it will most likely be our proxy.
458
        // Move the proxy out of the way so we can find the element at the current mouse position.
459
        this.proxy.offset({top: -1000, left: -1000});
460
        // Find the element at the current mouse position.
461
        var element = $(document.elementFromPoint(evt.clientX, evt.clientY));
462
 
463
        // Find the list element and the list over the mouse position.
464
        var mainElement = this.info.element[0],
465
            isNotSelf = function() {
466
                return this !== mainElement;
467
            },
468
            current = element.closest('.' + CSS.targetListClass + ' > :not(.' + CSS.isDraggedClass + ')').filter(isNotSelf),
469
            currentList = element.closest('.' + CSS.targetListClass),
470
            proxy = this.proxy,
471
            isNotProxy = function() {
472
                return !proxy || !proxy.length || this !== proxy[0];
473
            };
474
 
475
        // Add the specified class to the list element we are hovering.
476
        $('.' + CSS.overElementClass).removeClass(CSS.overElementClass);
477
        current.addClass(CSS.overElementClass);
478
 
479
        // Move proxy to the current position.
480
        this.proxy.offset({top: this.proxyDelta.y + evt.pageY, left: this.proxyDelta.x + evt.pageX});
481
 
482
        if (currentList.length && !currentList.children().filter(isNotProxy).length) {
483
            // Mouse is over an empty list.
484
            this.moveElement(currentList, $());
485
        } else if (current.length === 1 && !this.info.element.find(current[0]).length) {
486
            // Mouse is over an element in a list - find whether we should move the current position
487
            // above or below this element.
488
            var coordinates = this.getPositionInNode(evt.pageX, evt.pageY, current);
489
            if (coordinates) {
490
                var parent = current.parent(),
491
                    ratio = this.isListHorizontal(parent) ? coordinates.xRatio : coordinates.yRatio,
492
                    subList = current.find('.' + CSS.targetListClass),
493
                    subListEmpty = !subList.children().filter(isNotProxy).filter(isNotSelf).length;
494
                if (subList.length && subListEmpty && ratio > 0.2 && ratio < 0.8) {
495
                    // This is an element that is a parent of an empty list and we are around the middle of this element.
496
                    // Treat it as if we are over this empty list.
497
                   this.moveElement(subList, $());
498
                } else if (ratio > 0.5) {
499
                    // Insert after this element.
500
                   this.moveElement(parent, current.next().filter(isNotProxy));
501
                } else {
502
                    // Insert before this element.
503
                   this.moveElement(parent, current);
504
                }
505
            }
506
        }
507
 
508
        if (evt.type === 'mouseup' || evt.type === 'touchend') {
509
            // Drop the moved element.
510
            this.info.endX = evt.pageX;
511
            this.info.endY = evt.pageY;
512
            this.info.endTime = new Date().getTime();
513
            this.info.dropped = true;
514
            this.info.positionChanged = this.hasPositionChanged(this.info);
515
            var oldinfo = this.info;
516
            this.executeCallback(SortableList.EVENTS.DROP);
517
            this.finishDragging();
518
 
519
            if (evt.type === 'touchend'
520
                    && this.config.moveHandlerSelector !== null
521
                    && (oldinfo.endTime - oldinfo.startTime < 500)
522
                    && !oldinfo.positionChanged) {
523
                // The click event is not triggered on touch screens because we call preventDefault in touchstart handler.
524
                // If the touchend quickly followed touchstart without moving, consider it a "click".
525
                this.clickHandler(evt);
526
            } else if (oldinfo.positionChanged) {
527
                mainElement.classList.add(CSS.isDroppedClass);
528
            }
529
        }
530
    };
531
 
532
    /**
533
     * Checks if the position of the dragged element in the list has changed
534
     *
535
     * @private
536
     * @param {Object} info
537
     * @return {Boolean}
538
     */
539
    SortableList.prototype.hasPositionChanged = function(info) {
540
        return info.sourceList[0] !== info.targetList[0] ||
541
            info.sourceNextElement.length !== info.targetNextElement.length ||
542
            (info.sourceNextElement.length && info.sourceNextElement[0] !== info.targetNextElement[0]);
543
    };
544
 
545
    /**
546
     * Moves the current position of the dragged element
547
     *
548
     * @private
549
     * @param {jQuery} parentElement
550
     * @param {jQuery} beforeElement
551
     */
552
    SortableList.prototype.moveElement = function(parentElement, beforeElement) {
553
        var dragEl = this.info.element;
554
        if (beforeElement.length && beforeElement[0] === dragEl[0]) {
555
            // Insert before the current position of the dragged element - nothing to do.
556
            return;
557
        }
558
        if (parentElement[0] === this.info.targetList[0] &&
559
                beforeElement.length === this.info.targetNextElement.length &&
560
                beforeElement[0] === this.info.targetNextElement[0]) {
561
            // Insert in the same location as the current position - nothing to do.
562
            return;
563
        }
564
 
565
        if (beforeElement.length) {
566
            // Move the dragged element before the specified element.
567
            parentElement[0].insertBefore(dragEl[0], beforeElement[0]);
568
        } else if (this.proxy && this.proxy.parent().length && this.proxy.parent()[0] === parentElement[0]) {
569
            // We need to move to the end of the list but the last element in this list is a proxy.
570
            // Always leave the proxy in the end of the list.
571
            parentElement[0].insertBefore(dragEl[0], this.proxy[0]);
572
        } else {
573
            // Insert in the end of a list (when proxy is in another list).
574
            parentElement[0].appendChild(dragEl[0]);
575
        }
576
 
577
        // Save the current position of the dragged element in the list.
578
        this.info.targetList = parentElement;
579
        this.info.targetNextElement = beforeElement;
580
        this.executeCallback(SortableList.EVENTS.DRAG);
581
    };
582
 
583
    /**
584
     * Finish dragging (when dropped or cancelled).
585
     * @private
586
     */
587
    SortableList.prototype.finishDragging = function() {
588
        this.resetDraggedClasses();
589
        if (this.config.autoScroll) {
590
            autoScroll.stop();
591
        }
592
        $(window).off('mousemove touchmove.notPassive mouseup touchend.notPassive', $.proxy(this.dragHandler, this));
593
        $(window).off('keypress', $.proxy(this.dragcancelHandler, this));
594
        this.executeCallback(SortableList.EVENTS.DRAGEND);
595
        this.info = null;
596
    };
597
 
598
    /**
599
     * Executes callback specified in sortable list parameters
600
     *
601
     * @private
602
     * @param {String} eventName
603
     */
604
    SortableList.prototype.executeCallback = function(eventName) {
605
        this.info.element.trigger(eventName, this.info);
606
    };
607
 
608
    /**
609
     * Handler from keypress event (cancel dragging when Esc is pressed)
610
     *
611
     * @private
612
     * @param {Event} evt
613
     */
614
    SortableList.prototype.dragcancelHandler = function(evt) {
615
        if (evt.type !== 'keypress' || evt.originalEvent.keyCode !== 27) {
616
            // Only cancel dragging when Esc was pressed.
617
            return;
618
        }
619
        // Dragging was cancelled. Return item to the original position.
620
        this.moveElement(this.info.sourceList, this.info.sourceNextElement);
621
        this.finishDragging();
622
    };
623
 
624
    /**
625
     * Returns the name of the current element to be used in the move dialogue
626
     *
627
     * @public
628
     * @param {jQuery} element
629
     * @return {Promise}
630
     */
631
    SortableList.prototype.getElementName = function(element) {
632
        return $.Deferred().resolve(element.text());
633
    };
634
 
635
    /**
636
     * Returns the label for the potential move destination, i.e. "After ElementX" or "To the top of the list"
637
     *
638
     * Note that we use "after" in the label for better UX
639
     *
640
     * @public
641
     * @param {jQuery} parentElement
642
     * @param {jQuery} afterElement
643
     * @return {Promise}
644
     */
645
    SortableList.prototype.getDestinationName = function(parentElement, afterElement) {
646
        if (!afterElement.length) {
647
            return str.get_string('movecontenttothetop', 'moodle');
648
        } else {
649
            return this.getElementName(afterElement)
650
                .then(function(name) {
651
                    return str.get_string('movecontentafter', 'moodle', name);
652
                });
653
        }
654
    };
655
 
656
    /**
657
     * Returns the title for the move dialogue ("Move elementY")
658
     *
659
     * @public
660
     * @param {jQuery} element
661
     * @param {jQuery} handler
662
     * @return {Promise}
663
     */
664
    SortableList.prototype.getMoveDialogueTitle = function(element, handler) {
665
        if (handler.attr('title')) {
666
            return $.Deferred().resolve(handler.attr('title'));
667
        }
668
        return this.getElementName(element).then(function(name) {
669
            return str.get_string('movecontent', 'moodle', name);
670
        });
671
    };
672
 
673
    /**
674
     * Returns the list of possible move destinations
675
     *
676
     * @private
677
     * @return {Promise}
678
     */
679
    SortableList.prototype.getDestinationsList = function() {
680
        var addedLists = [],
681
            targets = $(this.config.targetListSelector),
682
            destinations = $('<ul/>').addClass(CSS.keyboardDragClass),
683
            result = $.when().then(function() {
684
                return destinations;
685
            }),
686
            createLink = $.proxy(function(parentElement, beforeElement, afterElement) {
687
                if (beforeElement.is(this.info.element) || afterElement.is(this.info.element)) {
688
                    // Can not move before or after itself.
689
                    return;
690
                }
691
                if ($.contains(this.info.element[0], parentElement[0])) {
692
                    // Can not move to its own child.
693
                    return;
694
                }
695
                result = result
696
                .then($.proxy(function() {
697
                    return this.getDestinationName(parentElement, afterElement);
698
                }, this))
699
                .then(function(txt) {
700
                    var li = $('<li/>').appendTo(destinations);
701
                    var a = $('<a href="#"/>').attr('data-core_sortable_list-quickmove', 1).appendTo(li);
702
                    a.data('parent-element', parentElement).data('before-element', beforeElement).text(txt);
703
                    return destinations;
704
                });
705
            }, this),
706
            addList = function() {
707
                // Destination lists may be nested. We want to add all move destinations in the same
708
                // order they appear on the screen for the user.
709
                if ($.inArray(this, addedLists) !== -1) {
710
                    return;
711
                }
712
                addedLists.push(this);
713
                var list = $(this),
714
                    children = list.children();
715
                children.each(function() {
716
                    var element = $(this);
717
                    createLink(list, element, element.prev());
718
                    // Add all nested lists.
719
                    element.find(targets).each(addList);
720
                });
721
                createLink(list, $(), children.last());
722
            };
723
        targets.each(addList);
724
        return result;
725
    };
726
 
727
    /**
728
     * Displays the dialogue to move element.
729
     * @param {jQuery} clickedElement element to return focus to after the modal is closed
730
     * @private
731
     */
732
    SortableList.prototype.displayMoveDialogue = function(clickedElement) {
733
        ModalCancel.create({
734
            title: this.getMoveDialogueTitle(this.info.element, clickedElement),
735
            body: this.getDestinationsList()
736
        }).then($.proxy(function(modal) {
737
            var quickMoveHandler = $.proxy(function(e) {
738
                e.preventDefault();
739
                e.stopPropagation();
740
                this.moveElement($(e.currentTarget).data('parent-element'), $(e.currentTarget).data('before-element'));
741
                this.info.endTime = new Date().getTime();
742
                this.info.positionChanged = this.hasPositionChanged(this.info);
743
                this.info.dropped = true;
744
                clickedElement.focus();
745
                this.executeCallback(SortableList.EVENTS.DROP);
746
                modal.hide();
747
            }, this);
748
            modal.getRoot().on('click', '[data-core_sortable_list-quickmove]', quickMoveHandler);
749
            modal.getRoot().on(ModalEvents.hidden, $.proxy(function() {
750
                // Always destroy when hidden, it is generated dynamically each time.
751
                modal.getRoot().off('click', '[data-core_sortable_list-quickmove]', quickMoveHandler);
752
                modal.destroy();
753
                this.finishDragging();
754
            }, this));
755
            modal.setLarge();
756
            modal.show();
757
            return modal;
758
        }, this)).catch(Notification.exception);
759
    };
760
 
761
    return SortableList;
762
 
763
});