FR.components.tagsPanel=Ext.extend(Ext.Panel,{cls:"tagsPanel",readOnly:false,initComponent:function(){this.enableTagDrag=this.detailsPanel.infoPanel.id=="FR-Info-Region";if(Ext.isMobile){this.cls+=" mobile"}this.addTagInput=new Ext.ux.form.AutoCompleteField({xtype:"autocompletefield",emptyText:"Add tags...",cls:"addTagsField",autoSelect:false,loadingText:"Searching tags for suggestions",listeners:{select:function(f,r){this.writeTagToFile(r.data.v,this.addTagItem(r.data.v,true));f.reset()},specialkey:function(f,e){if(e.getKey()==e.ENTER){var val=f.getRawValue();if(!val){return false}this.writeTagToFile(val,this.addTagItem(val,true));f.reset()}},scope:this}});this.bbar=[this.addTagInput];FR.components.tagsPanel.superclass.initComponent.apply(this,arguments)},setReadOnly:function(readOnly){this.readOnly=readOnly;this.el.toggleClassIf("tagsPanelReadOnly",readOnly)},reset:function(){this.removeAll(true);this.addTagInput.reset()},setNew:function(tags){var btns=[];Ext.each(tags,function(tag){btns.push(this.createTagItemCfg(tag.v))},this);this.add(btns);this.getBottomToolbar().setVisible(!this.readOnly);this.doLayout()},removeTagFromFile:function(btn){this.detailsPanel.setMetadata({url:"/?module=metadata&section=tags&page=remove",params:{"tags[]":[btn.tagValue]},successCallback:function(){if(!btn.isDestroyed){this.remove(btn)}},failureCallback:function(){if(!btn.isDestroyed){btn.show();this.doLayout()}},scope:this});return this},writeTagToFile:function(v,btn){this.detailsPanel.setMetadata({url:"/?module=metadata&section=tags&page=add",params:{"tags[]":[v]},failureCallback:function(){this.hideTagItem(btn).remove(btn)},scope:this})},hideTagItem:function(btn){btn.hide();this.doLayout();return this},createTagItemCfg:function(tag){var btnCfg={xtype:"button",cls:"tagBtn",style:"color:"+FR.utils.getTagColor(tag),text:Ext.util.Format.htmlEncode(tag),tagValue:tag,enableDrag:this.enableTagDrag};if(!Ext.isMobile){btnCfg.handler=function(){FR.actions.filterMeta("tag",this.tagValue,"exact")};if(!this.readOnly){btnCfg.menuIndicator={iconCls:"fa-times",handler:function(e){e.stopEvent();this.ownerCt.hideTagItem(this).removeTagFromFile(this)}}}}return btnCfg},addTagItem:function(tag,update){var btn=this.add(this.createTagItemCfg(tag));if(update){this.doLayout();window.scrollIntoView(this.addTagInput.el.dom,{scrollMode:"if-needed"});this.addTagInput.focus()}return btn},showMenu:function(btn){this.focusedTagItem=btn;this.menu.removeBtn.setVisible(!this.readOnly);this.menu.show(btn.el)},listeners:{afterrender:function(p){p.menu=new Ext.menu.Menu({items:[{text:"Search by",iconCls:"fa-search",handler:function(){FR.actions.filterMeta("tag",p.focusedTagItem.tagValue,"exact")},scope:p},{text:"Copy",iconCls:"fa-clipboard",handler:function(){FR.UI.copyToClipboard(p.focusedTagItem.tagValue,"The tag has been copied to clipboard.")},scope:p},{text:"Remove",iconCls:"fa-times",ref:"removeBtn",handler:function(){p.hideTagItem(p.focusedTagItem).removeTagFromFile(p.focusedTagItem)},scope:p}]});this.body.on(Ext.isPhone?"click":"contextmenu",function(e){var el=e.getTarget(".frBtn");if(!el){return false}var btn=Ext.getCmp(el.id);p.showMenu(btn)},p);if(this.enableTagDrag&&User.perms.metadata){new Ext.dd.DragZone(this.body,{ddGroup:"tagging",getDragData:function(e){var t=e.getTarget(".frBtn");if(t){var btn=Ext.getCmp(t.id);return{ddel:Ext.DomHelper.createDom({html:btn.tagValue}),tagValue:btn.tagValue,repairXY:btn.el.getXY()}}},getRepairXY:function(){return this.dragData.repairXY}})}}}});