AddressExplorer.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. MWF.xDesktop.requireApp("Attendance", "Explorer", null, false);
  2. MWF.xDesktop.requireApp("Template", "Explorer", null, false);
  3. MWF.xApplication.Attendance.AddressExplorer = new Class({
  4. Extends: MWF.xApplication.Attendance.Explorer,
  5. Implements: [Options, Events],
  6. initialize: function(node, app, actions, options){
  7. this.setOptions(options);
  8. this.app = app;
  9. this.path = "/x_component_Attendance/$AddressExplorer/";
  10. this.cssPath = "/x_component_Attendance/$AddressExplorer/"+this.options.style+"/css.wcss";
  11. this._loadCss();
  12. this.actions = actions;
  13. this.node = $(node);
  14. this.initData();
  15. },
  16. reload: function(){
  17. this.node.empty();
  18. this.load();
  19. },
  20. load: function(){
  21. this.loadToolbar();
  22. this.loadContentNode();
  23. this.loadContent();
  24. this.setNodeScroll();
  25. },
  26. destroy: function(){
  27. if( this.baiduMap ){
  28. this.baiduMap.map.clearOverlays();
  29. this.elementContentNode.destroy();
  30. }
  31. this.node.empty();
  32. delete this;
  33. },
  34. loadContent : function( filterData ){
  35. this.elementContentNode.empty();
  36. //this.view = new MWF.xApplication.Attendance.AddressExplorer.View(this.elementContentNode, this.app,this, this.viewData, this.options.searchKey );
  37. //this.view.filterData = filterData;
  38. //this.view.load();
  39. this.actions.listWorkplace( function(json){
  40. json.data = json.data || [];
  41. this.wpContent = this.toolbarNode.getElements("[name=wpContent]")[0];
  42. this.createList( json.data );
  43. this.baiduMap = new MWF.xApplication.Attendance.AddressExplorer.BaiduMap( this.elementContentNode, this.app, this, {} );
  44. this.baiduMap.load( json.data );
  45. this.setContentSize();
  46. }.bind(this));
  47. },
  48. reloadList: function(){
  49. this.actions.listWorkplace( function(json){
  50. this.wpContent.empty();
  51. this.createList( json.data || [] );
  52. }.bind(this))
  53. },
  54. createList: function( data ){
  55. this.wdList = new Element("div", {
  56. styles : this.css.wdList
  57. }).inject( this.wpContent );
  58. this.wdList.setStyle( "width" , this.toolbarNode.getSize().x - 200 + "px" );
  59. data.each( function( d ){
  60. var placeItem = new Element( "div", {
  61. styles : this.css.toolbarContentItem,
  62. text : d.placeName
  63. }).inject( this.wdList );
  64. placeItem.addEvent( "click" , function(e){
  65. this.obj.baiduMap.gotoMarker( this.data );
  66. e.stopPropagation();
  67. }.bind({ obj : this, data : d }) )
  68. }.bind(this) );
  69. this.arrow = "up";
  70. if( this.wdList.getScrollSize().y > this.wpContent.getSize().y ){
  71. this.wdList.addEvent("click",function(e){
  72. if( this.arrow != "down" ){
  73. this.openList( e );
  74. }else{
  75. this.closeList( e )
  76. }
  77. }.bind(this));
  78. this.arrowNode = new Element("div.arrowNode",{
  79. "styles" : this.css.arrowNode
  80. }).inject( this.wpContent, "top" );
  81. this.arrowNode.addEvents({
  82. "mouseover" : function(){
  83. this.arrowNode.setStyles( this.categoryArrow != "down" ? this.css.arrowNode_over : this.css.arrowNode_down_over);
  84. }.bind(this),
  85. "mouseout" : function(){
  86. this.arrowNode.setStyles( this.categoryArrow != "down" ? this.css.arrowNode : this.css.arrowNode_down);
  87. }.bind(this),
  88. "click" : function( e ){
  89. if( this.arrow != "down" ){
  90. this.openList( e );
  91. }else{
  92. this.closeList( e )
  93. }
  94. }.bind(this)
  95. });
  96. }
  97. },
  98. _setContentSize: function(){
  99. this.wdList.setStyle( "width" , this.toolbarNode.getSize().x - 200 + "px" );
  100. },
  101. openList : function( e ){
  102. this.arrow = "down";
  103. //this.arrowNode.setStyles(this.css.arrowNode_down_over );
  104. this.arrowNode.setStyle("display","none");
  105. this.wdList.setStyles(this.css.wdList_all);
  106. window.closeList = this.closeList.bind(this);
  107. this.app.content.addEvent("click", window.closeList );
  108. e.stopPropagation();
  109. },
  110. closeList : function( e ){
  111. this.arrow = "up";
  112. //this.arrowNode.setStyles(this.css.arrowNode );
  113. this.arrowNode.setStyle("display","");
  114. this.wdList.setStyles(this.css.wdList);
  115. this.app.content.removeEvent("click" , window.closeList );
  116. e.stopPropagation();
  117. },
  118. createDocument: function(){
  119. this.baiduMap.createMarker();
  120. }
  121. });
  122. MWF.xApplication.Attendance.AddressExplorer.BaiduMap = new Class({
  123. Implements: [Options, Events],
  124. options: {
  125. "style": "default"
  126. },
  127. initialize: function (container, app, explorer, options) {
  128. this.container = container;
  129. this.explorer = explorer;
  130. this.app = app;
  131. this.actions = explorer.actions;
  132. this.setOptions(options);
  133. this.markers = {};
  134. this.markerInfoWindows = {};
  135. },
  136. load : function( markerData ){
  137. this.markerData = markerData;
  138. this.mapNode = new Element("div", {styles : {
  139. width : "100%",
  140. height : "99%"
  141. }}).inject(this.container);
  142. setTimeout( function(){
  143. this.loadResource( );
  144. }.bind(this) , 200 )
  145. },
  146. loadResource: function (callback) {
  147. window.BMap_loadScriptTime = (new Date).getTime();
  148. //var apiPath = "http://api.map.baidu.com/api?v=2.0&ak=Qac4WmBvHXiC87z3HjtRrbotCE3sC9Zg";
  149. var apiPath;
  150. if( window.location.protocol.toLowerCase() === "https" ){
  151. apiPath = "https://api.map.baidu.com/getscript?v=2.0&ak=Qac4WmBvHXiC87z3HjtRrbotCE3sC9Zg&services=&t=20161219171637";
  152. }else{
  153. apiPath = "http://api.map.baidu.com/getscript?v=2.0&ak=Qac4WmBvHXiC87z3HjtRrbotCE3sC9Zg&services=&t=20161219171637";
  154. }
  155. if( !window.BDMapApiLoaded ){
  156. COMMON.AjaxModule.loadDom(apiPath, function () {
  157. window.BDMapApiLoaded = true;
  158. if( !window.BDMarkerToolLoaded ){
  159. COMMON.AjaxModule.load( "/x_component_Attendance/BDMarkerTool.js", function(){
  160. window.BDMarkerToolLoaded = true;
  161. this._loadMap();
  162. if (callback)callback();
  163. }.bind(this) );
  164. }else{
  165. this._loadMap();
  166. if (callback)callback();
  167. }
  168. }.bind(this));
  169. }else{
  170. this._loadMap();
  171. if (callback)callback();
  172. }
  173. },
  174. _loadMap: function(){
  175. if (navigator.geolocation){
  176. try{
  177. navigator.geolocation.getCurrentPosition(this.loadMap.bind(this), this.loadMap.bind(this));
  178. }catch( e ){
  179. this.loadMap();
  180. }
  181. }else{
  182. this.loadMap();
  183. }
  184. },
  185. loadMap: function(position){
  186. this.createMap( position );
  187. this.addMapControl();
  188. if( this.markerData ){
  189. this.addMarkerArray( this.markerData );
  190. }
  191. },
  192. createMap: function( position ){
  193. var point = null;
  194. if (position && position.coords){
  195. point = new BMap.Point(position.coords.longitude, position.coords.latitude);
  196. }
  197. if( !point ){
  198. if( this.markerData && this.markerData.length > 0){
  199. var json = this.markerData[0];
  200. point = new BMap.Point(json.longitude, json.latitude);
  201. }else{
  202. point = new BMap.Point(116.404, 39.915);
  203. }
  204. }
  205. var map = this.map = new BMap.Map(this.mapNode); // 创建Map实例
  206. map.centerAndZoom(point, 12); // 初始化地图,设置中心点坐标和地图级别
  207. // map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); // 初始化地图,设置中心点坐标和地图级别
  208. // map.setCurrentCity("北京"); // 设置地图显示的城市 此项是必须设置的
  209. map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
  210. },
  211. addMapControl: function(){
  212. //向地图中添加缩放控件
  213. var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_RIGHT,type:BMAP_NAVIGATION_CONTROL_LARGE});
  214. this.map.addControl(ctrl_nav);
  215. //向地图中添加缩略图控件
  216. var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:1});
  217. this.map.addControl(ctrl_ove);
  218. //向地图中添加比例尺控件
  219. var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
  220. this.map.addControl(ctrl_sca);
  221. //map.addControl(new this.mapWindow.BMap.MapTypeControl()); //添加地图类型控件
  222. this.addCityListControl();
  223. },
  224. addCityListControl : function(){
  225. this.map.addControl(new BMap.CityListControl({
  226. anchor: BMAP_ANCHOR_TOP_LEFT,
  227. offset: new BMap.Size(10, 20),
  228. // 切换城市之间事件
  229. onChangeBefore: function( ){
  230. },
  231. // 切换城市之后事件
  232. onChangeAfter:function( ){
  233. }
  234. }));
  235. },
  236. getInfoWindowHtml: function( json ){
  237. json = json || {};
  238. //拼接infowindow内容字串
  239. var html = [];
  240. html.push('<br/>');
  241. html.push('<table border="0" cellpadding="1" cellspacing="1" id="markerTable" docId="'+ (json.id || "") +'" style="font-size:12px;">');
  242. html.push(' <tr>');
  243. html.push(' <td style="width:50px" align="left" class="common">名称:</td>');
  244. html.push(' <td style="width: 300px"><input type="text" size="40" value="'+(json.placeName || "") +'" id="placeName" ></td>');
  245. html.push(' <td style="width: 10px" valign="top"><span style="color:#ff0000">*</span></td>');
  246. html.push(' </tr>');
  247. html.push(' <tr>');
  248. html.push(' <td align="left" class="common">别名:</td>');
  249. html.push(' <td><input type="text" maxlength="300px" size="40" value="'+(json.placeAlias || "") +'" id="placeAlias" ></td>');
  250. html.push(' <td valign="top"></td>');
  251. html.push(' </tr>');
  252. html.push(' <tr>');
  253. html.push(' <td align="left" class="common">范围:</td>');
  254. html.push(' <td><input type="text" maxlength="300px" size="40" value="'+(json.errorRange || "") +'" id="errorRange" ></td>');
  255. html.push(' <td valign="top">米</td>');
  256. html.push(' </tr>');
  257. html.push(' <tr>');
  258. html.push(' <td align="left" class="common">备注:</td>');
  259. html.push(' <td><textarea rows="4" cols="31" id="description">'+ (json.description || "") +'</textarea></td>');
  260. html.push(' <td valign="top"></td>');
  261. html.push(' </tr>');
  262. html.push(' <tr>');
  263. html.push(' <td align="center" colspan="3">');
  264. html.push(' <input type="button" name="btnOK" id="submitPlace" value="保存">&nbsp;&nbsp;');
  265. if( json.id ){
  266. html.push(' <input type="button" name="btnMove" id="enableMovePlace" value="允许移动">&nbsp;&nbsp;');
  267. }
  268. html.push(' <input type="button" name="btnClear" id="cancelPlace" value="删除">');
  269. html.push(' </td>');
  270. html.push(' </tr>');
  271. html.push('</table>');
  272. return html.join("");
  273. },
  274. createMarker: function(){
  275. var _self = this;
  276. var mkrTool = new BMapLib.MarkerTool(this.map, {autoClose: true});
  277. mkrTool.addEventListener("markend", function(evt){
  278. var infoWin = new BMap.InfoWindow(this.getInfoWindowHtml(), {offset: new BMap.Size(0, -10)});
  279. infoWin.addEventListener("open",function(){
  280. var table = document.id("markerTable");
  281. var submitPlace = table.getElements("[id='submitPlace']");
  282. submitPlace.addEvent("click", function(){
  283. this.obj.ok( this.mkr , this.table );
  284. }.bind({ obj : this, mkr : mkr, table : table }));
  285. var cancelPlace = table.getElements("[id='cancelPlace']");
  286. cancelPlace.addEvent("click", function(){
  287. this.obj.cancel( this.mkr, this.table );
  288. }.bind({ obj : this, mkr : mkr, table : table }));
  289. }.bind( _self ));
  290. var mkr = evt.marker;
  291. mkr.addEventListener("click",function(){
  292. this.openInfoWindow(infoWin);
  293. });
  294. mkr.addEventListener("dragend",function(){
  295. this.openInfoWindow(infoWin);
  296. });
  297. mkr.openInfoWindow(infoWin);
  298. }.bind(this));
  299. mkrTool.open(); //打开工具
  300. var icon = BMapLib.MarkerTool.SYS_ICONS[14]; //设置工具样式,使用系统提供的样式BMapLib.MarkerTool.SYS_ICONS[0] -- BMapLib.MarkerTool.SYS_ICONS[23]
  301. mkrTool.setIcon(icon);
  302. },
  303. //创建marker
  304. addMarkerArray : function ( markerArr ){
  305. for(var i=0;i<markerArr.length;i++){
  306. var json = markerArr[i];
  307. this.addMarker( json );
  308. }
  309. },
  310. addMarker : function( json ){
  311. var _self = this;
  312. var point = new BMap.Point(json.longitude, json.latitude);
  313. var iconImg = BMapLib.MarkerTool.SYS_ICONS[8];
  314. var marker = new BMap.Marker(point,{
  315. icon:iconImg,
  316. enableDragging : false
  317. });
  318. var label = new BMap.Label(json.placeName,{"offset":new BMap.Size(0,-20)});
  319. marker.setLabel(label);
  320. this.map.addOverlay(marker);
  321. label.setStyle({
  322. borderColor:"#808080",
  323. color:"#333",
  324. cursor:"pointer"
  325. });
  326. (function(){
  327. var _iw = new BMap.InfoWindow(this.getInfoWindowHtml( json ), {offset: new BMap.Size(0, -10)});
  328. var _marker = marker;
  329. var _json = json;
  330. _marker.addEventListener("click",function(){
  331. this.openInfoWindow(_iw);
  332. });
  333. _marker.addEventListener("dragend",function(){
  334. this.openInfoWindow(_iw);
  335. });
  336. _iw.addEventListener("open",function(){
  337. _marker.getLabel().hide();
  338. var table = document.id("markerTable");
  339. var enableMovePlace = table.getElements("[id='enableMovePlace']");
  340. enableMovePlace.addEvent("click", function(){
  341. this.obj.enableMove( this.mkr );
  342. }.bind({ obj : this, mkr : _marker, table : table, id : _json.id }));
  343. var submitPlace = table.getElements("[id='submitPlace']");
  344. submitPlace.addEvent("click", function(){
  345. this.obj.ok( this.mkr , this.table, this.id );
  346. }.bind({ obj : this, mkr : _marker, table : table, id : _json.id }));
  347. var cancelPlace = table.getElements("[id='cancelPlace']");
  348. cancelPlace.addEvent("click", function(){
  349. this.obj.cancel( this.mkr, this.table, this.id );
  350. }.bind({ obj : this, mkr : _marker, table : table, id : _json.id }))
  351. }.bind( _self ));
  352. _iw.addEventListener("close",function(){
  353. _marker.getLabel().show();
  354. });
  355. label.addEventListener("click",function(){
  356. _marker.openInfoWindow(_iw);
  357. });
  358. this.markers[ json.id ] = marker;
  359. this.markerInfoWindows[ json.id ] = _iw;
  360. }.bind(this))();
  361. },
  362. enableMove: function( mrk ){
  363. mrk.closeInfoWindow();
  364. mrk.enableDragging();
  365. },
  366. gotoMarker : function( json ){
  367. var marker = this.markers[ json.id ];
  368. this.map.centerAndZoom(marker.point, 15);
  369. marker.openInfoWindow( this.markerInfoWindows[ json.id ] );
  370. },
  371. ok: function( mkr, table, id ){
  372. //var id = table.get("docId");
  373. var placeName = table.getElements("[id='placeName']")[0].get("value");
  374. if( placeName.trim() == "" ){
  375. this.app.notice( "工作场所不能为空", "error" );
  376. return false;
  377. }
  378. var placeAlias = table.getElements("[id='placeAlias']")[0].get("value");
  379. var description = table.getElements("[id='description']")[0].get("value");
  380. var errorRange = table.getElements("[id='errorRange']")[0].get("value");
  381. var data = {
  382. placeName : placeName,
  383. placeAlias : placeAlias,
  384. errorRange : errorRange,
  385. description : description,
  386. longitude : mkr.point.lng,
  387. latitude : mkr.point.lat
  388. };
  389. if(id)data.id = id;
  390. this.actions.saveWorkplace( data, function( json ){
  391. data.id = json.data.message;
  392. mkr.closeInfoWindow();
  393. mkr.remove();
  394. this.addMarker( data );
  395. this.explorer.reloadList();
  396. }.bind(this) )
  397. },
  398. cancel: function( mkr, table, id ){
  399. if( id ){
  400. this.actions.deleteWorkplace( id, function(){
  401. mkr.closeInfoWindow();
  402. var label = mkr.getLabel();
  403. if( label )label.remove();
  404. mkr.remove();
  405. this.explorer.reloadList();
  406. }.bind(this) )
  407. }else{
  408. mkr.closeInfoWindow();
  409. var label = mkr.getLabel();
  410. if( label )label.remove();
  411. mkr.remove();
  412. }
  413. }
  414. });