CKEDITOR.plugins.add("ipsquote",{requires:"widget",icons:"ipsquote",hidpi:!0,allowedContent:"blockquote",init:function(f){f.widgets.add("ipsquote",{button:ips.getString("editorQuote"),template:ips.templates.render("core.editor.quote",{citation:ips.getString("editorQuote"),contents:""}),editables:{content:{selector:".ipsQuote_contents"}},upcast:function(b){if("blockquote"==b.name&&b.hasClass("ipsQuote")){b.attributes["data-gramm"]="false";var c={},a=0;for(a in b.attributes)"data-ipsquote-"==a.substr(0,
14)&&(c[a.substr(14)]=b.attributes[a]);b.children[0].hasClass("ipsQuote_citation")?(b.children[0].setHtml(ips.utils.getCitation(c)),_.isUndefined(b.children[1])||(b.children[1].attributes["data-gramm"]="false")):b.setHtml(ips.templates.render("core.editor.legacyQuoteUpcast",{citation:ips.utils.getCitation(c),contents:b.getHtml()}));return!0}},insert:function(){function b(){f.widgets.finalizeCreation(g)}var c=f.getSelectedHtml(!0),a=f.getSelection();a&&a.getSelectedElement();var a="function"==typeof this.defaults?
this.defaults():this.defaults,a=CKEDITOR.dom.element.createFromHtml(this.template.output(a)),d,e=f.widgets.wrapElement(a,this.name),g=new CKEDITOR.dom.documentFragment(e.getDocument());g.append(e);(d=f.widgets.initOn(a,this,!1))?(a=d.once("edit",function(a){if(a.data.dialog)d.once("dialog",function(a){a=a.data;var c,e;c=a.once("ok",b,null,null,20);e=a.once("cancel",function(a){a.data&&!1===a.data.hide||f.widgets.destroy(d,!0)});a.once("hide",function(){c.removeListener();e.removeListener()})});else b()},
null,null,999),d.edit(),a.removeListener(),c?("\x3cp\x3e"===c.substr(0,1)?d.editables.content.setHtml(c):d.editables.content.setHtml("\x3cp\x3e"+c+"\x3c/p\x3e"),c=f.createRange(),c.moveToElementEditablePosition(d.wrapper.getNext())):(c=f.createRange(),c.moveToElementEditablePosition(d.editables.content)),c.select()):b()}});f.on("key",function(b){if(13==b.data.keyCode)for(var c=f.getSelection().getRanges(),a=0;a<c.length;a++){b=c[a].getCommonAncestor();b=b instanceof CKEDITOR.dom.text?b.getParent():
b;var d=b.getHtml(),d=d.replace(/\<br\>/,""),d=d.replace(/\<span.*?\>\s*?\<\/span\>/g,""),d=d.trim();"span"!=b.getName()||d||(b=b.getParent());if("p"==b.getName()&&!d&&(d=b.getAscendant(function(a){return a instanceof CKEDITOR.dom.element&&"div"==a.getName()&&a.hasClass("ipsQuote_contents")},!0))){if(b.getNext()){c=CKEDITOR.dom.element.createFromHtml(ips.templates.render("core.editor.quote",{citation:d.getParent().findOne(".ipsQuote_citation").getText(),contents:""}));a=d.getParent();if(a.$.hasAttributes())for(var a=
a.$.attributes,e=a.length-1;0<=e;e--)a[e].specified&&a[e].name.startsWith("data-ipsquote")&&c.setAttribute(a[e].name,a[e].value);e=b;for(a=[];e=e.getNext();)a.push(e);for(var e=c.findOne(".ipsQuote_contents"),g=0;g<a.length;g++)a[g].move(e);a=CKEDITOR.dom.element.createFromHtml("\x3cp\x3e\x3cbr\x3e\x3c/p\x3e");a.insertAfter(d.getParent().getParent());c.insertAfter(a);f.widgets.initOn(c,"ipsquote");c=f.createRange();c.moveToPosition(a,CKEDITOR.POSITION_AFTER_START);f.getSelection().selectRanges([c]);
b.remove();f.focus()}else b.remove(),f.focus(),c=f.createRange(),b=d.getParent().getParent(),d=b.getNext(),d||(d=new CKEDITOR.dom.element("p",f.document),d.insertAfter(b)),c.moveToPosition(d,CKEDITOR.POSITION_AFTER_START),f.getSelection().selectRanges([c]);return!1}}},this,0);f.addCommand("ipsQuoteBreakout",{exec:function(b,c){var a=b.getSelection(),d=a.getSelectedElement();if(!d)for(var e=a.getRanges(),a=0;a<e.length;a++)d=e[a].getCommonAncestor();d=d.getAscendant(function(a){return a instanceof
CKEDITOR.dom.element&&a.hasClass("cke_widget_wrapper")},!0);e=d.find(".ipsQuote_contents \x3e *");for(a=0;a<e.count();a++)b.insertElement(e.getItem(a).clone(!0));d.remove()}});f.addCommand("ipsQuoteRemove",{exec:function(b){var c=b.getSelection(),a=c.createBookmarks2(!0),d=c.getSelectedElement();if(!d)for(var c=c.getRanges(),e=0;e<c.length;e++)d=c[e].getCommonAncestor();d.getAscendant(function(a){return a instanceof CKEDITOR.dom.element&&a.hasClass("cke_widget_wrapper")},!0).remove();b.getSelection().selectBookmarks(a)}});
f.contextMenu&&(f.addMenuGroup("ipsQuote"),f.addMenuItem("ipsQuoteBreakout",{label:ips.getString("editorQuoteBreakout"),icon:null,command:"ipsQuoteBreakout",group:"ipsQuote"}),f.addMenuItem("ipsQuoteRemove",{label:ips.getString("editorQuoteRemove"),icon:null,command:"ipsQuoteRemove",group:"ipsQuote"}),f.contextMenu.addListener(function(b,c){if(b.getAscendant(function(a){return a instanceof CKEDITOR.dom.element&&a.hasClass("ipsQuote")},!0)||b.find(".ipsQuote").count())return{ipsQuoteBreakout:CKEDITOR.TRISTATE_OFF,
ipsQuoteRemove:CKEDITOR.TRISTATE_OFF}}))}});