Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * Provides drag and drop functionality for blocks.
3
 *
4
 * @module moodle-core-blockdraganddrop
5
 */
6
 
7
var AJAXURL = '/lib/ajax/blocks.php',
8
CSS = {
9
    BLOCK: 'block',
10
    BLOCKREGION: 'block-region',
11
    BLOCKADMINBLOCK: 'block_adminblock',
12
    EDITINGMOVE: 'editing_move',
13
    HEADER: 'header',
14
    LIGHTBOX: 'lightbox',
15
    REGIONCONTENT: 'region-content',
16
    SKIPBLOCK: 'skip-block',
17
    SKIPBLOCKTO: 'skip-block-to',
18
    MYINDEX: 'page-my-index',
19
    REGIONMAIN: 'region-main',
20
    BLOCKSMOVING: 'blocks-moving'
21
};
22
 
23
var SELECTOR = {
24
    DRAGHANDLE: '.' + CSS.HEADER + ' .commands .moodle-core-dragdrop-draghandle'
25
};
26
 
27
/**
28
 * Legacy drag and drop manager.
29
 * This drag and drop manager is specifically designed for themes using side-pre and side-post
30
 * that do not make use of the block output methods introduced by MDL-39824.
31
 *
32
 * @namespace M.core.blockdraganddrop
33
 * @class LegacyManager
34
 * @constructor
35
 * @extends M.core.dragdrop
36
 */
37
DRAGBLOCK = function() {
38
    DRAGBLOCK.superclass.constructor.apply(this, arguments);
39
};
40
Y.extend(DRAGBLOCK, M.core.dragdrop, {
41
    skipnodetop: null,
42
    skipnodebottom: null,
43
    dragsourceregion: null,
44
    initializer: function() {
45
        // Set group for parent class
46
        this.groups = ['block'];
47
        this.samenodeclass = CSS.BLOCK;
48
        this.parentnodeclass = CSS.REGIONCONTENT;
49
 
50
        // Add relevant classes and ID to 'content' block region on Dashboard page.
51
        var myhomecontent = Y.Node.all('body#' + CSS.MYINDEX + ' #' + CSS.REGIONMAIN + ' > .' + CSS.REGIONCONTENT);
52
        if (myhomecontent.size() > 0) {
53
            var contentregion = myhomecontent.item(0);
54
            contentregion.addClass(CSS.BLOCKREGION);
55
            contentregion.set('id', CSS.REGIONCONTENT);
56
            contentregion.one('div').addClass(CSS.REGIONCONTENT);
57
        }
58
 
59
        // Initialise blocks dragging
60
        // Find all block regions on the page
61
        var blockregionlist = Y.Node.all('div.' + CSS.BLOCKREGION);
62
 
63
        if (blockregionlist.size() === 0) {
64
            return false;
65
        }
66
 
67
        // See if we are missing either of block regions,
68
        // if yes we need to add an empty one to use as target
69
        if (blockregionlist.size() !== this.get('regions').length) {
70
            var blockregion = Y.Node.create('<div></div>')
71
                .addClass(CSS.BLOCKREGION);
72
            var regioncontent = Y.Node.create('<div></div>')
73
                .addClass(CSS.REGIONCONTENT);
74
            blockregion.appendChild(regioncontent);
75
            var pre = blockregionlist.filter('#region-pre');
76
            var post = blockregionlist.filter('#region-post');
77
 
78
            if (pre.size() === 0 && post.size() === 1) {
79
                // pre block is missing, instert it before post
80
                blockregion.setAttrs({id: 'region-pre'});
81
                post.item(0).insert(blockregion, 'before');
82
                blockregionlist.unshift(blockregion);
83
            } else if (post.size() === 0 && pre.size() === 1) {
84
                // post block is missing, instert it after pre
85
                blockregion.setAttrs({id: 'region-post'});
86
                pre.item(0).insert(blockregion, 'after');
87
                blockregionlist.push(blockregion);
88
            }
89
        }
90
 
91
        blockregionlist.each(function(blockregionnode) {
92
 
93
            // Setting blockregion as droptarget (the case when it is empty)
94
            // The region-post (the right one)
95
            // is very narrow, so add extra padding on the left to drop block on it.
96
            new Y.DD.Drop({
97
                node: blockregionnode.one('div.' + CSS.REGIONCONTENT),
98
                groups: this.groups,
99
                padding: '40 240 40 240'
100
            });
101
 
102
            // Make each div element in the list of blocks draggable
103
            var del = new Y.DD.Delegate({
104
                container: blockregionnode,
105
                nodes: '.' + CSS.BLOCK,
106
                target: true,
107
                handles: [SELECTOR.DRAGHANDLE],
108
                invalid: '.block-hider-hide, .block-hider-show, .moveto',
109
                dragConfig: {groups: this.groups}
110
            });
111
            del.dd.plug(Y.Plugin.DDProxy, {
112
                // Don't move the node at the end of the drag
113
                moveOnEnd: false
114
            });
115
            del.dd.plug(Y.Plugin.DDWinScroll);
116
 
117
            var blocklist = blockregionnode.all('.' + CSS.BLOCK);
118
            blocklist.each(function(blocknode) {
119
                var move = blocknode.one('a.' + CSS.EDITINGMOVE);
120
                if (move) {
121
                    move.replace(this.get_drag_handle(move.getAttribute('title'), '', 'iconsmall', true));
122
                    blocknode.one(SELECTOR.DRAGHANDLE).setStyle('cursor', 'move');
123
                }
124
            }, this);
125
        }, this);
126
    },
127
 
128
    get_block_id: function(node) {
129
        return Number(node.get('id').replace(/inst/i, ''));
130
    },
131
 
132
    get_block_region: function(node) {
133
        var region = node.ancestor('div.' + CSS.BLOCKREGION).get('id').replace(/region-/i, '');
134
        if (Y.Array.indexOf(this.get('regions'), region) === -1) {
135
            // Must be standard side-X
136
            if (window.right_to_left()) {
137
                if (region === 'post') {
138
                    region = 'pre';
139
                } else if (region === 'pre') {
140
                    region = 'post';
141
                }
142
            }
143
            return 'side-' + region;
144
        }
145
        // Perhaps custom region
146
        return region;
147
    },
148
 
149
    get_region_id: function(node) {
150
        return node.get('id').replace(/region-/i, '');
151
    },
152
 
153
    drag_start: function(e) {
154
        // Get our drag object
155
        var drag = e.target;
156
 
157
        // Store the parent node of original drag node (block)
158
        // we will need it later for show/hide empty regions
159
        this.dragsourceregion = drag.get('node').ancestor('div.' + CSS.BLOCKREGION);
160
 
161
        // Determine skipnodes and store them
162
        if (drag.get('node').previous() && drag.get('node').previous().hasClass(CSS.SKIPBLOCK)) {
163
            this.skipnodetop = drag.get('node').previous();
164
        }
165
        if (drag.get('node').next() && drag.get('node').next().hasClass(CSS.SKIPBLOCKTO)) {
166
            this.skipnodebottom = drag.get('node').next();
167
        }
168
 
169
        // Add the blocks-moving class so that the theme can respond if need be.
170
        Y.one('body').addClass(CSS.BLOCKSMOVING);
171
    },
172
 
173
    drop_over: function(e) {
174
        // Get a reference to our drag and drop nodes
175
        var drag = e.drag.get('node');
176
        var drop = e.drop.get('node');
177
 
178
        // We need to fix the case when parent drop over event has determined
179
        // 'goingup' and appended the drag node after admin-block.
180
        if (drop.hasClass(this.parentnodeclass) &&
181
                drop.one('.' + CSS.BLOCKADMINBLOCK) &&
182
                drop.one('.' + CSS.BLOCKADMINBLOCK).next('.' + CSS.BLOCK)) {
183
            drop.prepend(drag);
184
        }
185
 
186
        // Block is moved within the same region
187
        // stop here, no need to modify anything.
188
        if (this.dragsourceregion.contains(drop)) {
189
            return false;
190
        }
191
 
192
        // TODO: Hiding-displaying block region only works for base theme blocks
193
        // (region-pre, region-post) at the moment. It should be improved
194
        // to work with custom block regions as well.
195
 
196
        // TODO: Fix this for the case when user drag block towards empty section,
197
        // then the section appears, then user chnages his mind and moving back to
198
        // original section. The opposite section remains opened and empty.
199
 
200
        var documentbody = Y.one('body');
201
        // Moving block towards hidden region-content, display it
202
        var regionname = this.get_region_id(this.dragsourceregion);
203
        if (documentbody.hasClass('side-' + regionname + '-only')) {
204
            documentbody.removeClass('side-' + regionname + '-only');
205
        }
206
 
207
        // Moving from empty region-content towards the opposite one,
208
        // hide empty one (only for region-pre, region-post areas at the moment).
209
        regionname = this.get_region_id(drop.ancestor('div.' + CSS.BLOCKREGION));
210
        if (this.dragsourceregion.all('.' + CSS.BLOCK).size() === 0 &&
211
                this.dragsourceregion.get('id').match(/(region-pre|region-post)/i)) {
212
            if (!documentbody.hasClass('side-' + regionname + '-only')) {
213
                documentbody.addClass('side-' + regionname + '-only');
214
            }
215
        }
216
    },
217
 
218
    drag_end: function() {
219
        // clear variables
220
        this.skipnodetop = null;
221
        this.skipnodebottom = null;
222
        this.dragsourceregion = null;
223
        // Remove the blocks moving class once the drag-drop is over.
224
        Y.one('body').removeClass(CSS.BLOCKSMOVING);
225
    },
226
 
227
    drag_dropmiss: function(e) {
228
        // Missed the target, but we assume the user intended to drop it
229
        // on the last last ghost node location, e.drag and e.drop should be
230
        // prepared by global_drag_dropmiss parent so simulate drop_hit(e).
231
        this.drop_hit(e);
232
    },
233
 
234
    drop_hit: function(e) {
235
        var drag = e.drag;
236
        // Get a reference to our drag node
237
        var dragnode = drag.get('node');
238
        var dropnode = e.drop.get('node');
239
 
240
        // Amend existing skipnodes
241
        if (dragnode.previous() && dragnode.previous().hasClass(CSS.SKIPBLOCK)) {
242
            // the one that belongs to block below move below
243
            dragnode.insert(dragnode.previous(), 'after');
244
        }
245
        // Move original skipnodes
246
        if (this.skipnodetop) {
247
            dragnode.insert(this.skipnodetop, 'before');
248
        }
249
        if (this.skipnodebottom) {
250
            dragnode.insert(this.skipnodebottom, 'after');
251
        }
252
 
253
        // Add lightbox if it not there
254
        var lightbox = M.util.add_lightbox(Y, dragnode);
255
 
256
        // Prepare request parameters
257
        var params = {
258
            sesskey: M.cfg.sesskey,
259
            pagehash: this.get('pagehash'),
260
            action: 'move',
261
            bui_moveid: this.get_block_id(dragnode),
262
            bui_newregion: this.get_block_region(dropnode)
263
        };
264
 
265
        if (this.get('cmid')) {
266
            params.cmid = this.get('cmid');
267
        }
268
 
269
        if (dragnode.next('.' + this.samenodeclass) && !dragnode.next('.' + this.samenodeclass).hasClass(CSS.BLOCKADMINBLOCK)) {
270
            params.bui_beforeid = this.get_block_id(dragnode.next('.' + this.samenodeclass));
271
        }
272
 
273
        // Do AJAX request
274
        Y.io(M.cfg.wwwroot + AJAXURL, {
275
            method: 'POST',
276
            data: params,
277
            on: {
278
                start: function() {
279
                    lightbox.show();
280
                },
281
                success: function(tid, response) {
282
                    window.setTimeout(function() {
283
                        lightbox.hide();
284
                    }, 250);
285
                    try {
286
                        var responsetext = Y.JSON.parse(response.responseText);
287
                        if (responsetext.error) {
288
                            new M.core.ajaxException(responsetext);
289
                        }
290
                    } catch (e) {
291
                        // Ignore.
292
                    }
293
                },
294
                failure: function(tid, response) {
295
                    this.ajax_failure(response);
296
                    lightbox.hide();
297
                }
298
            },
299
            context: this
300
        });
301
    }
302
}, {
303
    NAME: 'core-blocks-dragdrop',
304
    ATTRS: {
305
        pagehash: {
306
            value: null
307
        },
308
        regions: {
309
            value: null
310
        }
311
    }
312
});
313
 
314
M.core = M.core || {};
315
M.core.blockdraganddrop = M.core.blockdraganddrop || {};
316
 
317
/**
318
 * True if the page is using the new blocks methods.
319
 * @private
320
 * @static
321
 * @property M.core.blockdraganddrop._isusingnewblocksmethod
322
 * @type Boolean
323
 * @default null
324
 */
325
M.core.blockdraganddrop._isusingnewblocksmethod = null;
326
 
327
/**
328
 * Returns true if the page is using the new blocks methods.
329
 * @static
330
 * @method M.core.blockdraganddrop.is_using_blocks_render_method
331
 * @return Boolean
332
 */
333
M.core.blockdraganddrop.is_using_blocks_render_method = function() {
334
    if (this._isusingnewblocksmethod === null) {
335
        var goodregions = Y.all('.block-region[data-blockregion]').size();
336
        var allregions = Y.all('.block-region').size();
337
        this._isusingnewblocksmethod = (allregions === goodregions);
338
        if (goodregions > 0 && allregions > 0 && goodregions !== allregions) {
339
            Y.log('Both core_renderer::blocks and core_renderer::blocks_for_region have been used.', 'warn', 'moodle-core_blocks');
340
        }
341
    }
342
    return this._isusingnewblocksmethod;
343
};
344
 
345
/**
346
 * Initialises a drag and drop manager.
347
 * This should only ever be called once for a page.
348
 * @static
349
 * @method M.core.blockdraganddrop.init
350
 * @param {Object} params
351
 * @return Manager
352
 */
353
M.core.blockdraganddrop.init = function(params) {
354
    if (this.is_using_blocks_render_method()) {
355
        Y.log('Block drag and drop initialised for the blocks method.', 'info', 'moodle-core_blocks');
356
        new MANAGER(params);
357
    } else {
358
        Y.log('Block drag and drop initialised with the legacy manager (blocks_for_region used).', 'info', 'moodle-core_blocks');
359
        new DRAGBLOCK(params);
360
    }
361
};
362
 
363
/*
364
 * Legacy code to keep things working.
365
 */
366
M.core_blocks = M.core_blocks || {};
367
M.core_blocks.init_dragdrop = function(params) {
368
    M.core.blockdraganddrop.init(params);
369
};