1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* AMD module used when rearranging a custom certificate.
|
|
|
18 |
*
|
|
|
19 |
* @module mod_customcert/rearrange-area
|
|
|
20 |
* @copyright 2016 Mark Nelson <markn@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/notification',
|
|
|
24 |
'core/str', 'core/templates', 'core/ajax'],
|
|
|
25 |
function($, Y, fragment, Dialogue, notification, str, template, ajax) {
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* RearrangeArea class.
|
|
|
29 |
*
|
|
|
30 |
* @param {String} selector The rearrange PDF selector
|
|
|
31 |
*/
|
|
|
32 |
var RearrangeArea = function(selector) {
|
|
|
33 |
this._node = $(selector);
|
|
|
34 |
this._setEvents();
|
|
|
35 |
};
|
|
|
36 |
|
|
|
37 |
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPLEFT = 0;
|
|
|
38 |
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPCENTER = 1;
|
|
|
39 |
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT = 2;
|
|
|
40 |
RearrangeArea.prototype.PIXELSINMM = 3.779527559055;
|
|
|
41 |
|
|
|
42 |
RearrangeArea.prototype._setEvents = function() {
|
|
|
43 |
this._node.on('click', '.element', this._editElement.bind(this));
|
|
|
44 |
};
|
|
|
45 |
|
|
|
46 |
RearrangeArea.prototype._editElement = function(event) {
|
|
|
47 |
var elementid = event.currentTarget.id.substr(8);
|
|
|
48 |
var contextid = this._node.attr('data-contextid');
|
|
|
49 |
var params = {
|
|
|
50 |
'elementid': elementid
|
|
|
51 |
};
|
|
|
52 |
|
|
|
53 |
fragment.loadFragment('mod_customcert', 'editelement', contextid, params).done(function(html, js) {
|
|
|
54 |
str.get_string('editelement', 'mod_customcert').done(function(title) {
|
|
|
55 |
Y.use('moodle-core-formchangechecker', function() {
|
|
|
56 |
new Dialogue(
|
|
|
57 |
title,
|
|
|
58 |
'<div id=\'elementcontent\'></div>',
|
|
|
59 |
this._editElementDialogueConfig.bind(this, elementid, html, js),
|
|
|
60 |
undefined,
|
|
|
61 |
true
|
|
|
62 |
);
|
|
|
63 |
}.bind(this));
|
|
|
64 |
}.bind(this));
|
|
|
65 |
}.bind(this)).fail(notification.exception);
|
|
|
66 |
};
|
|
|
67 |
|
|
|
68 |
RearrangeArea.prototype._editElementDialogueConfig = function(elementid, html, js, popup) {
|
|
|
69 |
// Place the content in the dialogue.
|
|
|
70 |
template.replaceNode('#elementcontent', html, js);
|
|
|
71 |
|
|
|
72 |
// We may have dragged the element changing it's position.
|
|
|
73 |
// Ensure the form has the current up-to-date location.
|
|
|
74 |
this._setPositionInForm(elementid);
|
|
|
75 |
|
|
|
76 |
// Add events for when we save, close and cancel the page.
|
|
|
77 |
var body = $(popup.getContent());
|
|
|
78 |
body.on('click', '#id_submitbutton', function(e) {
|
|
|
79 |
// Do not want to ask the user if they wish to stay on page after saving.
|
|
|
80 |
M.core_formchangechecker.reset_form_dirty_state();
|
|
|
81 |
// Save the data.
|
|
|
82 |
this._saveElement(elementid).then(function() {
|
|
|
83 |
// Update the DOM to reflect the adjusted value.
|
|
|
84 |
this._getElementHTML(elementid).done(function(html) {
|
|
|
85 |
var elementNode = this._node.find('#element-' + elementid);
|
|
|
86 |
var refpoint = parseInt($('#id_refpoint').val());
|
|
|
87 |
var refpointClass = '';
|
|
|
88 |
if (refpoint == this.CUSTOMCERT_REF_POINT_TOPLEFT) {
|
|
|
89 |
refpointClass = 'refpoint-left';
|
|
|
90 |
} else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPCENTER) {
|
|
|
91 |
refpointClass = 'refpoint-center';
|
|
|
92 |
} else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPRIGHT) {
|
|
|
93 |
refpointClass = 'refpoint-right';
|
|
|
94 |
}
|
|
|
95 |
elementNode.empty().append(html);
|
|
|
96 |
// Update the ref point.
|
|
|
97 |
elementNode.removeClass();
|
|
|
98 |
elementNode.addClass('element ' + refpointClass);
|
|
|
99 |
elementNode.attr('data-refpoint', refpoint);
|
|
|
100 |
// Move the element.
|
|
|
101 |
var posx = $('#editelementform #id_posx').val();
|
|
|
102 |
var posy = $('#editelementform #id_posy').val();
|
|
|
103 |
this._setPosition(elementid, refpoint, posx, posy);
|
|
|
104 |
// All done.
|
|
|
105 |
popup.close();
|
|
|
106 |
}.bind(this));
|
|
|
107 |
}.bind(this)).fail(notification.exception);
|
|
|
108 |
e.preventDefault();
|
|
|
109 |
}.bind(this));
|
|
|
110 |
|
|
|
111 |
body.on('click', '#id_cancel', function(e) {
|
|
|
112 |
popup.close();
|
|
|
113 |
e.preventDefault();
|
|
|
114 |
});
|
|
|
115 |
};
|
|
|
116 |
|
|
|
117 |
RearrangeArea.prototype._setPosition = function(elementid, refpoint, posx, posy) {
|
|
|
118 |
var element = Y.one('#element-' + elementid);
|
|
|
119 |
|
|
|
120 |
posx = Y.one('#pdf').getX() + posx * this.PIXELSINMM;
|
|
|
121 |
posy = Y.one('#pdf').getY() + posy * this.PIXELSINMM;
|
|
|
122 |
var nodewidth = parseFloat(element.getComputedStyle('width'));
|
|
|
123 |
var maxwidth = element.width * this.PIXELSINMM;
|
|
|
124 |
|
|
|
125 |
if (maxwidth && (nodewidth > maxwidth)) {
|
|
|
126 |
nodewidth = maxwidth;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
switch (refpoint) {
|
|
|
130 |
case this.CUSTOMCERT_REF_POINT_TOPCENTER:
|
|
|
131 |
posx -= nodewidth / 2;
|
|
|
132 |
break;
|
|
|
133 |
case this.CUSTOMCERT_REF_POINT_TOPRIGHT:
|
|
|
134 |
posx = posx - nodewidth + 2;
|
|
|
135 |
break;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
element.setX(posx);
|
|
|
139 |
element.setY(posy);
|
|
|
140 |
};
|
|
|
141 |
|
|
|
142 |
RearrangeArea.prototype._setPositionInForm = function(elementid) {
|
|
|
143 |
var posxelement = $('#editelementform #id_posx');
|
|
|
144 |
var posyelement = $('#editelementform #id_posy');
|
|
|
145 |
|
|
|
146 |
if (posxelement.length && posyelement.length) {
|
|
|
147 |
var element = Y.one('#element-' + elementid);
|
|
|
148 |
var posx = element.getX() - Y.one('#pdf').getX();
|
|
|
149 |
var posy = element.getY() - Y.one('#pdf').getY();
|
|
|
150 |
var refpoint = parseInt(element.getData('refpoint'));
|
|
|
151 |
var nodewidth = parseFloat(element.getComputedStyle('width'));
|
|
|
152 |
|
|
|
153 |
switch (refpoint) {
|
|
|
154 |
case this.CUSTOMCERT_REF_POINT_TOPCENTER:
|
|
|
155 |
posx += nodewidth / 2;
|
|
|
156 |
break;
|
|
|
157 |
case this.CUSTOMCERT_REF_POINT_TOPRIGHT:
|
|
|
158 |
posx += nodewidth;
|
|
|
159 |
break;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
posx = Math.round(parseFloat(posx / this.PIXELSINMM));
|
|
|
163 |
posy = Math.round(parseFloat(posy / this.PIXELSINMM));
|
|
|
164 |
|
|
|
165 |
posxelement.val(posx);
|
|
|
166 |
posyelement.val(posy);
|
|
|
167 |
}
|
|
|
168 |
};
|
|
|
169 |
|
|
|
170 |
RearrangeArea.prototype._getElementHTML = function(elementid) {
|
|
|
171 |
// Get the variables we need.
|
|
|
172 |
var templateid = this._node.attr('data-templateid');
|
|
|
173 |
|
|
|
174 |
// Call the web service to get the updated element.
|
|
|
175 |
var promises = ajax.call([{
|
|
|
176 |
methodname: 'mod_customcert_get_element_html',
|
|
|
177 |
args: {
|
|
|
178 |
templateid: templateid,
|
|
|
179 |
elementid: elementid
|
|
|
180 |
}
|
|
|
181 |
}]);
|
|
|
182 |
|
|
|
183 |
// Return the promise.
|
|
|
184 |
return promises[0];
|
|
|
185 |
};
|
|
|
186 |
|
|
|
187 |
RearrangeArea.prototype._saveElement = function(elementid) {
|
|
|
188 |
// Get the variables we need.
|
|
|
189 |
var templateid = this._node.attr('data-templateid');
|
|
|
190 |
var inputs = $('#editelementform').serializeArray();
|
|
|
191 |
|
|
|
192 |
// Call the web service to save the element.
|
|
|
193 |
var promises = ajax.call([{
|
|
|
194 |
methodname: 'mod_customcert_save_element',
|
|
|
195 |
args: {
|
|
|
196 |
templateid: templateid,
|
|
|
197 |
elementid: elementid,
|
|
|
198 |
values: inputs
|
|
|
199 |
}
|
|
|
200 |
}]);
|
|
|
201 |
|
|
|
202 |
// Return the promise.
|
|
|
203 |
return promises[0];
|
|
|
204 |
};
|
|
|
205 |
|
|
|
206 |
return {
|
|
|
207 |
init: function(selector) {
|
|
|
208 |
new RearrangeArea(selector);
|
|
|
209 |
}
|
|
|
210 |
};
|
|
|
211 |
}
|
|
|
212 |
);
|