2016-06-13 07:49:05 +00:00
|
|
|
YUI.add('moodle-mod_customcert-rearrange', function (Y, NAME) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rearrange elements in the custom certificate
|
|
|
|
*
|
|
|
|
* @class M.mod_customcert.rearrange.rearrange
|
|
|
|
* @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
|
|
|
*/
|
2016-02-16 09:03:34 +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.
|
|
|
|
*/
|
2016-06-13 07:49:05 +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.
|
|
|
|
*/
|
2016-06-13 07:49:05 +00:00
|
|
|
elements : [],
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the X coordinates of the top left of the pdf div.
|
|
|
|
*/
|
|
|
|
pdfx : 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the Y coordinates of the top left of the pdf div.
|
|
|
|
*/
|
|
|
|
pdfy : 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the width of the pdf div.
|
|
|
|
*/
|
|
|
|
pdfwidth : 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the height of the pdf div.
|
|
|
|
*/
|
|
|
|
pdfheight : 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the location of the element before we move.
|
|
|
|
*/
|
|
|
|
elementxy : 0,
|
|
|
|
|
2015-12-08 09:41:38 +00:00
|
|
|
/**
|
|
|
|
* Store the left boundary of the pdf div.
|
|
|
|
*/
|
|
|
|
pdfleftboundary : 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the right boundary of the pdf div.
|
|
|
|
*/
|
|
|
|
pdfrightboundary : 0,
|
|
|
|
|
2015-08-05 05:38:35 +00:00
|
|
|
/**
|
|
|
|
* The number of pixels in a mm.
|
|
|
|
*/
|
2016-06-13 07:49:05 +00:00
|
|
|
pixelsinmm : 3.779527559055, // '3.779528'.
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise.
|
|
|
|
*
|
2016-06-13 07:49:05 +00:00
|
|
|
* @param params
|
2015-08-05 05:38:35 +00:00
|
|
|
*/
|
2016-06-13 07:49:05 +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();
|
|
|
|
this.pdfwidth = parseFloat(Y.one('#pdf').getComputedStyle('width'), 10);
|
|
|
|
this.pdfheight = parseFloat(Y.one('#pdf').getComputedStyle('height'), 10);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-13 07:49:05 +00:00
|
|
|
this.set_data();
|
|
|
|
this.set_positions();
|
|
|
|
this.create_events();
|
2015-08-05 05:38:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the additional data for the elements.
|
|
|
|
*/
|
2016-06-13 07:49:05 +00:00
|
|
|
set_data : function() {
|
2015-08-05 05:38:35 +00:00
|
|
|
// Go through the elements and set their reference points.
|
|
|
|
for (var key in this.elements) {
|
|
|
|
var element = this.elements[key];
|
2016-06-13 07:49:05 +00:00
|
|
|
Y.one('#element-' + element.id).setData('refpoint', element.refpoint);
|
|
|
|
Y.one('#element-' + element.id).setData('width', element.width);
|
2015-08-05 05:38:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current position of the elements.
|
|
|
|
*/
|
2016-06-13 07:49:05 +00:00
|
|
|
set_positions : 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;
|
|
|
|
var nodewidth = parseFloat(Y.one('#element-' + element.id).getComputedStyle('width'), 10);
|
|
|
|
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
|
|
|
Y.one('#element-' + element.id).setStyle('width', nodewidth + 'px');
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2016-06-13 07:49:05 +00:00
|
|
|
switch (element.refpoint) {
|
2015-08-05 05:38:35 +00:00
|
|
|
case '1': // Top-center
|
|
|
|
posx -= nodewidth / 2;
|
|
|
|
break;
|
|
|
|
case '2': // Top-right
|
|
|
|
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.
|
|
|
|
*/
|
2016-06-13 07:49:05 +00:00
|
|
|
create_events : 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) {
|
|
|
|
this.save_positions(e);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
// Trigger a save event when apply button is pushed.
|
|
|
|
Y.one('.applypositionsbtn input[type=submit]').on('click', function(e) {
|
|
|
|
this.save_positions(e);
|
|
|
|
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.elementwidth = node.getComputedStyle('width');
|
|
|
|
}, 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');
|
|
|
|
if (this.is_out_of_bounds(node)) {
|
|
|
|
node.setXY(this.elementxy);
|
|
|
|
node.setStyle('width', this.elementwidth);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if any part of the element is placed outside of the PDF div, false otherwise.
|
|
|
|
*
|
|
|
|
* @param node
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
is_out_of_bounds : function(node) {
|
|
|
|
// Get the width and height of the node.
|
|
|
|
var nodewidth = parseFloat(node.getComputedStyle('width'), 10);
|
|
|
|
var nodeheight = parseFloat(node.getComputedStyle('height'), 10);
|
|
|
|
|
|
|
|
// 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.
|
2015-12-08 09:41:38 +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.
|
|
|
|
if ((top < this.pdfy) || (bottom > (this.pdfy + this.pdfheight))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an AJAX call and save the positions of the elements.
|
|
|
|
*
|
|
|
|
* @param e
|
|
|
|
*/
|
|
|
|
save_positions : function(e) {
|
|
|
|
// 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
|
|
|
|
|
|
|
// Get the current X and Y positions for this element.
|
|
|
|
var posx = node.getX() - this.pdfx;
|
|
|
|
var posy = node.getY() - this.pdfy;
|
|
|
|
|
|
|
|
var nodewidth = parseFloat(node.getComputedStyle('width'), 10);
|
|
|
|
|
2016-06-13 07:49:05 +00:00
|
|
|
switch (element.refpoint) {
|
2015-08-05 05:38:35 +00:00
|
|
|
case '1': // Top-center
|
|
|
|
posx += nodewidth / 2;
|
|
|
|
break;
|
|
|
|
case '2': // Top-right
|
|
|
|
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,
|
2015-08-05 05:38:35 +00:00
|
|
|
posx: Math.round(parseFloat(posx / this.pixelsinmm, 10)),
|
|
|
|
posy: Math.round(parseFloat(posy / this.pixelsinmm, 10))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
params.values = JSON.stringify(params.values);
|
|
|
|
|
|
|
|
// Save these positions.
|
|
|
|
Y.io(M.cfg.wwwroot + '/mod/customcert/rest.php', {
|
|
|
|
method: 'POST',
|
|
|
|
data: params,
|
|
|
|
on: {
|
|
|
|
failure: function(tid, response) {
|
|
|
|
this.ajax_failure(response);
|
2015-12-09 05:34:52 +00:00
|
|
|
},
|
|
|
|
success: function() {
|
|
|
|
var formNode = e.currentTarget.ancestor('form', true);
|
2016-02-16 09:03:34 +00:00
|
|
|
var baseUrl = formNode.getAttribute('action');
|
|
|
|
var pageinput = formNode.one('[name=pid]');
|
|
|
|
if (pageinput) {
|
|
|
|
var pageid = pageinput.get('value');
|
|
|
|
window.location = baseUrl + '?pid=' + pageid;
|
|
|
|
} else {
|
|
|
|
var templateid = formNode.one('[name=tid]').get('value');
|
|
|
|
window.location = baseUrl + '?tid=' + templateid;
|
|
|
|
}
|
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.
|
|
|
|
*
|
|
|
|
* @param response
|
|
|
|
* @returns {M.core.exception}
|
|
|
|
*/
|
|
|
|
ajax_failure : function(response) {
|
|
|
|
var e = {
|
|
|
|
name: response.status + ' ' + response.statusText,
|
|
|
|
message: response.responseText
|
|
|
|
};
|
|
|
|
return new M.core.exception(e);
|
|
|
|
}
|
2016-06-13 07:49:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
M.mod_customcert = M.mod_customcert || {};
|
|
|
|
M.mod_customcert.rearrange = M.mod_customcert.rearrange || {};
|
|
|
|
M.mod_customcert.rearrange.init = function(templateid, page, elements) {
|
|
|
|
new Rearrange(templateid, page, elements);
|
|
|
|
};
|
|
|
|
|
|
|
|
}, '@VERSION@', {"requires": ["dd-delegate", "dd-drag"]});
|