#64 Fixed incorrect element position after changing reference point

This commit is contained in:
Mark Nelson 2017-01-30 16:56:53 +08:00
parent 2c3f6d1419
commit 62f73e3db8
7 changed files with 51 additions and 81 deletions

View file

@ -1 +1 @@
define(["jquery","core/yui","core/fragment","mod_customcert/dialogue","core/notification","core/str","core/templates","core/ajax"],function(a,b,c,d,e,f,g,h){var i=function(b){this._node=a(b),this._setEvents()};return i.prototype.CUSTOMCERT_REF_POINT_TOPLEFT=0,i.prototype.CUSTOMCERT_REF_POINT_TOPCENTER=1,i.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT=2,i.prototype._setEvents=function(){this._node.on("click",".element",this._editElement.bind(this))},i.prototype._editElement=function(a){var g=a.currentTarget.id.substr(8),h=this._node.attr("data-contextid"),i={elementid:g};c.loadFragment("mod_customcert","editelement",h,i).done(function(a,c){f.get_string("editelement","mod_customcert").done(function(e){b.use("moodle-core-formchangechecker",function(){new d(e,"<div id='elementcontent'></div>",this._editElementDialogueConfig.bind(this,g,a,c),(void 0),(!0))}.bind(this))}.bind(this))}.bind(this)).fail(e.exception)},i.prototype._editElementDialogueConfig=function(b,c,d,e){g.replaceNode("#elementcontent",c,d);var f=a(e.getContent());f.on("click","#id_submitbutton",function(c){M.core_formchangechecker.reset_form_dirty_state(),this._saveElement(b).then(function(){this._getElementHTML(b).done(function(c){var d=this._node.find("#element-"+b),f=a("#id_refpoint").val(),g="";f==this.CUSTOMCERT_REF_POINT_TOPLEFT?g="refpoint-left":f==this.CUSTOMCERT_REF_POINT_TOPCENTER?g="refpoint-center":f==this.CUSTOMCERT_REF_POINT_TOPRIGHT&&(g="refpoint-right"),d.empty().append(c),d.removeClass(),d.addClass("element "+g),this._setPosition(b,f),e.close()}.bind(this))}.bind(this)),c.preventDefault()}.bind(this)),f.on("click","#id_cancel",function(a){e.close(),a.preventDefault()}.bind(this))},i.prototype._setPosition=function(c,d){var e=b.one("#element-"+c),f=3.779527559055,g=b.one("#pdf").getX(),h=b.one("#pdf").getY(),i=g+a("#editelementform #id_posx").val()*f,j=h+a("#editelementform #id_posy").val()*f,k=parseFloat(e.getComputedStyle("width")),l=e.width*this.pixelsinmm;switch(l&&k>l&&(k=l),d){case"1":i-=k/2;break;case"2":i=i-k+2}e.setX(i),e.setY(j)},i.prototype._getElementHTML=function(a){var b=this._node.attr("data-templateid"),c=h.call([{methodname:"mod_customcert_get_element_html",args:{templateid:b,elementid:a}}]);return c[0]},i.prototype._saveElement=function(b){var c=this._node.attr("data-templateid"),d=a("#editelementform").serializeArray(),e=h.call([{methodname:"mod_customcert_save_element",args:{templateid:c,elementid:b,values:d}}]);return e[0]},{init:function(a){new i(a)}}});
define(["jquery","core/yui","core/fragment","mod_customcert/dialogue","core/notification","core/str","core/templates","core/ajax"],function(a,b,c,d,e,f,g,h){var i=function(b){this._node=a(b),this._setEvents()};return i.prototype.CUSTOMCERT_REF_POINT_TOPLEFT=0,i.prototype.CUSTOMCERT_REF_POINT_TOPCENTER=1,i.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT=2,i.prototype.PIXELSINMM=3.779527559055,i.prototype._setEvents=function(){this._node.on("click",".element",this._editElement.bind(this))},i.prototype._editElement=function(a){var g=a.currentTarget.id.substr(8),h=this._node.attr("data-contextid"),i={elementid:g};c.loadFragment("mod_customcert","editelement",h,i).done(function(a,c){f.get_string("editelement","mod_customcert").done(function(e){b.use("moodle-core-formchangechecker",function(){new d(e,"<div id='elementcontent'></div>",this._editElementDialogueConfig.bind(this,g,a,c),(void 0),(!0))}.bind(this))}.bind(this))}.bind(this)).fail(e.exception)},i.prototype._editElementDialogueConfig=function(b,c,d,e){g.replaceNode("#elementcontent",c,d);var f=a(e.getContent());f.on("click","#id_submitbutton",function(c){M.core_formchangechecker.reset_form_dirty_state(),this._saveElement(b).then(function(){this._getElementHTML(b).done(function(c){var d=this._node.find("#element-"+b),f=parseInt(a("#id_refpoint").val()),g="";f==this.CUSTOMCERT_REF_POINT_TOPLEFT?g="refpoint-left":f==this.CUSTOMCERT_REF_POINT_TOPCENTER?g="refpoint-center":f==this.CUSTOMCERT_REF_POINT_TOPRIGHT&&(g="refpoint-right"),d.empty().append(c),d.removeClass(),d.addClass("element "+g),d.attr("data-refpoint",f);var h=a("#editelementform #id_posx").val(),i=a("#editelementform #id_posy").val();this._setPosition(b,f,h,i),e.close()}.bind(this))}.bind(this)),c.preventDefault()}.bind(this)),f.on("click","#id_cancel",function(a){e.close(),a.preventDefault()}.bind(this))},i.prototype._setPosition=function(a,c,d,e){var f=b.one("#element-"+a),d=b.one("#pdf").getX()+d*this.PIXELSINMM,e=b.one("#pdf").getY()+e*this.PIXELSINMM,g=parseFloat(f.getComputedStyle("width")),h=f.width*this.PIXELSINMM;switch(h&&g>h&&(g=h),c){case this.CUSTOMCERT_REF_POINT_TOPCENTER:d-=g/2;break;case this.CUSTOMCERT_REF_POINT_TOPRIGHT:d=d-g+2}f.setX(d),f.setY(e)},i.prototype._getElementHTML=function(a){var b=this._node.attr("data-templateid"),c=h.call([{methodname:"mod_customcert_get_element_html",args:{templateid:b,elementid:a}}]);return c[0]},i.prototype._saveElement=function(b){var c=this._node.attr("data-templateid"),d=a("#editelementform").serializeArray(),e=h.call([{methodname:"mod_customcert_save_element",args:{templateid:c,elementid:b,values:d}}]);return e[0]},{init:function(a){new i(a)}}});

View file

@ -38,6 +38,7 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPLEFT = 0;
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPCENTER = 1;
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT = 2;
RearrangeArea.prototype.PIXELSINMM = 3.779527559055;
RearrangeArea.prototype._setEvents = function() {
this._node.on('click', '.element', this._editElement.bind(this));
@ -79,7 +80,7 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/
// Update the DOM to reflect the adjusted value.
this._getElementHTML(elementid).done(function(html) {
var elementNode = this._node.find('#element-' + elementid);
var refpoint = $('#id_refpoint').val();
var refpoint = parseInt($('#id_refpoint').val());
var refpointClass = '';
if (refpoint == this.CUSTOMCERT_REF_POINT_TOPLEFT) {
refpointClass = 'refpoint-left';
@ -92,8 +93,12 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/
// Update the ref point.
elementNode.removeClass();
elementNode.addClass('element ' + refpointClass);
// Move the element if we need to.
this._setPosition(elementid, refpoint);
elementNode.attr('data-refpoint', refpoint);
// Move the element.
var posx = $('#editelementform #id_posx').val();
var posy = $('#editelementform #id_posy').val();
this._setPosition(elementid, refpoint, posx, posy);
// All done.
popup.close();
}.bind(this));
}.bind(this));
@ -106,26 +111,23 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/
}.bind(this));
};
RearrangeArea.prototype._setPosition = function(elementid, refpoint) {
RearrangeArea.prototype._setPosition = function(elementid, refpoint, posx, posy) {
var element = Y.one('#element-' + elementid);
var pixelsinmm = 3.779527559055;
var pdfx = Y.one('#pdf').getX();
var pdfy = Y.one('#pdf').getY();
var posx = pdfx + $('#editelementform #id_posx').val() * pixelsinmm;
var posy = pdfy + $('#editelementform #id_posy').val() * pixelsinmm;
var posx = Y.one('#pdf').getX() + posx * this.PIXELSINMM;
var posy = Y.one('#pdf').getY() + posy * this.PIXELSINMM;
var nodewidth = parseFloat(element.getComputedStyle('width'));
var maxwidth = element.width * this.pixelsinmm;
var maxwidth = element.width * this.PIXELSINMM;
if (maxwidth && (nodewidth > maxwidth)) {
nodewidth = maxwidth;
}
switch (refpoint) {
case '1': // Top-center.
case this.CUSTOMCERT_REF_POINT_TOPCENTER:
posx -= nodewidth / 2;
break;
case '2': // Top-right.
case this.CUSTOMCERT_REF_POINT_TOPRIGHT:
posx = posx - nodewidth + 2;
break;
}