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