2016-06-13 07:49:05 +00:00
|
|
|
YUI.add('moodle-mod_customcert-rearrange', function (Y, NAME) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rearrange elements in the custom certificate
|
|
|
|
*
|
2017-02-16 12:12:19 +00:00
|
|
|
* @class Y.M.mod_customcert.rearrange
|
2016-06-13 07:49:05 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
var Rearrange = function() {
|
|
|
|
Rearrange.superclass.constructor.apply(this, [arguments]);
|
|
|
|
};
|
|
|
|
Y.extend(Rearrange, Y.Base, {
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-13 07:49:05 +00:00
|
|
|
* The template id.
|
2015-08-05 05:38:35 +00:00
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
templateid: 0,
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2015-12-08 09:41:38 +00:00
|
|
|
/**
|
|
|
|
* The customcert page we are displaying.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
page: [],
|
2015-12-08 09:41:38 +00:00
|
|
|
|
2015-08-05 05:38:35 +00:00
|
|
|
/**
|
|
|
|
* The custom certificate elements to display.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
elements: [],
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the X coordinates of the top left of the pdf div.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
pdfx: 0,
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the Y coordinates of the top left of the pdf div.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
pdfy: 0,
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the width of the pdf div.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
pdfwidth: 0,
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the height of the pdf div.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
pdfheight: 0,
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the location of the element before we move.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
elementxy: 0,
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2015-12-08 09:41:38 +00:00
|
|
|
/**
|
|
|
|
* Store the left boundary of the pdf div.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
pdfleftboundary: 0,
|
2015-12-08 09:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the right boundary of the pdf div.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
pdfrightboundary: 0,
|
2015-12-08 09:41:38 +00:00
|
|
|
|
2015-08-05 05:38:35 +00:00
|
|
|
/**
|
|
|
|
* The number of pixels in a mm.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
pixelsinmm: 3.779527559055, // 3.779528.
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise.
|
|
|
|
*
|
2017-02-16 12:12:19 +00:00
|
|
|
* @param {Array} params
|
2015-08-05 05:38:35 +00:00
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
initializer: function(params) {
|
2015-08-05 05:38:35 +00:00
|
|
|
// Set the course module id.
|
2016-06-13 07:49:05 +00:00
|
|
|
this.templateid = params[0];
|
2015-12-08 09:41:38 +00:00
|
|
|
// Set the page.
|
2016-06-13 07:49:05 +00:00
|
|
|
this.page = params[1];
|
2015-08-05 05:38:35 +00:00
|
|
|
// Set the elements.
|
2016-06-13 07:49:05 +00:00
|
|
|
this.elements = params[2];
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
// Set the PDF dimensions.
|
|
|
|
this.pdfx = Y.one('#pdf').getX();
|
|
|
|
this.pdfy = Y.one('#pdf').getY();
|
2017-01-30 08:56:53 +00:00
|
|
|
this.pdfwidth = parseFloat(Y.one('#pdf').getComputedStyle('width'));
|
|
|
|
this.pdfheight = parseFloat(Y.one('#pdf').getComputedStyle('height'));
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2015-12-08 09:41:38 +00:00
|
|
|
// Set the boundaries.
|
|
|
|
this.pdfleftboundary = this.pdfx;
|
2016-06-13 07:49:05 +00:00
|
|
|
if (this.page.leftmargin) {
|
|
|
|
this.pdfleftboundary += parseInt(this.page.leftmargin * this.pixelsinmm, 10);
|
2015-12-08 09:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.pdfrightboundary = this.pdfx + this.pdfwidth;
|
2016-06-13 07:49:05 +00:00
|
|
|
if (this.page.rightmargin) {
|
|
|
|
this.pdfrightboundary -= parseInt(this.page.rightmargin * this.pixelsinmm, 10);
|
2015-12-08 09:41:38 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 12:12:19 +00:00
|
|
|
this.setpositions();
|
|
|
|
this.createevents();
|
2015-08-05 05:38:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current position of the elements.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
setpositions: function() {
|
2015-08-05 05:38:35 +00:00
|
|
|
// Go through the elements and set their positions.
|
|
|
|
for (var key in this.elements) {
|
|
|
|
var element = this.elements[key];
|
2016-06-13 07:49:05 +00:00
|
|
|
var posx = this.pdfx + element.posx * this.pixelsinmm;
|
|
|
|
var posy = this.pdfy + element.posy * this.pixelsinmm;
|
2017-01-30 08:56:53 +00:00
|
|
|
var nodewidth = parseFloat(Y.one('#element-' + element.id).getComputedStyle('width'));
|
2016-06-13 07:49:05 +00:00
|
|
|
var maxwidth = element.width * this.pixelsinmm;
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
if (maxwidth && (nodewidth > maxwidth)) {
|
|
|
|
nodewidth = maxwidth;
|
|
|
|
}
|
|
|
|
|
2016-06-13 07:49:05 +00:00
|
|
|
switch (element.refpoint) {
|
2017-02-16 12:12:19 +00:00
|
|
|
case '1': // Top-center.
|
2015-08-05 05:38:35 +00:00
|
|
|
posx -= nodewidth / 2;
|
|
|
|
break;
|
2017-02-16 12:12:19 +00:00
|
|
|
case '2': // Top-right.
|
2015-08-05 05:38:35 +00:00
|
|
|
posx = posx - nodewidth + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-13 07:49:05 +00:00
|
|
|
Y.one('#element-' + element.id).setX(posx);
|
|
|
|
Y.one('#element-' + element.id).setY(posy);
|
2015-08-05 05:38:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the JS events for changing element positions.
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
createevents: function() {
|
2015-08-05 05:38:35 +00:00
|
|
|
// Trigger a save event when save button is pushed.
|
|
|
|
Y.one('.savepositionsbtn input[type=submit]').on('click', function(e) {
|
2017-02-16 12:12:19 +00:00
|
|
|
this.savepositions(e);
|
2015-08-05 05:38:35 +00:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
// Trigger a save event when apply button is pushed.
|
|
|
|
Y.one('.applypositionsbtn input[type=submit]').on('click', function(e) {
|
2017-02-16 12:12:19 +00:00
|
|
|
this.savepositions(e);
|
2015-08-05 05:38:35 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
// Define the container and the elements that are draggable.
|
|
|
|
var del = new Y.DD.Delegate({
|
|
|
|
container: '#pdf',
|
|
|
|
nodes: '.element'
|
|
|
|
});
|
|
|
|
|
|
|
|
// When we start dragging keep track of it's position as we may set it back.
|
|
|
|
del.on('drag:start', function() {
|
|
|
|
var node = del.get('currentNode');
|
|
|
|
this.elementxy = node.getXY();
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
// When we finish the dragging action check that the node is in bounds,
|
|
|
|
// if not, set it back to where it was.
|
|
|
|
del.on('drag:end', function() {
|
|
|
|
var node = del.get('currentNode');
|
2017-02-16 12:12:19 +00:00
|
|
|
if (this.isoutofbounds(node)) {
|
2015-08-05 05:38:35 +00:00
|
|
|
node.setXY(this.elementxy);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if any part of the element is placed outside of the PDF div, false otherwise.
|
|
|
|
*
|
2017-02-16 12:12:19 +00:00
|
|
|
* @param {Node} node
|
2015-08-05 05:38:35 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
isoutofbounds: function(node) {
|
2015-08-05 05:38:35 +00:00
|
|
|
// Get the width and height of the node.
|
2017-01-30 08:56:53 +00:00
|
|
|
var nodewidth = parseFloat(node.getComputedStyle('width'));
|
|
|
|
var nodeheight = parseFloat(node.getComputedStyle('height'));
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
// Store the positions of each edge of the node.
|
|
|
|
var left = node.getX();
|
|
|
|
var right = left + nodewidth;
|
|
|
|
var top = node.getY();
|
|
|
|
var bottom = top + nodeheight;
|
|
|
|
|
|
|
|
// Check if it is out of bounds horizontally.
|
2017-02-16 12:12:19 +00:00
|
|
|
if ((left < this.pdfleftboundary) || (right > this.pdfrightboundary)) {
|
2015-08-05 05:38:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it is out of bounds vertically.
|
2017-02-16 12:12:19 +00:00
|
|
|
if ((top < this.pdfy) || (bottom > (this.pdfy + this.pdfheight))) {
|
2015-08-05 05:38:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an AJAX call and save the positions of the elements.
|
|
|
|
*
|
2017-02-16 12:12:19 +00:00
|
|
|
* @param {Event} e
|
2015-08-05 05:38:35 +00:00
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
savepositions: function(e) {
|
2015-08-05 05:38:35 +00:00
|
|
|
// The parameters to send the AJAX call.
|
|
|
|
var params = {
|
2016-02-16 09:03:34 +00:00
|
|
|
tid: this.templateid,
|
2015-08-05 05:38:35 +00:00
|
|
|
values: []
|
|
|
|
};
|
|
|
|
|
|
|
|
// Go through the elements and save their positions.
|
|
|
|
for (var key in this.elements) {
|
|
|
|
var element = this.elements[key];
|
2016-06-13 07:49:05 +00:00
|
|
|
var node = Y.one('#element-' + element.id);
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2017-01-30 08:56:53 +00:00
|
|
|
// Get the current X and Y positions and refpoint for this element.
|
2015-08-05 05:38:35 +00:00
|
|
|
var posx = node.getX() - this.pdfx;
|
|
|
|
var posy = node.getY() - this.pdfy;
|
2017-01-30 08:56:53 +00:00
|
|
|
var refpoint = node.getData('refpoint');
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2017-01-30 08:56:53 +00:00
|
|
|
var nodewidth = parseFloat(node.getComputedStyle('width'));
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2017-01-30 08:56:53 +00:00
|
|
|
switch (refpoint) {
|
2017-02-16 12:12:19 +00:00
|
|
|
case '1': // Top-center.
|
2015-08-05 05:38:35 +00:00
|
|
|
posx += nodewidth / 2;
|
|
|
|
break;
|
2017-02-16 12:12:19 +00:00
|
|
|
case '2': // Top-right.
|
2015-08-05 05:38:35 +00:00
|
|
|
posx += nodewidth;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the parameters to pass to the AJAX request.
|
|
|
|
params.values.push({
|
2016-06-13 07:49:05 +00:00
|
|
|
id: element.id,
|
2017-01-30 08:56:53 +00:00
|
|
|
posx: Math.round(parseFloat(posx / this.pixelsinmm)),
|
|
|
|
posy: Math.round(parseFloat(posy / this.pixelsinmm))
|
2015-08-05 05:38:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
params.values = JSON.stringify(params.values);
|
|
|
|
|
|
|
|
// Save these positions.
|
2016-06-15 04:51:16 +00:00
|
|
|
Y.io(M.cfg.wwwroot + '/mod/customcert/ajax.php', {
|
2015-08-05 05:38:35 +00:00
|
|
|
method: 'POST',
|
|
|
|
data: params,
|
|
|
|
on: {
|
|
|
|
failure: function(tid, response) {
|
2017-02-16 12:12:19 +00:00
|
|
|
this.ajaxfailure(response);
|
2015-12-09 05:34:52 +00:00
|
|
|
},
|
|
|
|
success: function() {
|
2017-02-16 12:12:19 +00:00
|
|
|
var formnode = e.currentTarget.ancestor('form', true);
|
|
|
|
var baseurl = formnode.getAttribute('action');
|
|
|
|
var pageinput = formnode.one('[name=pid]');
|
2016-02-16 09:03:34 +00:00
|
|
|
if (pageinput) {
|
|
|
|
var pageid = pageinput.get('value');
|
2017-02-16 12:12:19 +00:00
|
|
|
window.location = baseurl + '?pid=' + pageid;
|
2016-02-16 09:03:34 +00:00
|
|
|
} else {
|
2017-02-16 12:12:19 +00:00
|
|
|
var templateid = formnode.one('[name=tid]').get('value');
|
|
|
|
window.location = baseurl + '?tid=' + templateid;
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
2015-08-05 05:38:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
context: this
|
2015-12-09 05:34:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
e.preventDefault();
|
2015-08-05 05:38:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles any failures during an AJAX call.
|
|
|
|
*
|
2017-02-16 12:12:19 +00:00
|
|
|
* @param {XMLHttpRequest} response
|
2015-08-05 05:38:35 +00:00
|
|
|
* @returns {M.core.exception}
|
|
|
|
*/
|
2017-02-16 12:12:19 +00:00
|
|
|
ajaxfailure: function(response) {
|
2015-08-05 05:38:35 +00:00
|
|
|
var e = {
|
|
|
|
name: response.status + ' ' + response.statusText,
|
|
|
|
message: response.responseText
|
|
|
|
};
|
|
|
|
return new M.core.exception(e);
|
|
|
|
}
|
2016-06-13 07:49:05 +00:00
|
|
|
});
|
|
|
|
|
2017-02-16 12:12:19 +00:00
|
|
|
Y.namespace('M.mod_customcert.rearrange').init = function(templateid, page, elements) {
|
2016-06-13 07:49:05 +00:00
|
|
|
new Rearrange(templateid, page, elements);
|
|
|
|
};
|
|
|
|
|
2017-02-16 12:12:19 +00:00
|
|
|
|
2016-06-13 07:49:05 +00:00
|
|
|
}, '@VERSION@', {"requires": ["dd-delegate", "dd-drag"]});
|