weekly release 4.0dev
[moodle.git] / lib / editor / atto / plugins / equation / yui / build / moodle-atto_equation-button / moodle-atto_equation-button-min.js
CommitLineData
541e90c3
MC
1YUI.add("moodle-atto_equation-button",function(r,t){var a="atto_equation",l={EQUATION_TEXT:"atto_equation_equation",EQUATION_PREVIEW:"atto_equation_preview",SUBMIT:"atto_equation_submit",LIBRARY:"atto_equation_library",LIBRARY_GROUPS:"atto_equation_groups",LIBRARY_GROUP_PREFIX:"atto_equation_group"},u={LIBRARY:"."+l.LIBRARY,LIBRARY_GROUP:"."+l.LIBRARY_GROUPS+" > div > div",EQUATION_TEXT:"."+l.EQUATION_TEXT,EQUATION_PREVIEW:"."+l.EQUATION_PREVIEW,SUBMIT:"."+l.SUBMIT,LIBRARY_BUTTON:"."+l.LIBRARY+" button"},c={START:"\\(",END:"\\)"},i='<form class="atto_form">{{{library}}}<label for="{{elementid}}_{{CSS.EQUATION_TEXT}}">{{{get_string "editequation" component texdocsurl}}}</label><textarea class="fullwidth {{CSS.EQUATION_TEXT}}" id="{{elementid}}_{{CSS.EQUATION_TEXT}}" rows="8"></textarea><br/><label for="{{elementid}}_{{CSS.EQUATION_PREVIEW}}">{{get_string "preview" component}}</label><div describedby="{{elementid}}_cursorinfo" class="well well-small p-1 fullwidth {{CSS.EQUATION_PREVIEW}}" id="{{elementid}}_{{CSS.EQUATION_PREVIEW}}"></div><div id="{{elementid}}_cursorinfo">{{get_string "cursorinfo" component}}</div><div class="mdl-align"><br/><button class="btn btn-secondary {{CSS.SUBMIT}}">{{get_string "saveequation" component}}</button></div></form>',h='<div class="{{CSS.LIBRARY}}"><ul class="root nav nav-tabs mb-1" role="tablist">{{#each library}}<li class="nav-item"><a class="nav-link" href="#{{../elementid}}_{{../CSS.LIBRARY_GROUP_PREFIX}}_{{@key}}" data-target="#{{../elementidescaped}}_{{../CSS.LIBRARY_GROUP_PREFIX}}_{{@key}}" role="tab" data-toggle="tab">{{get_string groupname ../component}}</a></li>{{/each}}</ul><div class="tab-content mb-1 {{CSS.LIBRARY_GROUPS}}">{{#each library}}<div data-medium-type="{{CSS.LINK}}" class="tab-pane" id="{{../elementid}}_{{../CSS.LIBRARY_GROUP_PREFIX}}_{{@key}}"><div role="toolbar">{{#split "\n" elements}}<button class="btn btn-secondary" tabindex="-1" data-tex="{{this}}"aria-label="{{this}}" title="{{this}}">{{../../DELIMITERS.START}}{{this}}{{../../DELIMITERS.END}}</button>{{/split}}</div></div>{{/each}}</div></div>';r.namespace("M.atto_equation").Button=r.Base.create("button",r.M.editor_atto.EditorPlugin,[],{_currentSelection:null,_lastCursorPos:0,_content:null,_sourceEquation:null,_groupFocus:null,_equationPatterns:[/\$\$([\S\s]+?)\$\$/,/\\\(([\S\s]+?)\\\)/,/\\\[([\S\s]+?)\\\]/,/\[tex\]([\S\s]+?)\[\/tex\]/],initializer:function(){this._groupFocus={},this.get("texfilteractive")&&(this.addButton({icon:"e/math",callback:this._displayDialogue}),this.get("host").on("atto:selectionchanged",function(){this._resolveEquation()?this.highlightButtons():this.unHighlightButtons()},this),this.editor.all("tex").each(function(t){var e=r.Node.create("<span>"+c.START+" "+t.get("text")+" "+c.END+"</span>");t.replace(e)}))},_displayDialogue:function(){var t,e,n;this._currentSelection=this.get("host").getSelection(),!1!==this._currentSelection&&(t=this._resolveEquation(),e=this.getDialogue({headerContent:M.util.get_string("pluginname",a),focusAfterHide:!0,width:600,focusOnShowSelector:u.EQUATION_TEXT}),n=this._getDialogueContent(),e.set("bodyContent",n),n.one(".nav-item:first-child .nav-link").getDOMNode().click(),e.show(),require(["core/event"],function(t){t.notifyFilterContentUpdated(e.get("boundingBox").getDOMNode())}),t&&n.one(u.EQUATION_TEXT).set("text",t),this._updatePreview(!1))},_resolveEquation:function(){var c,t=this.get("host").getSelectionParentNode(),h=this.get("host").getSelection(),d=!1;return!!this.get("host").isActive()&&(!!t&&(!(!h||0===h.length)&&(this.sourceEquation=null,h=h[0],c=r.one(t).get("text"),r.Array.find(this._equationPatterns,function(u){var t=c.match(new RegExp(u.source,"g"));if(t&&t.length)return r.Array.find(t,function(t){for(var e,n,i,o,s,a,r,l=0;-1!==c.indexOf(t,l);){if(n=(e=c.indexOf(t,l))+t.length,i=h.startOffset>=e&&h.startOffset<n,o=h.endOffset<=n&&h.endOffset>e,i&&o&&(s=t.match(u))&&s.length)return r=(a=c.indexOf(s[1],e))+s[1].length,d=s[1],this.sourceEquation={startOuterPosition:e,endOuterPosition:n,outerMatch:t,startInnerPosition:a,endInnerPosition:r,innerMatch:s},!0;l=n}},this)},this),!1!==d&&(d=d.trim()),d)))},_setEquation:function(t){var e,n,i,o,s;o=this.get("host"),t.preventDefault(),this.getDialogue({focusAfterHide:null}).hide(),""!==(i=t.currentTarget.ancestor(".atto_form").one("textarea").get("value"))&&(o.setSelection(this._currentSelection),this.sourceEquation?(i=" "+i+" ",s=(n=(e=r.one(o.getSelectionParentNode())).get("text")).slice(0,this.sourceEquation.startInnerPosition)+i+n.slice(this.sourceEquation.endInnerPosition),e.set("text",s)):(i=c.START+" "+i+" "+c.END,o.insertContentAtFocusPoint(i)),this.markUpdated())},_throttle:function(n,i){var o=null;return function(){var t=this,e=arguments;clearTimeout(o),o=setTimeout(function(){n.apply(t,e)},i)}},_updatePreview:function(t){var e,n,i,o=this._content.one(u.EQUATION_TEXT),s=o.get("value"),a=o.get("selectionStart");for(t&&t.preventDefault(),a=a||0;"\\"===s.charAt(a)&&0<=a;)--a;if(n=/[a-zA-Z\{]/,0!==a&&"{"!=s.charAt(a-1))for(;n.test(s.charAt(a))&&a<s.length&&n.test(s.charAt(a-1));)a+=1;this._lastCursorPos=a,s=s.substring(0,a)+"\\Downarrow "+s.substring(a),s=c.START+" "+s+" "+c.END,e=M.cfg.wwwroot+"/lib/editor/atto/plugins/equation/ajax.php",i={sesskey:M.cfg.sesskey,contextid:this.get("contextid"),action:"filtertext",text:s},r.io(e,{context:this,data:i,timeout:500,on:{complete:this._loadPreview}})},_loadPreview:function(t,e){var n=this._content.one(u.EQUATION_PREVIEW);200===e.status&&(n.setHTML(e.responseText),require(["core/event"],function(t){t.notifyFilterContentUpdated(n.getDOMNode())}))},_getDialogueContent:function(){var t=this._getLibraryContent(),e=this._throttle(this._updatePreview,500),n=r.Handlebars.compile(i);return this._content=r.Node.create(n({elementid:this.get("host").get("elementid"),component:a,library:t,texdocsurl:this.get("texdocsurl"),CSS:l})),this._content.all(u.LIBRARY_GROUP).each(function(t){this._setGroupTabFocus(t,t.one("button")),t.all(
2"button a").setAttribute("tabindex","-1")},this),this._content.delegate("key",this._groupNavigation,"down:37,39",u.LIBRARY_BUTTON,this),this._content.one(u.SUBMIT).on("click",this._setEquation,this),this._content.one(u.EQUATION_TEXT).on("valuechange",e,this),this._content.one(u.EQUATION_TEXT).on("mouseup",e,this),this._content.one(u.EQUATION_TEXT).on("keyup",e,this),this._content.delegate("click",this._selectLibraryItem,u.LIBRARY_BUTTON,this),this._content},_groupNavigation:function(t){t.preventDefault();var e,n=t.currentTarget,i=n.get("parentNode"),o=i.all("button"),s=37!==t.keyCode?1:-1,a=o.indexOf(n);a<0&&(a=0),(a+=s)<0?a=o.size()-1:a>=o.size()&&(a=0),e=o.item(a),this._setGroupTabFocus(i,e),e.focus()},_setGroupTabFocus:function(t,e){var n=t.generateID();"undefined"!=typeof this._groupFocus[n]&&this._groupFocus[n].setAttribute("tabindex","-1"),(this._groupFocus[n]=e).setAttribute("tabindex",0),t.setAttribute("aria-activedescendant",e.generateID())},_selectLibraryItem:function(t){var e,n,i,o,s,a=t.currentTarget.getAttribute("data-tex"),r=0;t.preventDefault(),this._setGroupTabFocus(t.currentTarget.get("parentNode"),t.currentTarget)," "!==(n=(e=(i=t.currentTarget.ancestor(".atto_form").one("textarea")).get("value")).substring(0,this._lastCursorPos)).charAt(n.length-1)&&(n+=" "),r=(n+=a).length," "!==e.charAt(this._lastCursorPos)&&(n+=" "),n+=e.substring(this._lastCursorPos,e.length),i.set("value",n),i.focus(),"number"==typeof(o=i.getDOMNode()).selectionStart?o.selectionStart=o.selectionEnd=r:"undefined"!=typeof o.createTextRange&&((s=o.createTextRange()).moveToPoint(r),s.select()),this._updatePreview(!1)},_getLibraryContent:function(){var t,e,n,i=r.Handlebars.compile(h),o=this.get("library"),s="";return r.Handlebars.registerHelper("split",function(t,e,n){var i,o,s;if(void 0===t||void 0===e)return"";for(s="",i=e.trim().split(t);0<i.length;)o=i.shift().trim(),s+=n.fn(o);return s}),s=i({elementid:this.get("host").get("elementid"),elementidescaped:this._escapeQuerySelector(this.get("host").get("elementid")),component:a,library:o,CSS:l,DELIMITERS:c}),t=M.cfg.wwwroot+"/lib/editor/atto/plugins/equation/ajax.php",e={sesskey:M.cfg.sesskey,contextid:this.get("contextid"),action:"filtertext",text:s},200===(n=r.io(t,{sync:!0,data:e,method:"POST"})).status&&(s=n.responseText),s},_escapeQuerySelector:function(t){return t.replace(/(:|\.|\[|\]|,|=|@)/g,"\\$1")}},{ATTRS:{texfilteractive:{value:!1},contextid:{value:null},library:{value:{}},texdocsurl:{value:null}}})},"@VERSION@",{requires:["moodle-editor_atto-plugin","moodle-core-event","io","event-valuechange","tabview","array-extras"]});