StyleToolbar.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. L.StyleToolbar = L.Toolbar.extend({
  2. options: {
  3. stylePicker:{
  4. title: L.drawLocal.style.toolbar.stylePicker.title
  5. }
  6. },
  7. initialize: function (options) {
  8. // Ensure that the options are merged correctly since L.extend is only shallow
  9. for (var type in this.options) {
  10. if (this.options.hasOwnProperty(type)) {
  11. if (options[type]) {
  12. options[type] = L.extend({}, this.options[type], options[type]);
  13. }
  14. }
  15. }
  16. L.Toolbar.prototype.initialize.call(this, options);
  17. },
  18. addToolbar: function (map) {
  19. var _this = this;
  20. var container = L.DomUtil.create('div', 'leaflet-draw-section'),
  21. buttonIndex = 0,
  22. buttonClassPrefix = 'leaflet-draw-style';
  23. container.id = 'drawStyle';
  24. L.DomEvent.on(container, 'click', L.DomEvent.stopPropagation)
  25. .on(container, 'mousedown', L.DomEvent.stopPropagation)
  26. .on(container, 'dblclick', L.DomEvent.stopPropagation)
  27. .on(container, 'click', L.DomEvent.preventDefault);
  28. this._toolbarContainer = L.DomUtil.create('div', 'leaflet-draw-toolbar leaflet-bar');
  29. if (this.options.stylePicker) {
  30. this._initModeHandler(
  31. new L.Style.StylePicker(map, this.options.stylePicker),
  32. this._toolbarContainer,
  33. buttonIndex++,
  34. buttonClassPrefix
  35. );
  36. }
  37. // Save button index of the last button, -1 as we would have ++ after the last button
  38. this._lastButtonIndex = --buttonIndex;
  39. //Create the edit Panel this._actionsContainer
  40. this._actionsContainer = L.DomUtil.create('div', 'leaflet-draw-toolbar-style-edit');
  41. //文本大小
  42. this._fontsize_label = L.DomUtil.create('label','leaflet-draw-toolbar-style-edit-fontsize-label');
  43. this._fontsize_label.innerHTML = L.drawLocal.style.toolbar.fontSize;
  44. this._fontsize_input = L.DomUtil.create('input','leaflet-draw-toolbar-style-edit-fontsize-input');
  45. this._fontsize_input.value = '12';
  46. this._fontsize_input.style.textAlign = 'center';
  47. this._fontsize_input.id = 'fontSizeInput';
  48. L.DomEvent.on(this._fontsize_input,'change',function(e){
  49. var fontSize = e.target?e.target.value: e.srcElement.value;
  50. if(fontSize ==='' || fontSize.indexOf(' ') >= 0){
  51. window.alert('文本大小不能为空');
  52. return false;
  53. }
  54. if((/[^0-9]/g).test(fontSize)){
  55. window.alert('文本大小只能是数字');
  56. return false;
  57. }
  58. if(fontSize*1 > 30 || fontSize*1 < 12){
  59. window.alert('请输入大于12小于30的字体大小。');
  60. L.DrawStyle.shapeOptions.fontSize = 12;
  61. return false;
  62. }
  63. L.DrawStyle.shapeOptions.fontSize = fontSize;
  64. _this.resetStyle();
  65. });
  66. //文本颜色
  67. this._fontcolor_label = L.DomUtil.create('label','leaflet-draw-toolbar-style-edit-fontcolor-label');
  68. this._fontcolor_label.innerHTML = L.drawLocal.style.toolbar.fontColor;
  69. this._fontcolor_input = L.DomUtil.create('input','leaflet-draw-toolbar-style-edit-fontcolor-input');
  70. this._fontcolor_input.value = '#000000';
  71. this._fontcolor_input.id = 'fontColorInput';
  72. this._fontcolor_input.style.color = '#000000';
  73. this._fontcolor_input.style.background = '#000000';
  74. L.DomEvent.on(this._fontcolor_input,'change',function(e){
  75. var el = e.target ? e.target : e.srcElement;
  76. L.DrawStyle.shapeOptions.fontColor = el.value;
  77. el.style.color = el.value;
  78. el.style.background = el.value;
  79. _this.resetStyle();
  80. });
  81. L.DomEvent.on(this._fontcolor_input,'click',function(e){
  82. var el = e.target ? e.target : e.srcElement;
  83. _this._colorPicker.cEle = el.id;
  84. _this._colorPicker.style.display = 'block';
  85. });
  86. //线条粗细
  87. this._thickness_label = L.DomUtil.create('label','leaflet-draw-toolbar-style-edit-thickness-label');
  88. this._thickness_label.innerHTML = L.drawLocal.style.toolbar.thickness;
  89. this._thickness_input = L.DomUtil.create('input','leaflet-draw-toolbar-style-edit-thickness-input');
  90. this._thickness_input.value = '2';
  91. this._thickness_input.style.textAlign = 'center';
  92. this._thickness_input.id='thicknessInput';
  93. L.DomEvent.on(this._thickness_input,'change',function(e){
  94. var el = e.target ? e.target : e.srcElement;
  95. if(el.value ==='' || el.value.indexOf(' ') >= 0){
  96. window.alert('线条粗细不能为空');
  97. return false;
  98. }
  99. if((/[^0-9]/g).test(el.value)){
  100. window.alert('线条粗细只能是数字');
  101. return false;
  102. }
  103. if(el.value*1 > 100){
  104. window.alert('线条粗细不能超过100');
  105. return false;
  106. }
  107. L.DrawStyle.shapeOptions.weight = el.value*1;
  108. _this.resetStyle();
  109. });
  110. //线条颜色
  111. this._linecolor_label = L.DomUtil.create('label','leaflet-draw-toolbar-style-edit-linecolor-label');
  112. this._linecolor_label.innerHTML = L.drawLocal.style.toolbar.lineColor;
  113. this._linecolor_input = L.DomUtil.create('input','leaflet-draw-toolbar-style-edit-linecolor-input');
  114. this._linecolor_input.style.background = '#FF6600';
  115. this._linecolor_input.style.color ='#ff6600';
  116. this._linecolor_input.id='lineColorInput';
  117. this._linecolor_input.value = '#ff6600';
  118. L.DomEvent.on(this._linecolor_input,'change',function(e){
  119. var el = e.target ? e.target : e.srcElement;
  120. L.DrawStyle.shapeOptions.color = el.value;
  121. el.style.background = el.value;
  122. el.style.color = el.value;
  123. _this.resetStyle();
  124. });
  125. L.DomEvent.on(this._linecolor_input,'click',function(e){
  126. var el = e.target ? e.target : e.srcElement;
  127. _this._colorPicker.cEle = el.id;
  128. _this._colorPicker.style.display = 'block';
  129. });
  130. //填充色
  131. this._fillcolor_label = L.DomUtil.create('label','leaflet-draw-toolbar-style-edit-fillcolor-label');
  132. this._fillcolor_label.innerHTML = L.drawLocal.style.toolbar.fillColor;
  133. this._fillcolor_input = L.DomUtil.create('input','leaflet-draw-toolbar-style-edit-fillcolor-input');
  134. this._fillcolor_input.style.background = '#ff8800';
  135. this._fillcolor_input.style.color ='#ff8800';
  136. this._fillcolor_input.id='fillColorInput';
  137. this._fillcolor_input.value = '#ff8800';
  138. L.DomEvent.on(this._fillcolor_input,'change',function(e){
  139. var el = e.target ? e.target : e.srcElement;
  140. L.DrawStyle.shapeOptions.fillColor = el.value;
  141. el.style.background = el.value;
  142. el.style.color = el.value;
  143. _this.resetStyle();
  144. });
  145. L.DomEvent.on(this._fillcolor_input,'click',function(e){
  146. var el = e.target ? e.target : e.srcElement;
  147. _this._colorPicker.cEle = el.id;
  148. _this._colorPicker.style.display = 'block';
  149. });
  150. //透明度
  151. this._opacity_label = L.DomUtil.create('label','leaflet-draw-toolbar-style-edit-opacity-label');
  152. this._opacity_label.innerHTML = L.drawLocal.style.toolbar.opacity;
  153. this._opacity_input = L.DomUtil.create('input','leaflet-draw-toolbar-style-edit-opacity-input');
  154. this._opacity_input.value = '0.8';
  155. this._opacity_input.style.textAlign='center';
  156. this._opacity_input.id='opacityInput';
  157. L.DomEvent.on(this._opacity_input,'change',function(e){
  158. var el = e.target ? e.target : e.srcElement;
  159. if(el.value ==='' || el.value.indexOf(' ') >= 0){
  160. window.alert('透明度不能为空');
  161. return false;
  162. }
  163. if((/[^\.0-9]/g).test(el.value) || el.value<0 || el.value>1){
  164. window.alert('透明度只能是0-1的数值');
  165. return false;
  166. }
  167. L.DrawStyle.shapeOptions.opacity = el.value*1;
  168. L.DrawStyle.shapeOptions.fillOpacity = el.value*1/2;
  169. _this.resetStyle();
  170. });
  171. this._actionsContainer.appendChild(this._fontsize_label);
  172. this._actionsContainer.appendChild(this._fontsize_input);
  173. this._actionsContainer.appendChild(this._fontcolor_label);
  174. this._actionsContainer.appendChild(this._fontcolor_input);
  175. this._actionsContainer.appendChild(this._thickness_label);
  176. this._actionsContainer.appendChild(this._thickness_input);
  177. this._actionsContainer.appendChild(this._linecolor_label);
  178. this._actionsContainer.appendChild(this._linecolor_input);
  179. this._actionsContainer.appendChild(this._fillcolor_label);
  180. this._actionsContainer.appendChild(this._fillcolor_input);
  181. this._actionsContainer.appendChild(this._opacity_label);
  182. this._actionsContainer.appendChild(this._opacity_input);
  183. this._colorPicker = L.DomUtil.create('div', 'leaflet-draw-toolbar-style-color-picker');
  184. var colorContainer = L.DomUtil.create('ul', 'leaflet-draw-toolbar-style-color-picker-actions',this._colorPicker);
  185. this._colorPicker.aColor = ['00','33','66','99','cc','ff'];
  186. for(var r =0;r<6;r++){
  187. for(var g = 0; g<6;g++){
  188. for(var b= 0;b<6;b++){
  189. var color = '#'+this._colorPicker.aColor[r].toString()+this._colorPicker.aColor[g].toString()+this._colorPicker.aColor[b].toString();
  190. var li = L.DomUtil.create('li', '', colorContainer);
  191. li.title = color;
  192. li.style.background = color;
  193. li.setAttribute('colorText',color);
  194. L.DomEvent.on(li,'click',function(e){
  195. _this._colorPicker.currentColor = (e.target? e.target: e.srcElement).getAttribute('colorText');
  196. _this._changeColor();
  197. });
  198. }
  199. }
  200. }
  201. // Add draw and cancel containers to the control container
  202. container.appendChild(this._toolbarContainer);
  203. container.appendChild(this._actionsContainer);
  204. this._actionsContainer.appendChild(this._colorPicker);
  205. return container;
  206. },
  207. _changeColor:function(){
  208. var _this = this;
  209. var cEle = document.getElementById(_this._colorPicker.cEle);
  210. cEle.value = _this._colorPicker.currentColor;
  211. _this._colorPicker.style.display = 'none';
  212. if(L.Browser.ielt9){
  213. cEle.fireEvent('onChange');
  214. }else {
  215. var evt = document.createEvent('MouseEvents');
  216. evt.initMouseEvent('change',true,true,window,0,0,0,0,0,false,false,false,false,0,null);
  217. cEle.dispatchEvent(evt);
  218. }
  219. },
  220. changeType:function(type){
  221. switch (type){
  222. case 'label':
  223. this._fontsize_label.style.display = 'block';
  224. this._fontsize_input.style.display = 'block';
  225. this._fontcolor_label.style.display = 'block';
  226. this._fontcolor_input.style.display = 'block';
  227. this._thickness_label.style.display = 'none';
  228. this._thickness_input.style.display = 'none';
  229. this._linecolor_label.style.display = 'none';
  230. this._linecolor_input.style.display = 'none';
  231. this._fillcolor_label.style.display = 'none';
  232. this._fillcolor_input.style.display = 'none';
  233. this._opacity_label.style.display = 'none';
  234. this._opacity_input.style.display = 'none';
  235. break;
  236. case 'circle':
  237. case 'customcircle':
  238. case 'polygon':
  239. case 'rectangle':
  240. case 'latlngspolygon':
  241. this._fontsize_label.style.display = 'none';
  242. this._fontsize_input.style.display = 'none';
  243. this._fontcolor_label.style.display = 'none';
  244. this._fontcolor_input.style.display = 'none';
  245. this._thickness_label.style.display = 'block';
  246. this._thickness_input.style.display = 'block';
  247. this._linecolor_label.style.display = 'block';
  248. this._linecolor_input.style.display = 'block';
  249. this._fillcolor_label.style.display = 'block';
  250. this._fillcolor_input.style.display = 'block';
  251. this._opacity_label.style.display = 'block';
  252. this._opacity_input.style.display = 'block';
  253. break;
  254. case 'polyline':
  255. case 'latlngspolyline':
  256. case 'painting':
  257. this._fontsize_label.style.display = 'none';
  258. this._fontsize_input.style.display = 'none';
  259. this._fontcolor_label.style.display = 'none';
  260. this._fontcolor_input.style.display = 'none';
  261. this._fillcolor_label.style.display = 'none';
  262. this._fillcolor_input.style.display = 'none';
  263. this._thickness_label.style.display = 'block';
  264. this._thickness_input.style.display = 'block';
  265. this._linecolor_label.style.display = 'block';
  266. this._linecolor_input.style.display = 'block';
  267. this._opacity_label.style.display = 'block';
  268. this._opacity_input.style.display = 'block';
  269. break;
  270. default :
  271. this.fire('disable');
  272. break;
  273. }
  274. },
  275. setOptions: function (options) {
  276. L.setOptions(this, options);
  277. for (var type in this._modes) {
  278. if (this._modes.hasOwnProperty(type) && options.hasOwnProperty(type)) {
  279. this._modes[type].handler.setOptions(options[type]);
  280. }
  281. }
  282. },
  283. resetStyle:function(){
  284. this.fire('resetStyle');
  285. },
  286. enable:function(){
  287. },
  288. disable: function (e) {
  289. this.changeType(e.target._activeMode.handler.type);
  290. },
  291. _save: function () {
  292. }
  293. });