moodle-mod-customcert/yui/build/moodle-mod_customcert-rearrange/moodle-mod_customcert-rearrange-debug.js

291 lines
8.4 KiB
JavaScript
Raw Normal View History

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
* @constructor
*/
var Rearrange = function() {
Rearrange.superclass.constructor.apply(this, [arguments]);
};
Y.extend(Rearrange, Y.Base, {
/**
* The template id.
*/
2017-02-16 12:12:19 +00:00
templateid: 0,
/**
* The customcert page we are displaying.
*/
2017-02-16 12:12:19 +00:00
page: [],
/**
* The custom certificate elements to display.
*/
2017-02-16 12:12:19 +00:00
elements: [],
/**
* Store the X coordinates of the top left of the pdf div.
*/
2017-02-16 12:12:19 +00:00
pdfx: 0,
/**
* Store the Y coordinates of the top left of the pdf div.
*/
2017-02-16 12:12:19 +00:00
pdfy: 0,
/**
* Store the width of the pdf div.
*/
2017-02-16 12:12:19 +00:00
pdfwidth: 0,
/**
* Store the height of the pdf div.
*/
2017-02-16 12:12:19 +00:00
pdfheight: 0,
/**
* Store the location of the element before we move.
*/
2017-02-16 12:12:19 +00:00
elementxy: 0,
/**
* Store the left boundary of the pdf div.
*/
2017-02-16 12:12:19 +00:00
pdfleftboundary: 0,
/**
* Store the right boundary of the pdf div.
*/
2017-02-16 12:12:19 +00:00
pdfrightboundary: 0,
/**
* The number of pixels in a mm.
*/
2017-02-16 12:12:19 +00:00
pixelsinmm: 3.779527559055, // 3.779528.
/**
* Initialise.
*
2017-02-16 12:12:19 +00:00
* @param {Array} params
*/
2017-02-16 12:12:19 +00:00
initializer: function(params) {
// Set the course module id.
this.templateid = params[0];
// Set the page.
this.page = params[1];
// Set the elements.
this.elements = params[2];
// Set the PDF dimensions.
this.pdfx = Y.one('#pdf').getX();
this.pdfy = Y.one('#pdf').getY();
this.pdfwidth = parseFloat(Y.one('#pdf').getComputedStyle('width'));
this.pdfheight = parseFloat(Y.one('#pdf').getComputedStyle('height'));
// Set the boundaries.
this.pdfleftboundary = this.pdfx;
if (this.page.leftmargin) {
this.pdfleftboundary += parseInt(this.page.leftmargin * this.pixelsinmm, 10);
}
this.pdfrightboundary = this.pdfx + this.pdfwidth;
if (this.page.rightmargin) {
this.pdfrightboundary -= parseInt(this.page.rightmargin * this.pixelsinmm, 10);
}
2017-02-16 12:12:19 +00:00
this.setpositions();
this.createevents();
},
/**
* Sets the current position of the elements.
*/
2017-02-16 12:12:19 +00:00
setpositions: function() {
// Go through the elements and set their positions.
for (var key in this.elements) {
var element = this.elements[key];
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'));
var maxwidth = element.width * this.pixelsinmm;
if (maxwidth && (nodewidth > maxwidth)) {
nodewidth = maxwidth;
}
switch (element.refpoint) {
2017-02-16 12:12:19 +00:00
case '1': // Top-center.
posx -= nodewidth / 2;
break;
2017-02-16 12:12:19 +00:00
case '2': // Top-right.
posx = posx - nodewidth + 2;
break;
}
Y.one('#element-' + element.id).setX(posx);
Y.one('#element-' + element.id).setY(posy);
}
},
/**
* Creates the JS events for changing element positions.
*/
2017-02-16 12:12:19 +00:00
createevents: function() {
// Trigger a save event when save button is pushed.
Y.one('.savepositionsbtn [type=submit]').on('click', function(e) {
2017-02-16 12:12:19 +00:00
this.savepositions(e);
}, this);
// Trigger a save event when apply button is pushed.
Y.one('.applypositionsbtn [type=submit]').on('click', function(e) {
2017-02-16 12:12:19 +00:00
this.savepositions(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);
// 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)) {
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
* @returns {boolean}
*/
2017-02-16 12:12:19 +00:00
isoutofbounds: function(node) {
// Get the width and height of the node.
var nodewidth = parseFloat(node.getComputedStyle('width'));
var nodeheight = parseFloat(node.getComputedStyle('height'));
// 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)) {
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))) {
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
*/
2017-02-16 12:12:19 +00:00
savepositions: function(e) {
// The parameters to send the AJAX call.
var params = {
tid: this.templateid,
values: []
};
// Go through the elements and save their positions.
for (var key in this.elements) {
var element = this.elements[key];
var node = Y.one('#element-' + element.id);
// Get the current X and Y positions and refpoint for this element.
var posx = node.getX() - this.pdfx;
var posy = node.getY() - this.pdfy;
var refpoint = node.getData('refpoint');
var nodewidth = parseFloat(node.getComputedStyle('width'));
switch (refpoint) {
2017-02-16 12:12:19 +00:00
case '1': // Top-center.
posx += nodewidth / 2;
break;
2017-02-16 12:12:19 +00:00
case '2': // Top-right.
posx += nodewidth;
break;
}
// Set the parameters to pass to the AJAX request.
params.values.push({
id: element.id,
posx: Math.round(parseFloat(posx / this.pixelsinmm)),
posy: Math.round(parseFloat(posy / this.pixelsinmm))
});
}
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', {
method: 'POST',
data: params,
on: {
failure: function(tid, response) {
2017-02-16 12:12:19 +00:00
this.ajaxfailure(response);
},
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]');
if (pageinput) {
var pageid = pageinput.get('value');
2017-02-16 12:12:19 +00:00
window.location = baseurl + '?pid=' + pageid;
} else {
2017-02-16 12:12:19 +00:00
var templateid = formnode.one('[name=tid]').get('value');
window.location = baseurl + '?tid=' + templateid;
}
}
},
context: this
});
e.preventDefault();
},
/**
* Handles any failures during an AJAX call.
*
2017-02-16 12:12:19 +00:00
* @param {XMLHttpRequest} response
* @returns {M.core.exception}
*/
2017-02-16 12:12:19 +00:00
ajaxfailure: function(response) {
var e = {
name: response.status + ' ' + response.statusText,
message: response.responseText
};
return new M.core.exception(e);
}
});
2017-02-16 12:12:19 +00:00
Y.namespace('M.mod_customcert.rearrange').init = function(templateid, page, elements) {
new Rearrange(templateid, page, elements);
};
2017-02-16 12:12:19 +00:00
}, '@VERSION@', {"requires": ["dd-delegate", "dd-drag"]});