(function(){'use strict';tinymce.PluginManager.requireLangPack('foswikibuttons');tinymce.create('tinymce.plugins.FoswikiButtons',{formats_listbox:null,_lastButtonUpdateNode:null,_nodeChangeEventScheduled:null,_deferNodeChangeEvent:null,nodeChangeEventFrequency:null,init:function(ed,url){this.formats=ed.getParam('foswikibuttons_formats');this.nodeChangeEventFrequency=ed.getParam('foswikibuttons_cursoridletime');this.format_names=[];this.recipe_names=[];jQuery.each(this.formats,function(key,value){ed.plugins.foswikibuttons.format_names.push(key);});ed.onInit.add(function(editor){this.plugins.foswikibuttons._registerFormats(editor,this.plugins.foswikibuttons.formats);this.plugins.foswikibuttons._contextMenuVerbatimClasses(editor);});this._setupTTButton(ed,url);this._setupColourButton(ed,url);this._setupAttachButton(ed,url);this._setupIndentButton(ed,url);this._setupExdentButton(ed,url);this._setupHideButton(ed,url);this._setupFormatCommand(ed,this.formats);return;},getInfo:function(){return{longname:'Foswiki Buttons Plugin',author:'Crawford Currie',authorurl:'http://c-dot.co.uk',infourl:'http://c-dot.co.uk',version:3};},createControl:function(name,controlManager){if(name==='foswikiformat'){return this._createFoswikiFormatControl(name,controlManager,this);}
return null;},_setupTTButton:function(ed,url){ed.addCommand('foswikibuttonsTT',function(){ed.formatter.toggle('WYSIWYG_TT');});ed.addButton('tt',{title:'foswikibuttons.tt_desc',cmd:'foswikibuttonsTT',image:url+'/img/tt.gif'});return;},_registerFormats:function(ed,formats){ed.formatter.register('WYSIWYG_TT',{inline:'span',classes:'WYSIWYG_TT'});ed.formatter.register('WYSIWYG_COLOR',[{inline:'span',classes:'WYSIWYG_COLOR',styles:{color:'%value'}},{inline:'span',classes:'WYSIWYG_COLOR'}]);ed.formatter.register('IS_WYSIWYG_COLOR',{inline:'span',classes:'WYSIWYG_COLOR'});ed.formatter.register(formats);return;},_createFoswikiFormatControl:function(name,controlManager,plugin){var ed=controlManager.editor;plugin.formats_listbox=controlManager.createListBox(name,{title:'Format',onselect:function(format){ed.execCommand('foswikibuttonsFormat',false,format);}});jQuery.each(plugin.formats,function(formatname,format){plugin.formats_listbox.add(formatname,formatname);return;});plugin.formats_listbox.selectByIndex(0);return plugin.formats_listbox;},_setupFormatCommand:function(ed,formats){ed.addCommand('foswikibuttonsFormat',function(ui,formatname){jQuery.each(formats,function(name,format){ed.formatter.remove(name);});ed.formatter.apply(formatname);});ed.onNodeChange.add(this._nodeChange,this);return;},_setupColourButton:function(ed,url){ed.addCommand('foswikibuttonsColour',function(){if(ed.selection.isCollapsed()){return;}
ed.windowManager.open({location:false,menubar:false,toolbar:false,status:false,url:url+'/colours.htm',width:220,height:280,movable:true,popup_css:false,inline:true},{plugin_url:url});});ed.addButton('colour',{title:'foswikibuttons.colour_desc',cmd:'foswikibuttonsColour',image:url+'/img/colour.gif'});return;},_setupAttachButton:function(ed,url){ed.addCommand('foswikibuttonsAttach',function(){var htmpath='/attach.htm',htmheight=300;if(null!==FoswikiTiny.foswikiVars.TOPIC.match(/(X{10}|AUTOINC[0-9]+)/)){htmpath='/attach_error_autoinc.htm';htmheight=125;}
ed.windowManager.open({location:false,menubar:false,toolbar:false,status:false,url:url+htmpath,width:450,height:htmheight,movable:true,inline:true},{plugin_url:url});});ed.addButton('attach',{title:'foswikibuttons.attach_desc',cmd:'foswikibuttonsAttach',image:url+'/img/attach.gif'});return;},_setupIndentButton:function(ed,url){ed.addCommand('fwindent',function(){if(this.queryCommandState('InsertUnorderedList')||this.queryCommandState('InsertOrderedList')){this.execCommand("Indent");}
else{var dom=ed.dom,selection=ed.selection,node=dom.getParent(selection.getStart(),dom.isBlock)||dom.getParent(selection.getEnd(),dom.isBlock),div;if(node){div=dom.create('div',{'class':'foswikiIndent'});while(node.firstChild){dom.add(div,dom.remove(node.firstChild));}
div=dom.add(node,div);ed.selection.select(div);ed.selection.collapse();ed.selection.setCursorLocation(div,0);}}});ed.addButton('fwindent',{title:'foswikibuttons.indent_desc',cmd:'fwindent',image:url+'/img/indent.gif'});return;},_setupExdentButton:function(ed,url){ed.addCommand('fwexdent',function(){var dom=ed.dom,selection=ed.selection,node=dom.getParent(selection.getStart(),dom.isBlock),p;if(node&&dom.hasClass(node,'foswikiIndent')){p=node.parentNode;while(node.firstChild){p.insertBefore(dom.remove(node.firstChild),node);}
dom.remove(node);ed.selection.select(p.firstChild);ed.selection.collapse();}else{this.execCommand("Outdent");}});ed.onNodeChange.add(function(ed,cm,n,co,ob){var dom=ed.dom,selection=ed.selection,node=dom.getParent(selection.getStart(),dom.isBlock),state=(node&&dom.hasClass(node,'foswikiIndent'))||ed.queryCommandState('Outdent');cm.setDisabled('fwexdent',!state);});ed.addButton('fwexdent',{title:'foswikibuttons.exdent_desc',cmd:'fwexdent',image:url+'/img/exdent.gif'});return;},_setupHideButton:function(ed,url){ed.addCommand('foswikibuttonsHide',function(){if(FoswikiTiny.saveEnabled){if(ed.getParam('fullscreen_is_enabled')){ed.onGetContent.add(function(){tinymce.DOM.win.setTimeout(function(){var e=tinyMCE.activeEditor;tinyMCE.execCommand('mceToggleEditor',true,e.id);FoswikiTiny.switchToRaw(e);},10);});ed.execCommand('mceFullScreen');}
else{tinyMCE.execCommand("mceToggleEditor",true,ed.id);FoswikiTiny.switchToRaw(ed);}}});ed.addButton('hide',{title:'foswikibuttons.hide_desc',cmd:'foswikibuttonsHide',image:url+'/img/hide.gif'});return;},_nodeChange:function(ed,cm,node,collapsed){var selectedFormats,listbox;if(typeof(node)!=='object'){return;}
if(this.nodeChangeEventFrequency){this._scheduleNodeChangeEvent(ed,cm,node,collapsed);}else{this._doUpdateButtonState(ed,cm,node,collapsed);}
return true;},_scheduleNodeChangeEvent:function(ed,cm,node,collapsed){var that=this;if(this._nodeChangeEventScheduled){this._deferNodeChangeEvent=true;}else{this._deferNodeChangeEvent=false;this._nodeChangeEventScheduled=true;setTimeout(function(){that._tryNodeChangeEvent(that,ed,cm,node,collapsed);return;},this.nodeChangeEventFrequency);}
return;},_tryNodeChangeEvent:function(that,ed,cm,node,collapsed){if(that._deferNodeChangeEvent){that._deferNodeChangeEvent=false;that._nodeChangeEventScheduled=false;that._scheduleNodeChangeEvent(ed,cm,node,collapsed);}else{that._doUpdateButtonState(ed,cm);that._nodeChangeEventScheduled=false;}
return;},_doUpdateButtonState:function(ed,cm){var selectedFormats,listbox,node,collapsed;if(ed.selection){node=ed.selection.getNode();collapsed=ed.selection.isCollapsed();if(!collapsed){this._updateButtonState(ed,cm);}else if(node!==this._lastButtonUpdateNode){this._updateButtonState(ed,cm,node,collapsed);this._lastButtonUpdateNode=node;}}},_updateButtonState:function(ed,cm,node,collapsed){var selectedFormats=ed.formatter.matchAll(ed.plugins.foswikibuttons.format_names),listbox=cm.get(ed.id+'_foswikiformat');if(collapsed){cm.setDisabled('colour',true);cm.setDisabled('tt',true);}else{cm.setDisabled('colour',false);cm.setDisabled('tt',false);}
if(ed.formatter.match('WYSIWYG_TT')){cm.setActive('tt',true);}else{cm.setActive('tt',false);}
if(ed.formatter.match('WYSIWYG_COLOR')){cm.setActive('colour',true);}else{cm.setActive('colour',false);}
if(selectedFormats.length>0){listbox.select(selectedFormats[0]);}else{listbox.select('Normal');}
return true;},_contextMenuVerbatimClasses:function(ed){var recipes={},i,key,sm,se,el,selectedFormats,current='';ed.plugins.foswikibuttons.recipe_names=['tml','html','js'];for(i=0;i<ed.plugins.foswikibuttons.recipe_names.length;i+=1){key=ed.plugins.foswikibuttons.recipe_names[i];recipes[key]={"block":"pre","remove":"all",classes:'TMLverbatim '+key};}
ed.formatter.register(recipes);jQuery.each(recipes,function(key,value){ed.plugins.foswikibuttons.recipe_names.push(key);});if(ed&&ed.plugins.contextmenu){ed.plugins.contextmenu.onContextMenu.add(function(th,m,e){se=ed.selection;el=se.getNode()||ed.getBody();if(el.nodeName==='PRE'&&el.className.indexOf('TMLverbatim')!==-1){selectedFormats=ed.formatter.matchAll(ed.plugins.foswikibuttons.recipe_names);if(selectedFormats.length>0){current='('+selectedFormats[0]+')';}
sm=m.addMenu({title:'jQuery chili'+current});jQuery.each(recipes,function(name,format){sm.add({title:'&lt;verbatim class="'+name+'"&gt;',cmd:'foswikiVerbatimClass',value:name});});sm.add({title:'&lt;verbatim&gt;',cmd:'foswikiVerbatimClass',value:''});}});}
ed.addCommand('foswikiVerbatimClass',function(ui,val){jQuery.each(recipes,function(name,format){ed.formatter.remove(name);});if(val){ed.formatter.apply(val);}});return;}});tinymce.PluginManager.add('foswikibuttons',tinymce.plugins.FoswikiButtons);})();