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