text: TC.control=TC.control||{};TC.Control||TC.syncLoadJS(TC.apiLocation+"TC/Control");TC.Consts.event.BEFOREFEATUREMODIFY="beforefeaturemodify.tc";TC.Consts.event.FEATUREMODIFY="featuremodify.tc";TC.Consts.event.FEATURESSELECT="featureselect.tc";TC.Consts.event.FEATURESUNSELECT="featureunselect.tc";TC.Consts.event.CHANGE="change.tc";!function(){TC.control.Modify=function(){TC.Control.apply(this,arguments);this.__firstRender=$.Deferred();this.styles=$.extend(!0,TC.Cfg.styles.selection,this.options.styles);this.styles.text=this.styles.text||{fontSize:this.styles.line.fontSize,fontColor:this.styles.line.fontColor,labelOutlineColor:this.styles.line.labelOutlineColor,labelOutlineWidth:this.styles.line.labelOutlineWidth};this._classSelector="."+this.CLASS;this.wrap=new TC.wrap.control.Modify(this);this._layerPromise=$.Deferred()};TC.inherit(TC.control.Modify,TC.Control);var t=TC.control.Modify.prototype;t.CLASS="tc-ctl-mod";TC.isDebug?t.template=TC.apiLocation+"TC/templates/Modify.html":t.template=function(){dust.register(t.CLASS,e);function e(t,e){return t.w('<button class="tc-ctl-btn tc-ctl-mod-btn-select" disabled title="').h("i18n",e,{},{$key:"select"}).w('">').h("i18n",e,{},{$key:"select"}).w('</button><button class="tc-ctl-btn tc-ctl-mod-btn-delete" disabled title="').h("i18n",e,{},{$key:"deleteSelection"}).w('">').h("i18n",e,{},{$key:"deleteSelection"}).w('</button><button class="tc-ctl-btn tc-ctl-mod-btn-join" disabled title="').h("i18n",e,{},{$key:"joinGeometries.tooltip"}).w('">').h("i18n",e,{},{$key:"joinGeometries"}).w('</button><button class="tc-ctl-btn tc-ctl-mod-btn-split" disabled title="').h("i18n",e,{},{$key:"splitGeometry"}).w('">').h("i18n",e,{},{$key:"splitGeometry"}).w('</button><button class="tc-ctl-btn tc-ctl-mod-btn-text" contenteditable="true" disabled title="').h("i18n",e,{},{$key:"addText"}).w('">').h("i18n",e,{},{$key:"addText"}).w('</button><div class="tc-ctl-mod-style tc-hidden"><input type="text" class="tc-ctl-mod-txt tc-textbox" placeholder="').h("i18n",e,{},{$key:"writeTextForSketch"}).w('" style="font-size:').f(e.get(["fontSize"],!1),e,"h").w("pt;font-color:").f(e.get(["fontColor"],!1),e,"h").w(";text-shadow: 0 0 ").f(e.get(["labelOutlineWidth"],!1),e,"h").w("px ").f(e.get(["labelOutlineColor"],!1),e,"h").w(';" />').h("i18n",e,{},{$key:"textColor"}).w('<input type="color" class="tc-ctl-mod-fnt-c" value="').f(e.get(["fontColor"],!1),e,"h").w('" />').h("i18n",e,{},{$key:"fontSize"}).w('<input type="number" class="tc-ctl-mod-fnt-s tc-textbox" value="').f(e.get(["fontSize"],!1),e,"h").w('" min="7" max="20" /></div>')}e.__dustBody=!0;return e};const e=function(t,e){t._$deleteBtn.prop("disabled",0===e.length);t._$joinBtn.prop("disabled",e.length<2);t._$splitBtn.prop("disabled",0===e.filter(n).length);t.displayLabelText()},n=function(t){var e=!1;(TC.feature.MultiPolygon&&t instanceof TC.feature.MultiPolygon||TC.feature.MultiPolyline&&t instanceof TC.feature.MultiPolyline)&&t.geometry.length>1&&(e=!0);return e};t.register=function(t){var n=this;TC.Control.prototype.register.call(n,t);if(n.options.layer){n.setLayer(n.options.layer);t.on(TC.Consts.event.FEATUREADD+" "+TC.Consts.event.FEATURESADD,function(t){$.when(n.getLayer(),n.renderPromise()).then(function(e){if(t.layer===e){n._$selectBtn.prop("disabled",!1);n._$textBtn.prop("disabled",!1)}})}).on(TC.Consts.event.FEATUREREMOVE+" "+TC.Consts.event.FEATURESCLEAR,function(t){$.when(n.getLayer(),n.renderPromise()).then(function(o){if(t.layer===o){t.feature?n.unselectFeatures([t.feature]):n.unselectFeatures();e(n,n.getSelectedFeatures());if(0===n.layer.features.length){n._$selectBtn.prop("disabled",!0);n.setTextMode(!1);n._$textBtn.prop("disabled",!0)}}})});n.on(TC.Consts.event.FEATURESSELECT,function(t){const o=n.getSelectedFeatures();e(n,o)}).on(TC.Consts.event.FEATURESUNSELECT,function(t){e(n,n.getSelectedFeatures())})}};t.render=function(t){const e=this,n=function(){e._$selectBtn=e._$div.find("."+e.CLASS+"-btn-select").on(TC.Consts.event.CLICK,function(t){t.target.disabled||(e.isActive?e.deactivate():e.activate())});e._$deleteBtn=e._$div.find("."+e.CLASS+"-btn-delete").on(TC.Consts.event.CLICK,function(){e.deleteSelectedFeatures()});e._$textBtn=e._$div.find("."+e.CLASS+"-btn-text").on(TC.Consts.event.CLICK,function(){e.setTextMode(!e.textActive)});e._$joinBtn=e._$div.find("."+e.CLASS+"-btn-join");e._$splitBtn=e._$div.find("."+e.CLASS+"-btn-split");e._$text=e._$div.find("input."+e.CLASS+"-txt").on("input.tc",function(t){e.labelFeatures(t.target.value)});e._$styleSection=e._$div.find("."+e.CLASS+"-style");e._$fontColorPicker=e._$div.find(e._classSelector+"-fnt-c").on(TC.Consts.event.CHANGE,function(t){e.setFontColor(t.target.value)});e._$fontSizeSelector=e._$div.find("."+e.CLASS+"-fnt-s").on(TC.Consts.event.CHANGE,function(t){e.setFontSize(t.target.value)});e.__firstRender.resolve();$.isFunction(t)&&t()},o={fontSize:e.styles.text.fontSize,fontColor:e.styles.text.fontColor,labelOutlineColor:e.styles.text.labelOutlineColor,labelOutlineWidth:e.styles.text.labelOutlineWidth};Modernizr.inputtypes.color?e.renderData(o,n):TC.loadJS(!$.fn.spectrum,TC.apiLocation+"lib/spectrum/spectrum.min.js",function(){TC.loadCSS(TC.apiLocation+"lib/spectrum/spectrum.css");e.renderData(o,function(){e._$div.find("input[type=color]").spectrum({preferredFormat:"hex",showPalette:!0,palette:[],selectionPalette:[],cancelText:e.getLocaleString("cancel"),chooseText:e.getLocaleString("ok"),replacerClassName:e.CLASS+"-str-c"});n()})})};t.renderPromise=function(){return this.__firstRender.promise()};t.activate=function(){this._$selectBtn.addClass(TC.Consts.classes.ACTIVE);TC.Control.prototype.activate.call(this);this.wrap.activate(this.mode)};t.deactivate=function(){this._$selectBtn&&this._$selectBtn.removeClass(TC.Consts.classes.ACTIVE);TC.Control.prototype.deactivate.call(this);this._$selectBtn&&e(this,[]);this.wrap&&this.wrap.deactivate()};t.clear=function(){const t=this;t.layer&&t.layer.clearFatures();return t};t.isExclusive=function(){return!0};t.end=function(){this.wrap.end();return this};t.setMode=function(t,e){const n=this;t&&(n.mode=t);if(e&&t){n.layer&&n.layer.map.putLayerOnTop(n.layer);n.activate()}else n.deactivate();var o=e?TC.Consts.event.CONTROLACTIVATE:TC.Consts.event.CONTROLDEACTIVATE;n.map&&n.map.$events.trigger($.Event(o,{control:n}));return n};t.getLayer=function(){return this.options&&"boolean"==typeof this.options.layer&&!this.options.layer?null:this.layer?this.layer:this._layerPromise};t.setLayer=function(t){var e=this;if(e.map)if("string"==typeof t)e.map.loaded(function(){e.layer=e.map.getLayer(t);e._layerPromise.resolve(e.layer)});else{e.layer=t;e._layerPromise.resolve(e.layer)}};t.getSelectedFeatures=function(){return this.wrap.getSelectedFeatures()};t.setSelectedFeatures=function(t){const e=this.wrap.setSelectedFeatures(t);this.displayLabelText();return e};t.getActiveFeatures=function(){const t=this,e=t.getSelectedFeatures();!e.length&&t.layer.features.length&&e.push(t.layer.features[t.layer.features.length-1]);return e};t.unselectFeatures=function(t){t=t||[];this.wrap.unselectFeatures(t.map(function(t){return t.wrap.feature}));return this};t.deleteSelectedFeatures=function(){const t=this,e=t.getSelectedFeatures();t.wrap.unselectFeatures(e);e.forEach(function(e){t.layer.removeFeature(e)});return t};t.styleFunction=function(t,e){var n;const o=this.map.options.styles.selection;switch(!0){case TC.feature.Polygon&&t instanceof TC.feature.Polygon:case TC.feature.MultiPolygon&&t instanceof TC.feature.MultiPolygon:n=$.extend({},o.polygon);break;case TC.feature.Point&&t instanceof TC.feature.Point:case TC.feature.MultiPoint&&t instanceof TC.feature.MultiPoint:n=$.extend({},o.point);break;default:n=$.extend({},o.line)}const l=t.getStyle();if(l.label){n.label=l.label;n.fontSize=l.fontSize;n.fontColor=l.fontColor;n.labelOutlineColor=l.labelOutlineColor;n.labelOutlineWidth=l.labelOutlineWidth}return n};t.setTextMode=function(t){this.textActive=t;this._$textBtn.toggleClass(TC.Consts.classes.ACTIVE,t);this._$styleSection.toggleClass(TC.Consts.classes.HIDDEN,!t);this.displayLabelText();return this};t.setFontColorWatch=function(t,e){const n=this;void 0===t&&(t=n.styles.text.fontColor);t=TC.Util.colorArrayToString(t);e=e||n.getLabelOutlineColor(t);n.renderPromise().then(function(){n._$fontColorPicker.val(t);Modernizr.inputtypes.color||n._$fontColorPicker.spectrum("set",t);n._$text.css("color",t);n._$text.css("text-shadow","0 0 "+n.styles.text.labelOutlineWidth+"px "+e)});return n};t.setFontColor=function(t){const e=this;e.styles.text.fontColor=t;e.styles.text.labelOutlineColor=e.getLabelOutlineColor(t);e.setFontColorWatch(t,e.styles.text.labelOutlineColor);e.getActiveFeatures().forEach(function(n){const o=n.getStyle();o.fontColor=t;o.labelOutlineColor=e.styles.text.labelOutlineColor;n.setStyle(o)});return e};t.setFontSizeWatch=function(t){const e=this;void 0===t&&(t=e.styles.text.fontSize);const n=parseInt(t);n!==Number.NaN&&e.renderPromise().then(function(){e._$fontSizeSelector.val(n);e._$text.css("font-size",n+"pt")});return e};t.setFontSize=function(t){const e=this,n=parseInt(t);if(n!==Number.NaN){e.styles.text.fontSize=n;e.setFontSizeWatch(n);e.getActiveFeatures().forEach(function(t){const e=t.getStyle();e.fontSize=n;t.setStyle(e)})}return e};t.getLabelOutlineColor=function(t){if(t){const e=(t=TC.Util.colorArrayToString(t)).match(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i);e&&e.length&&(t="#"+e[1]+e[1]+e[2]+e[2]+e[3]+e[3]);const n=t.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);if(n&&n.length){return(parseInt(n[1],16)+parseInt(n[2],16)+parseInt(n[3],16))/3<128?"#fff":"#000"}}return"#fff"};t.displayLabelText=function(){const t=this,e=t.getSelectedFeatures();var n,o,l;if(t.isActive&&e.length){const t=e[e.length-1].getStyle();n=t.label;l=t.fontColor;o=t.fontSize}else{n="";l=t.styles.text.fontColor;o=t.styles.text.fontSize}t.renderPromise().then(function(){t.setFontSizeWatch(o).setFontColorWatch(l)._$text.val(n)});return t};t.labelFeatures=function(t){const e=this,n=e.getActiveFeatures();if(n.length){const o=n[0].getStyle();n.forEach(function(n){const l=$.extend({},e.styles.text,o);o.label=t;o.labelOffset=l.labelOffset;o.fontColor=l.fontColor;o.fontSize=l.fontSize;o.labelOutlineColor=l.labelOutlineColor;o.labelOutlineWidth=l.labelOutlineWidth;n.setStyle(o)})}return e}}();