text: // COPYRIGHT © 2017 Esri
  //
  // All rights reserved under the copyright laws of the United States
  // and applicable international laws, treaties, and conventions.
  //
  // This material is licensed for use under the Esri Master License
  // Agreement (MLA), and is bound by the terms of that agreement.
  // You may redistribute and use this code without modification,
  // provided you adhere to the terms of the MLA and include this
  // copyright notice.
  //
  // See use restrictions at http://www.esri.com/legal/pdfs/mla_e204_e300/english
  //
  // For additional information, contact:
  // Environmental Systems Research Institute, Inc.
  // Attn: Contracts and Legal Services Department
  // 380 New York Street
  // Redlands, California, USA 92373
  // USA
  //
  // email: contracts@esri.com
  //
  // See http://js.arcgis.com/4.6/esri/copyright.txt for details.
  
  define(["dojo/dom","dojo/dom-attr","dojo/dom-class","dojo/dom-construct","dojo/dom-style","dojo/html","dojo/keys","dojo/on","dojo/query","dojo/sniff","dojo/string","dstore/Memory","dijit/focus","dijit/form/TextBox","dijit/registry","dijit/Tooltip","dijit/_OnDijitClickMixin","dijit/_TemplatedMixin","./Widgette","dgrid/OnDemandGrid","dgrid/Selection","dgrid/Keyboard","../core/lang","dojo/i18n!./Tags/nls/Tags","dojo/NodeList-traverse","dojo/NodeList-manipulate"],function(t,i,e,s,r,h,o,a,n,d,_,c,l,u,g,f,C,S,p,E,T,L,m,y){return p.createSubclass([C,S],{declaredClass:"esri.widgets.Tags",templateString:'<div class="${baseClass}"></div>',baseClass:"esri-tags",constructor:function(t){this._idProperty="tag",this._tags=[],this._selRows=[],this._CHOICE_ALL_SELECTOR=this._CSS_STYLES.CLASS_SELECTOR+this._CSS_STYLES.CHOICE+this._CSS_STYLES.ALL_SELECTOR,this._CHOICE_FOCUS=this._CSS_STYLES.CLASS_SELECTOR+this._CSS_STYLES.SEARCH_CHOICE_FOCUS,this._CHOICE_FOCUS_ALL=this._CHOICE_FOCUS+this._CSS_STYLES.ALL_SELECTOR},postMixInProperties:function(){this.inherited(arguments);var t=(new Date).getTime();this._tagsId="userTags-"+t,this._gridId="grid-"+t,this._filterId="filter-"+t,this.i18n=y},postCreate:function(){this._attachmentNode=s.create("div",{id:this._tagsId,className:"grid_1"},this.domNode),this._createContainerNode(),this._createTagList(),this._createDropDownList(),this._createInput();var t=[{field:this._idProperty}],i=[{property:this._idProperty,ascending:!0}];this._store=new c({idProperty:this._idProperty,data:[]});var r=E.createSubclass([T,L]);this._grid=new r({collection:this._store,showHeader:!1,noDataMessage:this.i18n.noTagsFound,selectionMode:"extended",allowSelectAll:!0,cellNavigation:!1,columns:t,sort:i,renderRow:this._renderItem},this._dropDownList),this._grid.startup(),e.add(this._grid.domNode,"grid-height-limiter");var h;this.own(this._inputTextBox.watch("value",function(){h&&(clearTimeout(h),h=null),this._grid.refresh()}.bind(this))),this.own(this._grid.on(".dgrid-row:click",function(t){var i="";this._shiftKeyPressed||this._metaKeyPressed?t.shiftKey||t.metaKey||t.ctrlKey||(i=this._selRows[0],this._createTags(i),this._store.remove(i),this._grid.refresh(),this._resetInputTextBox()):(i=this._grid.row(t).data.tag,this._createTags(i),this._store.remove(i),this._grid.refresh(),this._resetInputTextBox())}.bind(this))),this.own(this._grid.on("dgrid-deselect",function(t){this._selRows=[];for(var i in this._grid.selection)this._grid.selection.hasOwnProperty(i)&&this._selRows.push(i)}.bind(this))),this.own(this._grid.on("dgrid-select",function(t){this._selRows=[];for(var i in this._grid.selection)this._grid.selection.hasOwnProperty(i)&&this._selRows.push(i)}.bind(this))),this.own(a(this.domNode,"keydown",this._keydownHandler.bind(this))),this.own(a(this.domNode,"keyup",this._keyupHandler.bind(this))),window.onkeydown=function(t){(t.shiftKey||t.ctrlKey||224===t.keyCode)&&(this._metaKeyPressed=!0)}.bind(this),this.own(a(document,"onkeydown",function(t){this._shiftKeyPressed=!0,this._metaKeyPressed=!0}.bind(this)))},_attachmentNode:"",_autocompleteList:"",_grid:"",_store:"",_idProperty:"",_gridId:"",_filterId:"",_placeHolder:"",_noDataMsg:"",_inputTextBox:"",_gridHasFocus:!1,_metaKeyPressed:!1,_shiftKeyPressed:!1,_CSS_STYLES:{CLASS_SELECTOR:".",ALL_SELECTOR:">",MULTI:"select2-container select2-container-multi",CHOICES:"select2-choices",CHOICE:"select2-search-choice",SEARCH_CHOICE_FOCUS:"select2-search-choice-focus",SEARCH_FIELD:"select2-search-field",CLOSE_ICON:"select2-search-choice-close"},_selRows:[],_CHOICE_SELECTOR:"",_CHOICE_FOCUS:"",_CHOICE_FOCUS_ALL:"",properties:{knownTags:{set:function(t){var i,e=[],s=this._tags;for(i=0;i<t.length;i++)s.indexOf(t[i])<0&&e.push(t[i]);this._store=new c({idProperty:this._idProperty,data:e}),this._grid.set("collection",this._store),this._grid.refresh()}},matchParam:{value:"first",cast:function(t){return t?t:"first"}},maxWidth:{value:"auto",cast:function(t){return t?t:"auto"}},minWidth:{value:"auto",cast:function(t){return t?t:"auto"}},required:!1,value:{set:function(t){this._setTagValues(t)},get:function(){return this._getTagValues()}}},isValid:function(){return!this.required||m.isDefined(this._tags)&&this._tags.length>0},validate:function(){this._created&&!this.isValid()?(i.set(this.domNode,"aria-invalid","true"),this._displayMessage(this.i18n.required)):(i.set(this.domNode,"aria-invalid","false"),this._clearMessage())},reset:function(){this.clearTags()},focus:function(){l.focus(this.domNode),this._inputTextBox.focus()},prepopulate:function(t){t.forEach(function(t,i){this._createTags(t),this._store.remove(t)},this)},clearTags:function(){var i,e=n(this._CSS_STYLES.CLASS_SELECTOR+this._CSS_STYLES.CHOICE,t.byId(this.id)),r=!1;e.length>0&&(r=!0,e.forEach(function(e,r){try{i=n(this._CHOICE_ALL_SELECTOR,t.byId(this.id))[0].title,this._store.add({tag:i})}catch(h){}s.destroy(e)},this),this._tags=[],this._set("value",""))},addStyledTags:function(i,o){e.add(t.byId(o),this._CSS_STYLES.MULTI);var a=s.create("ul",null,t.byId(o));e.add(a,this._CSS_STYLES.CHOICES),r.set(a,"border","none"),i.forEach(function(t,i){var o=s.create("li",null,a);r.set(o,"padding","3px 5px 3px 5px"),e.add(o,"select2-search-resultSet");var n=s.create("div",{title:t},o);h.set(n,t)})},_setTagValues:function(t){var i;"string"==typeof t?i=t.split(","):t&&Array.isArray(t)&&(i=t),this.clearTags(),this.prepopulate(i?this._getUniqueTags(i):[])},_getTagValues:function(){return this._tags?this._tags.join(","):""},_clearMessage:function(){this._displayMessage(null)},_displayMessage:function(i){var e=t.byId(this._tagsId);i&&this.focused?f.show(i,e,["after"]):f.hide(e)},_resetInputTextBox:function(){this._inputTextBox.set("value","")},_isEmpty:function(t){return 0===t.trim().length},_navigate:function(t,i,e,s){t.length>0&&1>i?("right"===s?this._hightlightTag(t.next()):this._hightlightTag(t.prev()),this._unhighlightTag(t)):1>i&&this._hightlightTag(e)},_contains:function(t,i){return t.indexOf(i)>=0},_hightlightTag:function(t){t.addClass(this._CSS_STYLES.SEARCH_CHOICE_FOCUS)},_unhighlightTag:function(t){t.removeClass(this._CSS_STYLES.SEARCH_CHOICE_FOCUS)},_removeTag:function(t){t&&-1!==this._tags.indexOf(t)&&(this._tags.splice(this._tags.indexOf(t),1),this._set("value",this._tags.join(",")))},_hideGrid:function(){t.byId(this._gridId)&&r.set(t.byId(this._gridId),"display","none"),this._gridHasFocus=!1},_showGrid:function(){r.set(t.byId(this._gridId),"display","block");var i=r.get(t.byId(this._attachmentNode),"width");r.set(t.byId(this._gridId),"width",i+"px")},_setFocusOnFirstRow:function(t,i){return n(".dgrid-content .dgrid-cell",this._grid.domNode)[i]||n(".dgrid-content .dgrid-row",this._grid.domNode)[i]},_createTags:function(i){n(this._CHOICE_FOCUS,t.byId(this.id)).removeClass("select2-search-choice-focus");var r=s.create("li",null,this._autocompleteList);e.add(r,this._CSS_STYLES.CHOICE);var o=s.create("div",{title:i},r);h.set(o,this._htmlEncode(i));var d=s.create("a",{href:"#"},o);e.add(d,this._CSS_STYLES.CLOSE_ICON),a(d,"click",function(t){var i=_.trim(t.target.parentElement.innerText||t.target.parentElement.textContent);try{this._store.add({tag:i})}catch(e){}this._grid.refresh(),this._removeTag(i),s.destroy(t.target.parentNode.parentNode),t.preventDefault()}.bind(this));var c=g.byId(this._filterId);s.place(c.domNode,this._autocompleteList,"last"),this._hideGrid(),this._tags.push(i),this._set("value",this._tags.join(","))},_renderItem:function(t){return s.create("div",{innerHTML:t.tag})},_createContainerNode:function(){this._containerNode=s.create("div",null,this._attachmentNode),e.add(this._containerNode,this._CSS_STYLES.MULTI),r.set(this._containerNode,{maxWidth:this.maxWidth,minWidth:this.minWidth})},_createTagList:function(){this._autocompleteList=s.create("ul",null,this._containerNode),e.add(this._autocompleteList,this._CSS_STYLES.CHOICES)},_createInput:function(){var t=s.create("li",null,this._autocompleteList,"last");e.add(t,this._CSS_STYLES.SEARCH_FIELD),this._inputTextBox=new u({id:this._filterId,placeHolder:this.i18n.addTags,intermediateChanges:!0,trim:!0,style:{border:"none"}},t),e.add(this._inputTextBox,"input-text-box"),r.set(this._inputTextBox,"width",this.minWidth),(d("ie")>8||d("trident")>4)&&e.add(this._inputTextBox.domNode,"ie-style"),this.own(l.watch("curNode",this._blurHandler.bind(this)))},_createDropDownList:function(){this._dropDownList=s.create("div",{id:this._gridId},this._containerNode),e.add(this._dropDownList,"drop-down-list"),r.set(this._dropDownList,"width",this.minWidth)},_getUniqueTags:function(t){var i,e=[];return t.forEach(function(t){i=this._stripHTML(t),m.isDefined(i)&&i.length>0&&e.push(i)},this),e.filter(function(t,i){return e.indexOf(t)===i},this)},_stripHTML:function(t){return t&&t.replace(/<(?:.|\s)*?>/g,"")},_htmlEncode:function(t){return t?_.escape(t):t},_keyupHandler:function(t){var i=this._inputTextBox?this._inputTextBox.get("value").length:0;i>0?this._applyFilter():this._removeFilter()},_keydownHandler:function(i){this._clearMessage();var e,h,a,d,c,u=n(this._CHOICE_FOCUS,t.byId(this.id)),g=n(this._CSS_STYLES.CLASS_SELECTOR+this._CSS_STYLES.CHOICE,t.byId(this.id)).last();switch(void 0!==l.curNode.value&&(c=l.curNode.value.length),i.keyCode){case o.RIGHT_ARROW:this._navigate(u,c,g,"right"),this._hideGrid();break;case o.LEFT_ARROW:this._navigate(u,c,g,"left"),this._hideGrid();break;case o.DOWN_ARROW:i.preventDefault(),this._unhighlightTag(u),"none"===r.get(this._gridId,"display")&&this._showGrid(),this._gridHasFocus||(this._grid.focus(this._setFocusOnFirstRow(this._grid,0)),this._gridHasFocus=!0);break;case o.UP_ARROW:break;case o.BACKSPACE:var f;if(0===c&&0===n(this._CHOICE_FOCUS_ALL).length&&void 0!==n(this._CHOICE_ALL_SELECTOR)[n(this._CHOICE_ALL_SELECTOR).length-1]&&(f=n(this._CHOICE_ALL_SELECTOR)[n(this._CHOICE_ALL_SELECTOR).length-1].title,n("li",this._attachmentNode).length>0&&(s.destroy(g[0]),void 0!==f)))try{this._store.add({tag:f})}catch(C){}if(n(this._CHOICE_FOCUS_ALL).length>0&&(f=n(this._CHOICE_FOCUS_ALL).text(),s.destroy(u[0]),void 0!==f))try{this._store.add({tag:f})}catch(C){}this._grid.refresh(),0===c&&this._hideGrid(),this._removeTag(f),this.validate();break;case o.CTRL:this._metaKeyPressed=!0;break;case o.META:this._metaKeyPressed=!0;break;case o.SHIFT:this._shiftKeyPressed=!0;break;case o.ENTER:if(c>0)e=this._stripHTML(l.curNode.value),h=e.split(","),a=[],h.forEach(function(t,i){-1===a.indexOf(t)&&a.push(_.trim(t))}),a.forEach(function(t,i){this._isEmpty(t)||this._contains(this._tags,t)||this._createTags(t)},this);else{for(d=0;d<this._selRows.length;d++)this._createTags(this._selRows[d]),this._store.remove(this._selRows[d]);this._shiftKeyPressed=!1,this._metaKeyPressed=!1}this._resetInputTextBox(),i.preventDefault(),l.focus(t.byId(this._filterId));break;case o.TAB:if(l.curNode.id!==this._filterId||0!==c){if(c>0)e=this._stripHTML(l.curNode.value),h=e.split(","),a=[],h.forEach(function(t,i){-1===a.indexOf(t)&&a.push(_.trim(t))}),a.forEach(function(t,i){this._isEmpty(t)||this._contains(this._tags,t)||this._createTags(t)},this);else{for(d=0;d<this._selRows.length;d++)this._createTags(this._selRows[d]),this._store.remove(this._selRows[d]);this._shiftKeyPressed=!1,this._metaKeyPressed=!1}this._resetInputTextBox(),i.preventDefault(),l.focus(t.byId(this._filterId))}break;case o.ESCAPE:this._hideGrid();break;default:c>-1&&("none"===r.get(t.byId(this._gridId),"display")&&this._showGrid(),this._unhighlightTag(u)),this._metaKeyPressed=!1}},_applyFilter:function(){var t,i=new this._store.Filter,e=this._inputTextBox?this._inputTextBox.get("value")+"":"",s=new RegExp("^"+e.toLowerCase(),"i"),r=new RegExp(e.toLowerCase(),"i");t="first"===this.matchParam?i.match(this._idProperty,s):i.match(this._idProperty,r),this._grid.set("collection",this._store.filter(t)),this._grid.refresh()},_removeFilter:function(){this._grid.set("collection",this._store),this._grid.refresh()},_blurHandler:function(t,i,e){if(!this.focused){var s=this._stripHTML(this._inputTextBox.value);if(s.length>0){var r=[],h=s.split(",");h.forEach(function(t,i){-1===r.indexOf(t)&&r.push(_.trim(t))}),r.forEach(function(t,i){this._isEmpty(t)||this._contains(this._tags,t)||this._createTags(t)},this),this._resetInputTextBox(),this._hideGrid()}else this._hideGrid()}this.validate()}})});