Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

YUI.add("moodle-atto_image-button",function(m,e){var c={RESPONSIVE:"img-fluid",INPUTALIGNMENT:"atto_image_alignment",INPUTALT:"atto_image_altentry",INPUTHEIGHT:"atto_image_heightentry",INPUTSUBMIT:"atto_image_urlentrysubmit",INPUTURL:"atto_image_urlentry",INPUTSIZE:"atto_image_size",INPUTWIDTH:"atto_image_widthentry",IMAGEALTWARNING:"atto_image_altwarning",IMAGEURLWARNING:"atto_image_urlwarning",IMAGEBROWSER:"openimagebrowser",IMAGEPRESENTATION:"atto_image_presentation",INPUTCONSTRAIN:"atto_image_constrain",INPUTCUSTOMSTYLE:"atto_image_customstyle",IMAGEPREVIEW:"atto_image_preview",IMAGEPREVIEWBOX:"atto_image_preview_box",ALIGNSETTINGS:"atto_image_button"},i={URL:"urlentry",ALT:"altentry"},t={INPUTURL:"."+c.INPUTURL},s=[{name:"verticalAlign",str:"alignment_top",value:"text-top",margin:"0 0.5em"},{name:"verticalAlign",str:"alignment_middle",value:"middle",margin:"0 0.5em"},{name:"verticalAlign",str:"alignment_bottom",value:"text-bottom",margin:"0 0.5em",isDefault:!0},{name:"float",str:"alignment_left",value:"left",margin:"0 0.5em 0 0"},{name:"float",str:"alignment_right",value:"right",margin:"0 0 0 0.5em"}],a=160,o=160,u=/\d+%/,d="atto_image",I='<img src="{{url}}" alt="{{alt}}" {{#if width}}width="{{width}}" {{/if}}{{#if height}}height="{{height}}" {{/if}}{{#if presentation}}role="presentation" {{/if}}{{#if customstyle}}style="{{customstyle}}" {{/if}}{{#if classlist}}class="{{classlist}}" {{/if}}{{#if id}}id="{{id}}" {{/if}}/>';m.namespace("M.atto_image").Button=m.Base.create("button",m.M.editor_atto.EditorPlugin,[],{_currentSelection:null,_selectedImage:null,_form:null,_rawImageDimensions:null,initializer:function(){this.addButton({icon:"e/insert_edit_image",callback:this._displayDialogue,tags:"img",tagMatchRequiresAll:!1}),this.editor.delegate("dblclick",this._displayDialogue,"img",this),this.editor.delegate("click",this._handleClick,"img",this),this.editor.on("paste",this._handlePaste,this),this.editor.on("drop",this._handleDragDrop,this),this.editor.on("dragover",function(e){e.preventDefault()},this),this.editor.on("dragenter",function(e){e.preventDefault()},this)},_handleDragDrop:function(e){e._event&&e._event.dataTransfer&&this._handlePasteOrDropHelper(e,e._event.dataTransfer)},_handlePaste:function(e){return!e._event||!e._event.clipboardData||this._handlePasteOrDropHelper(e,e._event.clipboardData)},_handlePasteOrDropHelper:function(e,t){for(var i,n=t.items,a=!1,s=0;s<n.length;s++)"file"===(i=n[s]).kind&&this._isImage(i.type)&&(this._uploadImage(i.getAsFile()),a=!0);a&&e.preventDefault()},_isImage:function(e){return 0===e.indexOf("image/")},_uploadImage:function(e){var t,i,n,a,s,o,l,r=this,h=this.get("host"),g=m.Handlebars.compile(I);for(h.saveSelection(),require(["core_form/events"],function(e){e.notifyUploadStarted(r.editor.get("id"))}),s=(t=h.get("filepickeroptions").image).savepath===undefined?"/":t.savepath,i=new FormData,a=new XMLHttpRequest,o=Object.keys(t.repositories),i.append("repo_upload_file",e),i.append("itemid",t.itemid),l=0;l<o.length;l++)if("upload"===t.repositories[o[l]].type){i.append("repo_id",t.repositories[o[l]].id);break}i.append("env",t.env),i.append("sesskey",M.cfg.sesskey),i.append("client_id",t.client_id),i.append("savepath",s),i.append("ctx_id",t.context.id),e=(new Date).getTime(),n="moodleimage_"+Math.round(1e5*Math.random())+"-"+e,h.focus(),h.restoreSelection(),s=g({url:M.util.image_url("i/loading_small","moodle"),alt:M.util.get_string("uploading",d),id:n}),h.insertContentAtFocusPoint(s),r.markUpdated(),a.onreadystatechange=function(){var e,t,i=r.editor.one("#"+n);if(4===a.readyState){if(200===a.status){if(e=JSON.parse(a.responseText)){if(e.error)throw i&&i.remove(!0),require(["core_form/events"],function(e){e.notifyUploadCompleted(r.editor.get("id"))}),new M.core.ajaxException(e);(t=e).event&&"fileexists"===e.event&&(t=e.newfile),e=g({url:t.url,presentation:!0,classlist:c.RESPONSIVE}),t=m.Node.create(e),i?i.replace(t):r.editor.appendChild(t),r.markUpdated()}}else m.use("moodle-core-notification-alert",function(){require(["core_form/events"],function(e){e.notifyUploadCompleted(r.editor.get("id"))}),new M.core.alert({message:M.util.get_string("servererror","moodle")})}),i&&i.remove(!0);require(["core_form/events"],function(e){e.notifyUploadCompleted(r.editor.get("id"))})}},a.open("POST",M.cfg.wwwroot+"/repository/repository_ajax.php?action=upload",!0),a.send(i)},_handleClick:function(e){e=e.target,e=this.get("host").getSelectionFromNode(e);this.get("host").getSelection()!==e&&this.get("host").setSelection(e)},_displayDialogue:function(){var e;this._currentSelection=this.get("host").getSelection(),!1!==this._currentSelection&&(this._rawImageDimensions=null,(e=this.getDialogue({headerContent:M.util.get_string("imageproperties",d),width:"auto",focusAfterHide:!0,focusOnShowSelector:t.INPUTURL})).get("boundingBox").setStyle("maxWidth","90%"),e.set("bodyContent",this._getDialogueContent()).show())},_loadPreviewImage:function(e){var t=new Image,n=this;t.onerror=function(){n._form.one("."+c.IMAGEPREVIEW).setStyles({display:"none"}),n.getDialogue().centerDialogue()},t.onload=function(){var e,t,i;n._rawImageDimensions={width:this.width||a,height:this.height||o},""===(t=(e=n._form.one("."+c.INPUTWIDTH)).get("value"))&&(e.set("value",n._rawImageDimensions.width),t=""+n._rawImageDimensions.width),""===(i=(e=n._form.one("."+c.INPUTHEIGHT)).get("value"))&&(e.set("value",n._rawImageDimensions.height),i=""+n._rawImageDimensions.height),(e=n._form.one("."+c.IMAGEPREVIEW)).setAttribute("src",this.src),e.setStyles({display:"inline"}),e=n._form.one("."+c.INPUTCONSTRAIN),t.match(u)&&i.match(u)?e.set("checked",t===i):0===this.width||0===this.height?e.set("disabled","disabled"):(t=Math.round(1e3*parseInt(t,10)/this.width),i=Math.round(1e3*parseInt(i,10)/this.height),e.set("checked",t===i)),n._autoAdjustSize(n),n.getDialogue().centerDialogue()},t.src=e},_getDialogueContent:function(){var e=m.Handlebars.compile(
'<form class="atto_form"><div style="display:none" role="alert" class="alert alert-warning mb-1 {{CSS.IMAGEURLWARNING}}"><label for="{{elementid}}_{{CSS.INPUTURL}}">{{get_string "imageurlrequired" component}}</label></div>{{#if showFilepicker}}<div class="mb-1"><label for="{{elementid}}_{{CSS.INPUTURL}}">{{get_string "enterurl" component}}</label><div class="input-group input-append w-100"><input name="{{FORMNAMES.URL}}" class="form-control {{CSS.INPUTURL}}" type="url" id="{{elementid}}_{{CSS.INPUTURL}}" size="32"/><span class="input-group-append"><button class="btn btn-secondary {{CSS.IMAGEBROWSER}}" type="button">{{get_string "browserepositories" component}}</button></span></div></div>{{else}}<div class="mb-1"><label for="{{elementid}}_{{CSS.INPUTURL}}">{{get_string "enterurl" component}}</label><input name="{{FORMNAMES.URL}}" class="form-control fullwidth {{CSS.INPUTURL}}" type="url" id="{{elementid}}_{{CSS.INPUTURL}}" size="32"/></div>{{/if}}<div style="display:none" role="alert" class="alert alert-warning mb-1 {{CSS.IMAGEALTWARNING}}"><label for="{{elementid}}_{{CSS.INPUTALT}}">{{get_string "presentationoraltrequired" component}}</label></div><div class="mb-1"><label for="{{elementid}}_{{CSS.INPUTALT}}">{{get_string "enteralt" component}}</label><textarea class="form-control fullwidth {{CSS.INPUTALT}}" id="{{elementid}}_{{CSS.INPUTALT}}" name="{{FORMNAMES.ALT}}" maxlength="125"></textarea><div id="the-count" class="d-flex justify-content-end small"><span id="currentcount">0</span><span id="maximumcount"> / 125</span></div><div class="form-check"><input type="checkbox" class="form-check-input {{CSS.IMAGEPRESENTATION}}" id="{{elementid}}_{{CSS.IMAGEPRESENTATION}}"/><label class="form-check-label" for="{{elementid}}_{{CSS.IMAGEPRESENTATION}}">{{get_string "presentation" component}}</label></div></div><div class="mb-1"><label class="" for="{{elementid}}_{{CSS.INPUTSIZE}}">{{get_string "size" component}}</label><div id="{{elementid}}_{{CSS.INPUTSIZE}}" class="d-flex flex-wrap align-items-center {{CSS.INPUTSIZE}}"><label class="accesshide" for="{{elementid}}_{{CSS.INPUTWIDTH}}">{{get_string "width" component}}</label><input type="text" class="form-control w-auto mr-1 input-mini {{CSS.INPUTWIDTH}}" id="{{elementid}}_{{CSS.INPUTWIDTH}}" size="4"/> x<label class="accesshide" for="{{elementid}}_{{CSS.INPUTHEIGHT}}">{{get_string "height" component}}</label><input type="text" class="form-control w-auto ml-1 input-mini {{CSS.INPUTHEIGHT}}" id="{{elementid}}_{{CSS.INPUTHEIGHT}}" size="4"/><div class="form-check ml-2"><input type="checkbox" class="form-check-input {{CSS.INPUTCONSTRAIN}}" id="{{elementid}}_{{CSS.INPUTCONSTRAIN}}"/><label class="form-check-label" for="{{elementid}}_{{CSS.INPUTCONSTRAIN}}">{{get_string "constrain" component}}</label></div></div></div><div class="d-flex flex-wrap align-items-center mb-1"><label class="mb-0" for="{{elementid}}_{{CSS.INPUTALIGNMENT}}">{{get_string "alignment" component}}</label><select class="custom-select ml-2 {{CSS.INPUTALIGNMENT}}" id="{{elementid}}_{{CSS.INPUTALIGNMENT}}">{{#each alignments}}<option value="{{value}}">{{get_string str ../component}}</option>{{/each}}</select></div><input type="hidden" class="{{CSS.INPUTCUSTOMSTYLE}}"/><br/><div class="mdl-align"><div class="{{CSS.IMAGEPREVIEWBOX}}"><img class="{{CSS.IMAGEPREVIEW}}" alt="" style="display: none;"/></div><button class="btn btn-secondary {{CSS.INPUTSUBMIT}}" type="submit">{{get_string "saveimage" component}}</button></div></form>'),t=this.get("host").canShowFilepicker("image"),e=m.Node.create(e({elementid:this.get("host").get("elementid"),CSS:c,FORMNAMES:i,component:d,showFilepicker:t,alignments:s}));return this._form=e,this._applyImageProperties(this._form),this._form.one("."+c.INPUTURL).on("blur",this._urlChanged,this),this._form.one("."+c.INPUTURL).on("change",this._hasErrorUrlField,this),this._form.one("."+c.IMAGEPRESENTATION).on("change",this._hasErrorAltField,this),this._form.one("."+c.INPUTALT).on("blur",this._hasErrorAltField,this),this._form.one("."+c.INPUTWIDTH).on("blur",this._autoAdjustSize,this),this._form.one("."+c.INPUTHEIGHT).on("blur",this._autoAdjustSize,this,!0),this._form.one("."+c.INPUTCONSTRAIN).on("change",function(e){e.target.get("checked")&&this._autoAdjustSize(e)},this),this._form.one("."+c.INPUTSUBMIT).on("click",this._setImage,this),t&&this._form.one("."+c.IMAGEBROWSER).on("click",function(){this.get("host").showFilepicker("image",this._filepickerCallback,this)},this),this._form.one("."+c.INPUTALT).on("keyup",this._handleKeyup,this),e},_autoAdjustSize:function(g,e){var t,i,n,d,a,s,o,l,r,h;e=e||!1,t=this._form.one("."+c.INPUTWIDTH),i="width",n=this._form.one("."+c.INPUTHEIGHT),d="height",h=this._form.one("."+c.INPUTCONSTRAIN),a=t.get("value"),s=n.get("value"),o=this._form.one("."+c.IMAGEPREVIEW),this._rawImageDimensions&&(""===a&&(a=this._rawImageDimensions[i],t.set("value",a),a=t.get("value")),o.setStyles({width:null,height:null}),h.get("checked")?(e&&(h=t,t=n,n=h,h=i,i=d,d=h,h=a,a=s,s=h),a.match(u)?(s=a,l=parseInt(a,10),r=this._rawImageDimensions.width/100*l,o.setStyle("width",r),r=this._rawImageDimensions.height/100*l,o.setStyle("height",r)):(s=Math.round(a/this._rawImageDimensions[i]*this._rawImageDimensions[d]),e?o.setStyles({width:s,height:a}):o.setStyles({width:a,height:s})),n.set("value",s)):(a.match(u)?(l=parseInt(a,10),r=this._rawImageDimensions.width/100*l,o.setStyle("width",r+"px")):o.setStyle("width",a+"px"),s.match(u)?(l=parseInt(s,10),r=this._rawImageDimensions.height/100*l,o.setStyle("height",r+"px")):o.setStyle("height",s+"px")))},_filepickerCallback:function(e){""!==e.url&&(this._form.one("."+c.INPUTURL).set("value",e.url),this._form.one("."+c.INPUTWIDTH).set("value",""),this._form.one("."+c.INPUTHEIGHT).set("value",""),this._loadPreviewImage(e.url))},_applyImageProperties:function(t){var e=this._getSelectedImageProperties(),i=t.one("."+c.IMAGEPREVIEW);if(!1===e)return i.setStyle("display","none"),void s.some(function(e){return!!e.isDefault&&(
t.one("."+c.INPUTALIGNMENT).set("value",e.value),!0)},this);e.align&&t.one("."+c.INPUTALIGNMENT).set("value",e.align),e.customstyle&&t.one("."+c.INPUTCUSTOMSTYLE).set("value",e.customstyle),e.width&&t.one("."+c.INPUTWIDTH).set("value",e.width),e.height&&t.one("."+c.INPUTHEIGHT).set("value",e.height),e.alt&&t.one("."+c.INPUTALT).set("value",e.alt),e.src&&(t.one("."+c.INPUTURL).set("value",e.src),this._loadPreviewImage(e.src)),e.presentation&&t.one("."+c.IMAGEPRESENTATION).set("checked","checked"),this._autoAdjustSize()},_getSelectedImageProperties:function(){var e,t,i={src:null,alt:null,width:null,height:null,align:"",presentation:!1},n=this.get("host").getSelectedNodes();return(n=n&&n.filter("img"))&&n.size()?(n=this._removeLegacyAlignment(n.item(0)),t=(this._selectedImage=n).getAttribute("style"),i.customstyle=t,(t=n.getAttribute("width")).match(u)||(t=parseInt(t,10)),(e=n.getAttribute("height")).match(u)||(e=parseInt(e,10)),0!==t&&(i.width=t),0!==e&&(i.height=e),this._getAlignmentPropeties(n,i),i.src=n.getAttribute("src"),i.alt=n.getAttribute("alt")||"",i.presentation="presentation"===n.get("role"),i):(this._selectedImage=null,!1)},_getAlignmentPropeties:function(i,n){var a;!s.some(function(e){var t=this._getAlignmentClass(e.value);return i.hasClass(t)?(n.align=e.value,!0):(e.isDefault&&(a=e.value),!1)},this)&&a&&(n.align=a)},_urlChanged:function(){var e=this._form.one("."+c.INPUTURL);""!==e.get("value")&&this._loadPreviewImage(e.get("value"))},_setImage:function(e){var t=this._form,i=t.one("."+c.INPUTURL).get("value"),n=t.one("."+c.INPUTALT).get("value"),a=t.one("."+c.INPUTWIDTH).get("value"),s=t.one("."+c.INPUTHEIGHT).get("value"),o=this._getAlignmentClass(t.one("."+c.INPUTALIGNMENT).get("value")),l=t.one("."+c.IMAGEPRESENTATION).get("checked"),g=t.one("."+c.INPUTCONSTRAIN).get("checked"),d=t.one("."+c.INPUTCUSTOMSTYLE).get("value"),r=[],h=this.get("host");if(e.preventDefault(),!this._updateWarning()){if(h.focus(),""!==i){if(this._selectedImage?h.setSelection(h.getSelectionFromNode(this._selectedImage)):h.setSelection(this._currentSelection),g&&r.push(c.RESPONSIVE),r.push(o),!a.match(u)&&isNaN(parseInt(a,10)))return void t.one("."+c.INPUTWIDTH).focus();if(!s.match(u)&&isNaN(parseInt(s,10)))return void t.one("."+c.INPUTHEIGHT).focus();e=m.Handlebars.compile(I)({url:i,alt:n,width:a,height:s,presentation:l,customstyle:d,classlist:r.join(" ")}),this.get("host").insertContentAtFocusPoint(e),this.markUpdated()}this.getDialogue({focusAfterHide:null}).hide()}},_removeLegacyAlignment:function(i){return i.getStyle("margin")&&s.some(function(e){if(i.getStyle(e.name)!==e.value)return!1;var t=m.Node.create("<div>");return t.setStyle("margin",e.margin),i.getStyle("margin")===t.getStyle("margin")&&(i.addClass(this._getAlignmentClass(e.value)),i.setStyle(e.name,null),i.setStyle("margin",null),!0)},this),i},_getAlignmentClass:function(e){return c.ALIGNSETTINGS+"_"+e},_toggleVisibility:function(e,t){var i=this._form,e=i.all(e);e.setStyle("display",t?"block":"none")},_toggleAriaInvalid:function(e,t){var i=this._form;e.forEach(function(e){i.all(e).setAttribute("aria-invalid",t)})},_hasErrorUrlField:function(){var e=this._form,e=e.one("."+c.INPUTURL).get("value"),e=""===e;return this._toggleVisibility("."+c.IMAGEURLWARNING,e),this._toggleAriaInvalid(["."+c.INPUTURL],e),e},_hasErrorAltField:function(){var e=this._form,t=e.one("."+c.INPUTALT).get("value"),e=e.one("."+c.IMAGEPRESENTATION).get("checked"),t=""===t&&!e;return this._toggleVisibility("."+c.IMAGEALTWARNING,t),this._toggleAriaInvalid(["."+c.INPUTALT,"."+c.IMAGEPRESENTATION],t),t},_updateWarning:function(){var e=this._hasErrorUrlField(),t=this._hasErrorAltField(),e=e||t;return this.getDialogue().centerDialogue(),e},_handleKeyup:function(){var e=this._form,t=e.one("."+c.INPUTALT).get("value").length;e.one("#currentcount").setHTML(t)}})},"@VERSION@",{requires:["moodle-editor_atto-plugin"]});