MDL-67675 atto_h5p: No need to manually clear the H5P placeholder
[moodle.git] / lib / editor / atto / plugins / h5p / yui / build / moodle-atto_h5p-button / moodle-atto_h5p-button-min.js
CommitLineData
2c4be2eb
SR
1YUI.add("moodle-atto_h5p-button",function(e,t){var n={CONTENTWARNING:"att_h5p_contentwarning",H5PBROWSER:"openh5pbrowser",INPUTALT:"atto_h5p_altentry",INPUTH5PFILE:"atto_h5p_file",INPUTH5PURL:"atto_h5p_url",INPUTSUBMIT:"atto_h5p_urlentrysubmit",OPTION_DOWNLOAD_BUTTON:"atto_h5p_option_download_button",OPTION_COPYRIGHT_BUTTON:"atto_h5p_option_copyright_button",OPTION_EMBED_BUTTON:"atto_h5p_option_embed_button",URLWARNING:"atto_h5p_warning"},r={CONTENTWARNING:"."+n.CONTENTWARNING,H5PBROWSER:"."+n.H5PBROWSER,INPUTH5PFILE:"."+n.INPUTH5PFILE,INPUTH5PURL:"."+n.INPUTH5PURL,INPUTSUBMIT:"."+n.INPUTSUBMIT,OPTION_DOWNLOAD_BUTTON:"."+n.OPTION_DOWNLOAD_BUTTON,OPTION_COPYRIGHT_BUTTON:"."+n.OPTION_COPYRIGHT_BUTTON,OPTION_EMBED_BUTTON:"."+n.OPTION_EMBED_BUTTON,URLWARNING:"."+n.URLWARNING},i="atto_h5p",s='<form class="atto_form mform" id="{{elementid}}_atto_h5p_form"><div style="display:none" role="alert" class="alert alert-warning mb-1 {{CSS.CONTENTWARNING}}">{{get_string "noh5pcontent" component}}</div>{{#if canUploadAndEmbed}}<div class="mt-2 attoh5pinstructions">{{{get_string "instructions" component}}}</div><div class="my-2"><strong>{{get_string "either" component}}</strong></div>{{/if}}{{#if canEmbed}}<div class="mb-4"><label for="{{elementid}}_{{CSS.INPUTH5PURL}}">{{get_string "enterurl" component}}</label><div style="display:none" role="alert" class="alert alert-warning mb-1 {{CSS.URLWARNING}}">{{get_string "invalidh5purl" component}}</div><textarea rows="3" data-region="h5purl" class="form-control {{CSS.INPUTH5PURL}}" type="url" id="{{elementid}}_{{CSS.INPUTH5PURL}}" />{{embedURL}}</textarea></div>{{/if}}{{#if canUploadAndEmbed}}<div class="my-2"><strong>{{get_string "or" component}}</strong></div>{{/if}}{{#if canUpload}}<div class="mb-4"><label for="{{elementid}}_{{CSS.H5PBROWSER}}">{{get_string "h5pfile" component}}</label><div class="input-group input-append w-100"><input class="form-control {{CSS.INPUTH5PFILE}}" type="url" value="{{fileURL}}" id="{{elementid}}_{{CSS.INPUTH5PFILE}}" size="32"/><span class="input-group-append"><button class="btn btn-secondary {{CSS.H5PBROWSER}}" type="button">{{get_string "browserepositories" component}}</button></span></div><fieldset class="collapsible {{#if collapseOptions}}collapsed{{/if}}" id="{{elementid}}_h5poptions"><legend class="ftoggler">{{get_string "h5poptions" component}}</legend><div class="fcontainer"><div class="form-check"><input type="checkbox" {{optionDownloadButton}} class="form-check-input {{CSS.OPTION_DOWNLOAD_BUTTON}}"id="{{elementid}}_h5p-option-allow-download"/><label class="form-check-label" for="{{elementid}}_h5p-option-allow-download">{{get_string "downloadbutton" component}}</label></div><div class="form-check"><input type="checkbox" {{optionEmbedButton}} class="form-check-input {{CSS.OPTION_EMBED_BUTTON}}" id="{{elementid}}_h5p-option-embed-button"/><label class="form-check-label" for="{{elementid}}_h5p-option-embed-button">{{get_string "embedbutton" component}}</label></div><div class="form-check mb-2"><input type="checkbox" {{optionCopyrightButton}} class="form-check-input {{CSS.OPTION_COPYRIGHT_BUTTON}}" id="{{elementid}}_h5p-option-copyright-button"/><label class="form-check-label" for="{{elementid}}_h5p-option-copyright-button">{{get_string "copyrightbutton" component}}</label></div></div></fieldset></div>{{/if}}<div class="text-center"><button class="btn btn-secondary {{CSS.INPUTSUBMIT}}" type="submit">{{get_string "pluginname" component}}</button></div></form>',o='{{#if addParagraphs}}<p><br></p>{{/if}}<div class="h5p-placeholder" contenteditable="false">{{{url}}}</div>{{#if addParagraphs}}<p><br></p>{{/if}}';e.namespace("M.atto_h5p").Button=e.Base.create("button",e.M.editor_atto.EditorPlugin,[],{_currentSelection:null,_form:null,_H5PDiv:null,_allowedmethods:"none",initializer:function(){this._allowedmethods=this.get("allowedmethods");if(this._allowedmethods==="none")return;this.addButton({icon:"icon",iconComponent:"atto_h5p",callback:this._displayDialogue,tags:".h5p-placeholder",tagMatchRequiresAll:!1}),this.editor.all(".h5p-placeholder").setAttribute("contenteditable","false"),this.editor.delegate("dblclick",this._handleDblClick,".h5p-placeholder",this),this.editor.delegate("click",this._handleClick,".h5p-placeholder",this)},_handleDblClick:function(){this._displayDialogue()},_handleClick:function(e){var t=this.get("host").getSelectionFromNode(e.target);this.get("host").getSelection()!==t&&this.get("host").setSelection(t)},_displayDialogue:function(){this._currentSelection=this.get("host").getSelection();if(this._currentSelection===!1)return;this._getH5PDiv();var e=this.getDialogue({headerContent:M.util.get_string("pluginname",i),width:"auto",focusAfterHide:!0});e.set("bodyContent",this._getDialogueContent()).show(),M.form.shortforms({formid:this.get("host").get("elementid")+"_atto_h5p_form"})},_getH5PDiv:function(){var e=this.get("host").getSelectedNodes(),t=null;e.each(function(e){e.hasClass("h5p-placeholder")&&(t=e)}),this._H5PDiv=t},_getPermissions:function(){var e={canUpload:!1,canUploadAndEbmed:!1,canEmbed:!1};this.get("host").canShowFilepicker("h5p")&&(this._allowedmethods==="both"?(e.canUploadAndEmbed=!0,e.canUpload=!0):this._allowedmethods==="upload"&&(e.canUpload=!0));if(this._allowedmethods==="both"||this._allowedmethods==="embed")e.canEmbed=!0;return e},_getDialogueContent:function(){var t=this._getPermissions(),r,o,u,a,f,l=!0;if(this._H5PDiv){var c=this._H5PDiv.get("innerHTML"),h=M.cfg.wwwroot+"/draftfile.php";if(h==c.substring(0,h.length)){r=c.split("?")[0];var p=c.split("?")[1];p&&(p.match(/export=1/)&&(u="checked",l=!1),p.match(/embed=1/)&&(a="checked",l=!1),p.match(/copyright=1/)&&(f="checked",l=!1))}else o=c}var d=e.Handlebars.compile(s),v=e.Node.create(d({elementid:this.get("host").get("elementid"),CSS:n,component:i,canUpload:t.canUpload,canEmbed:t.canEmbed,fileURL:r,embedURL:o,canUploadAndEmbed:t.canUploadAndEmbed,collapseOptions:l,optionDownloadButton:u,optionEmbedButton:a,optionCopyrightButton:f}));return this
2._form=v,this._setEventListeners(),v},_filepickerCallback:function(e){if(e.url!==""){var t=this._form.one(r.INPUTH5PFILE);t.set("value",e.url),this._form.one(r.INPUTH5PURL).set("value",""),this._removeWarnings()}},_setEventListeners:function(){var e=this._form,t=this._getPermissions();e.one(r.INPUTSUBMIT).on("click",this._setH5P,this),t.canUpload&&e.one(r.H5PBROWSER).on("click",function(){this.get("host").showFilepicker("h5p",this._filepickerCallback,this)},this),t.canUploadAndEmbed&&(e.one(r.INPUTH5PFILE).on("change",function(){e.one(r.INPUTH5PURL).set("value",""),this._removeWarnings()},this),e.one(r.INPUTH5PURL).on("change",function(){e.one(r.INPUTH5PFILE).set("value",""),this._removeWarnings()},this))},_removeWarnings:function(){var e=this._form;e.one(r.URLWARNING).setStyle("display","none"),e.one(r.CONTENTWARNING).setStyle("display","none")},_setH5P:function(t){var n=this._form,i=n.one(r.INPUTH5PURL).get("value"),s,u=this.get("host"),a,f=this._getPermissions();f.canEmbed&&(i=n.one(r.INPUTH5PURL).get("value")),f.canUpload&&(a=n.one(r.INPUTH5PFILE).get("value")),t.preventDefault();if(this._updateWarning())return;u.focus();var l=!0;this._H5PDiv&&(this._H5PDiv.remove(),l=!1);if(i!==""){u.setSelection(this._currentSelection);if(this._validEmbed(i)){var c=e.Handlebars.compile(o),h=/<iframe.*?src="(.*?)".*<\/iframe>/,p=i.match(h)[1];p.startsWith(M.cfg.wwwroot+"/h5p/embed.php")&&(p=decodeURIComponent(p.split("url=")[1])),s=c({url:p})}else{var d=e.Handlebars.compile(o);s=d({url:i})}this.get("host").insertContentAtFocusPoint(s),this.markUpdated()}else if(a!==""){u.setSelection(this._currentSelection);var v={};n.one(r.OPTION_DOWNLOAD_BUTTON).get("checked")&&(v["export"]="1"),n.one(r.OPTION_EMBED_BUTTON).get("checked")&&(v.embed="1"),n.one(r.OPTION_COPYRIGHT_BUTTON).get("checked")&&(v.copyright="1");var m="";for(var g in v)m===""&&a.indexOf("?")===-1?m+="?":m+="&amp;",m+=g+"="+v[g];var y=e.Handlebars.compile(o);s=y({url:a+m,addParagraphs:l}),this.get("host").insertContentAtFocusPoint(s),this.markUpdated()}this.getDialogue({focusAfterHide:null}).hide()},_validEmbed:function(e){var t=new RegExp("^(<iframe).*(<\\/iframe>)");return!!t.test(e)},_validURL:function(e){var t=new RegExp("^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*");return!!t.test(e)},_updateWarning:function(){var e=this._form,t=!0,n,i,s=this._getPermissions();if(s.canEmbed){n=e.one(r.INPUTH5PURL).get("value");if(n!=="")return this._validURL(n)||this._validEmbed(n)?(e.one(r.URLWARNING).setStyle("display","none"),t=!1):(e.one(r.URLWARNING).setStyle("display","block"),t=!0),t}return s.canUpload&&(i=e.one(r.INPUTH5PFILE).get("value"),i!==""?(e.one(r.CONTENTWARNING).setStyle("display","none"),t=!1):(e.one(r.CONTENTWARNING).setStyle("display","block"),t=!0)),t}},{ATTRS:{allowedmethods:{value:null}}})},"@VERSION@",{requires:["moodle-editor_atto-plugin"]});