Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('moodle-mod_customcert-rearrange', function (Y, NAME) {
2
 
3
/**
4
 * Rearrange elements in the custom certificate
5
 *
6
 * @class Y.M.mod_customcert.rearrange
7
 * @constructor
8
 */
9
var Rearrange = function() {
10
    Rearrange.superclass.constructor.apply(this, [arguments]);
11
};
12
Y.extend(Rearrange, Y.Base, {
13
 
14
    /**
15
     * The template id.
16
     */
17
    templateid: 0,
18
 
19
    /**
20
     * The customcert page we are displaying.
21
     */
22
    page: [],
23
 
24
    /**
25
     * The custom certificate elements to display.
26
     */
27
    elements: [],
28
 
29
    /**
30
     * Store the X coordinates of the top left of the pdf div.
31
     */
32
    pdfx: 0,
33
 
34
    /**
35
     * Store the Y coordinates of the top left of the pdf div.
36
     */
37
    pdfy: 0,
38
 
39
    /**
40
     * Store the width of the pdf div.
41
     */
42
    pdfwidth: 0,
43
 
44
    /**
45
     * Store the height of the pdf div.
46
     */
47
    pdfheight: 0,
48
 
49
    /**
50
     * Store the location of the element before we move.
51
     */
52
    elementxy: 0,
53
 
54
    /**
55
     * Store the left boundary of the pdf div.
56
     */
57
    pdfleftboundary: 0,
58
 
59
    /**
60
     * Store the right boundary of the pdf div.
61
     */
62
    pdfrightboundary: 0,
63
 
64
    /**
65
     * The number of pixels in a mm.
66
     */
67
    pixelsinmm: 3.779527559055, // 3.779528.
68
 
69
    /**
70
     * Initialise.
71
     *
72
     * @param {Array} params
73
     */
74
    initializer: function(params) {
75
        // Set the course module id.
76
        this.templateid = params[0];
77
        // Set the page.
78
        this.page = params[1];
79
        // Set the elements.
80
        this.elements = params[2];
81
 
82
        // Set the PDF dimensions.
83
        this.setPdfDimensions();
84
 
85
        // Set the boundaries.
86
        this.setBoundaries();
87
 
88
        this.setpositions();
89
        this.createevents();
90
        window.addEventListener("resize", this.checkWindownResize.bind(this));
91
    },
92
 
93
    /**
94
     * Sets the current position of the elements.
95
     */
96
    setpositions: function() {
97
        // Go through the elements and set their positions.
98
        for (var key in this.elements) {
99
            var element = this.elements[key];
100
            var posx = this.pdfx + element.posx * this.pixelsinmm;
101
            var posy = this.pdfy + element.posy * this.pixelsinmm;
102
            var nodewidth = parseFloat(Y.one('#element-' + element.id).getComputedStyle('width'));
103
            var maxwidth = element.width * this.pixelsinmm;
104
 
105
            if (maxwidth && (nodewidth > maxwidth)) {
106
                nodewidth = maxwidth;
107
            }
108
 
109
            switch (element.refpoint) {
110
                case '1': // Top-center.
111
                    posx -= nodewidth / 2;
112
                    break;
113
                case '2': // Top-right.
114
                    posx = posx - nodewidth + 2;
115
                    break;
116
            }
117
 
118
            Y.one('#element-' + element.id).setX(posx);
119
            Y.one('#element-' + element.id).setY(posy);
120
        }
121
    },
122
 
123
    /**
124
     * Sets the PDF dimensions.
125
     */
126
    setPdfDimensions: function() {
127
        this.pdfx = Y.one('#pdf').getX();
128
        this.pdfy = Y.one('#pdf').getY();
129
        this.pdfwidth = parseFloat(Y.one('#pdf').getComputedStyle('width'));
130
        this.pdfheight = parseFloat(Y.one('#pdf').getComputedStyle('height'));
131
    },
132
 
133
    /**
134
     * Sets the boundaries.
135
     */
136
    setBoundaries: function() {
137
        this.pdfleftboundary = this.pdfx;
138
        if (this.page.leftmargin) {
139
            this.pdfleftboundary += parseInt(this.page.leftmargin * this.pixelsinmm, 10);
140
        }
141
 
142
        this.pdfrightboundary = this.pdfx + this.pdfwidth;
143
        if (this.page.rightmargin) {
144
            this.pdfrightboundary -= parseInt(this.page.rightmargin * this.pixelsinmm, 10);
145
        }
146
    },
147
 
148
    /**
149
     * Check browser resize and reset position.
150
     */
151
    checkWindownResize: function() {
152
        this.setPdfDimensions();
153
        this.setBoundaries();
154
        this.setpositions();
155
    },
156
 
157
    /**
158
     * Creates the JS events for changing element positions.
159
     */
160
    createevents: function() {
161
        // Trigger a save event when save button is pushed.
162
        Y.one('.savepositionsbtn [type=submit]').on('click', function(e) {
163
            this.savepositions(e);
164
        }, this);
165
 
166
        // Trigger a save event when apply button is pushed.
167
        Y.one('.applypositionsbtn [type=submit]').on('click', function(e) {
168
            this.savepositions(e);
169
            e.preventDefault();
170
        }, this);
171
 
172
        // Define the container and the elements that are draggable.
173
        var del = new Y.DD.Delegate({
174
            container: '#pdf',
175
            nodes: '.element'
176
        });
177
 
178
        // When we start dragging keep track of it's position as we may set it back.
179
        del.on('drag:start', function() {
180
            var node = del.get('currentNode');
181
            this.elementxy = node.getXY();
182
        }, this);
183
 
184
        // When we finish the dragging action check that the node is in bounds,
185
        // if not, set it back to where it was.
186
        del.on('drag:end', function() {
187
            var node = del.get('currentNode');
188
            if (this.isoutofbounds(node)) {
189
                node.setXY(this.elementxy);
190
            }
191
        }, this);
192
    },
193
 
194
    /**
195
     * Returns true if any part of the element is placed outside of the PDF div, false otherwise.
196
     *
197
     * @param {Node} node
198
     * @returns {boolean}
199
     */
200
    isoutofbounds: function(node) {
201
        // Get the width and height of the node.
202
        var nodewidth = parseFloat(node.getComputedStyle('width'));
203
        var nodeheight = parseFloat(node.getComputedStyle('height'));
204
 
205
        // Store the positions of each edge of the node.
206
        var left = node.getX();
207
        var right = left + nodewidth;
208
        var top = node.getY();
209
        var bottom = top + nodeheight;
210
 
211
        this.pdfx = Y.one('#pdf').getX();
212
        this.pdfy = Y.one('#pdf').getY();
213
 
214
        // Check if it is out of bounds horizontally.
215
        if ((left < this.pdfleftboundary) || (right > this.pdfrightboundary)) {
216
            return true;
217
        }
218
 
219
        // Check if it is out of bounds vertically.
220
        if ((top < this.pdfy) || (bottom > (this.pdfy + this.pdfheight))) {
221
            return true;
222
        }
223
 
224
        return false;
225
    },
226
 
227
    /**
228
     * Perform an AJAX call and save the positions of the elements.
229
     *
230
     * @param {Event} e
231
     */
232
    savepositions: function(e) {
233
        // The parameters to send the AJAX call.
234
        var params = {
235
            tid: this.templateid,
236
            values: []
237
        };
238
 
239
        this.pdfx = Y.one("#pdf").getX();
240
        this.pdfy = Y.one("#pdf").getY();
241
 
242
        // Go through the elements and save their positions.
243
        for (var key in this.elements) {
244
            var element = this.elements[key];
245
            var node = Y.one('#element-' + element.id);
246
 
247
            // Get the current X and Y positions and refpoint for this element.
248
            var posx = node.getX() - this.pdfx;
249
            var posy = node.getY() - this.pdfy;
250
            var refpoint = node.getData('refpoint');
251
 
252
            var nodewidth = parseFloat(node.getComputedStyle('width'));
253
 
254
            switch (refpoint) {
255
                case '1': // Top-center.
256
                    posx += nodewidth / 2;
257
                    break;
258
                case '2': // Top-right.
259
                    posx += nodewidth;
260
                    break;
261
            }
262
 
263
            // Set the parameters to pass to the AJAX request.
264
            params.values.push({
265
                id: element.id,
266
                posx: Math.round(parseFloat(posx / this.pixelsinmm)),
267
                posy: Math.round(parseFloat(posy / this.pixelsinmm))
268
            });
269
        }
270
 
271
        params.values = JSON.stringify(params.values);
272
 
273
        // Save these positions.
274
        Y.io(M.cfg.wwwroot + '/mod/customcert/ajax.php', {
275
            method: 'POST',
276
            data: params,
277
            on: {
278
                failure: function(tid, response) {
279
                    this.ajaxfailure(response);
280
                },
281
                success: function() {
282
                    var formnode = e.currentTarget.ancestor('form', true);
283
                    var baseurl = formnode.getAttribute('action');
284
                    var pageinput = formnode.one('[name=pid]');
285
                    if (pageinput) {
286
                        var pageid = pageinput.get('value');
287
                        window.location = baseurl + '?pid=' + pageid;
288
                    } else {
289
                        var templateid = formnode.one('[name=tid]').get('value');
290
                        window.location = baseurl + '?tid=' + templateid;
291
                    }
292
                }
293
            },
294
            context: this
295
        });
296
 
297
        e.preventDefault();
298
    },
299
 
300
    /**
301
     * Handles any failures during an AJAX call.
302
     *
303
     * @param {XMLHttpRequest} response
304
     * @returns {M.core.exception}
305
     */
306
    ajaxfailure: function(response) {
307
        var e = {
308
            name: response.status + ' ' + response.statusText,
309
            message: response.responseText
310
        };
311
        return new M.core.exception(e);
312
    }
313
});
314
 
315
Y.namespace('M.mod_customcert.rearrange').init = function(templateid, page, elements) {
316
    new Rearrange(templateid, page, elements);
317
};
318
 
319
 
320
}, '@VERSION@', {"requires": ["dd-delegate", "dd-drag"]});