baidumap.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*******************************************************************************
  2. * KindEditor - WYSIWYG HTML Editor for Internet
  3. * Copyright (C) 2006-2011 kindsoft.net
  4. *
  5. * @author Roddy <luolonghao@gmail.com>
  6. * @site http://www.kindsoft.net/
  7. * @licence http://www.kindsoft.net/license.php
  8. *******************************************************************************/
  9. // Baidu Maps: http://dev.baidu.com/wiki/map/index.php?title=%E9%A6%96%E9%A1%B5
  10. KindEditor.plugin('baidumap', function(K) {
  11. var self = this, name = 'baidumap', lang = self.lang(name + '.');
  12. var mapWidth = K.undef(self.mapWidth, 558);
  13. var mapHeight = K.undef(self.mapHeight, 360);
  14. self.clickToolbar(name, function() {
  15. var html = ['<div style="padding:10px 20px;">',
  16. '<div class="ke-header">',
  17. // left start
  18. '<div class="ke-left">',
  19. lang.address + ' <input id="kindeditor_plugin_map_address" name="address" class="ke-input-text" value="" style="width:200px;" /> ',
  20. '<span class="ke-button-common ke-button-outer">',
  21. '<input type="button" name="searchBtn" class="ke-button-common ke-button" value="' + lang.search + '" />',
  22. '</span>',
  23. '</div>',
  24. // right start
  25. '<div class="ke-right">',
  26. '<input type="checkbox" id="keInsertDynamicMap" name="insertDynamicMap" value="1" /> <label for="keInsertDynamicMap">' + lang.insertDynamicMap + '</label>',
  27. '</div>',
  28. '<div class="ke-clearfix"></div>',
  29. '</div>',
  30. '<div class="ke-map" style="width:' + mapWidth + 'px;height:' + mapHeight + 'px;"></div>',
  31. '</div>'].join('');
  32. var dialog = self.createDialog({
  33. name : name,
  34. width : mapWidth + 42,
  35. title : self.lang(name),
  36. body : html,
  37. yesBtn : {
  38. name : self.lang('yes'),
  39. click : function(e) {
  40. var map = win.map;
  41. var centerObj = map.getCenter();
  42. var center = centerObj.lng + ',' + centerObj.lat;
  43. var zoom = map.getZoom();
  44. var url = [checkbox[0].checked ? self.pluginsPath + 'baidumap/index.html' : 'http://api.map.baidu.com/staticimage',
  45. '?center=' + encodeURIComponent(center),
  46. '&zoom=' + encodeURIComponent(zoom),
  47. '&width=' + mapWidth,
  48. '&height=' + mapHeight,
  49. '&markers=' + encodeURIComponent(center),
  50. '&markerStyles=' + encodeURIComponent('l,A')].join('');
  51. if (checkbox[0].checked) {
  52. self.insertHtml('<iframe src="' + url + '" frameborder="0" style="width:' + (mapWidth + 2) + 'px;height:' + (mapHeight + 2) + 'px;"></iframe>');
  53. } else {
  54. self.exec('insertimage', url);
  55. }
  56. self.hideDialog().focus();
  57. }
  58. },
  59. beforeRemove : function() {
  60. searchBtn.remove();
  61. if (doc) {
  62. doc.write('');
  63. }
  64. iframe.remove();
  65. }
  66. });
  67. var div = dialog.div,
  68. addressBox = K('[name="address"]', div),
  69. searchBtn = K('[name="searchBtn"]', div),
  70. checkbox = K('[name="insertDynamicMap"]', dialog.div),
  71. win, doc;
  72. var iframe = K('<iframe class="ke-textarea" frameborder="0" src="' + self.pluginsPath + 'baidumap/map.html" style="width:' + mapWidth + 'px;height:' + mapHeight + 'px;"></iframe>');
  73. function ready() {
  74. win = iframe[0].contentWindow;
  75. doc = K.iframeDoc(iframe);
  76. }
  77. iframe.bind('load', function() {
  78. iframe.unbind('load');
  79. if (K.IE) {
  80. ready();
  81. } else {
  82. setTimeout(ready, 0);
  83. }
  84. });
  85. K('.ke-map', div).replaceWith(iframe);
  86. // search map
  87. searchBtn.click(function() {
  88. win.search(addressBox.val());
  89. });
  90. });
  91. });