AddressExplorer.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 = "http://api.map.baidu.com/getscript?v=2.0&ak=Qac4WmBvHXiC87z3HjtRrbotCE3sC9Zg&services=&t=20161219171637";
  150. if( !window.BDMapApiLoaded ){
  151. COMMON.AjaxModule.loadDom(apiPath, function () {
  152. window.BDMapApiLoaded = true;
  153. if( !window.BDMarkerToolLoaded ){
  154. COMMON.AjaxModule.load( "/x_component_Attendance/BDMarkerTool.js", function(){
  155. window.BDMarkerToolLoaded = true;
  156. this._loadMap();
  157. if (callback)callback();
  158. }.bind(this) );
  159. }else{
  160. this._loadMap();
  161. if (callback)callback();
  162. }
  163. }.bind(this));
  164. }else{
  165. this._loadMap();
  166. if (callback)callback();
  167. }
  168. },
  169. _loadMap: function(){
  170. if (navigator.geolocation){
  171. try{
  172. navigator.geolocation.getCurrentPosition(this.loadMap.bind(this), this.loadMap.bind(this));
  173. }catch( e ){
  174. this.loadMap();
  175. }
  176. }else{
  177. this.loadMap();
  178. }
  179. },
  180. loadMap: function(position){
  181. this.createMap( position );
  182. this.addMapControl();
  183. if( this.markerData ){
  184. this.addMarkerArray( this.markerData );
  185. }
  186. },
  187. createMap: function( position ){
  188. var point = null;
  189. if (position && position.coords){
  190. point = new BMap.Point(position.coords.longitude, position.coords.latitude);
  191. }
  192. if( !point ){
  193. if( this.markerData && this.markerData.length > 0){
  194. var json = this.markerData[0];
  195. point = new BMap.Point(json.longitude, json.latitude);
  196. }else{
  197. point = new BMap.Point(116.404, 39.915);
  198. }
  199. }
  200. var map = this.map = new BMap.Map(this.mapNode); // 创建Map实例
  201. map.centerAndZoom(point, 12); // 初始化地图,设置中心点坐标和地图级别
  202. // map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); // 初始化地图,设置中心点坐标和地图级别
  203. // map.setCurrentCity("北京"); // 设置地图显示的城市 此项是必须设置的
  204. map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
  205. },
  206. addMapControl: function(){
  207. //向地图中添加缩放控件
  208. var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_RIGHT,type:BMAP_NAVIGATION_CONTROL_LARGE});
  209. this.map.addControl(ctrl_nav);
  210. //向地图中添加缩略图控件
  211. var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:1});
  212. this.map.addControl(ctrl_ove);
  213. //向地图中添加比例尺控件
  214. var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
  215. this.map.addControl(ctrl_sca);
  216. //map.addControl(new this.mapWindow.BMap.MapTypeControl()); //添加地图类型控件
  217. this.addCityListControl();
  218. },
  219. addCityListControl : function(){
  220. this.map.addControl(new BMap.CityListControl({
  221. anchor: BMAP_ANCHOR_TOP_LEFT,
  222. offset: new BMap.Size(10, 20),
  223. // 切换城市之间事件
  224. onChangeBefore: function( ){
  225. },
  226. // 切换城市之后事件
  227. onChangeAfter:function( ){
  228. }
  229. }));
  230. },
  231. getInfoWindowHtml: function( json ){
  232. json = json || {};
  233. //拼接infowindow内容字串
  234. var html = [];
  235. html.push('<br/>');
  236. html.push('<table border="0" cellpadding="1" cellspacing="1" id="markerTable" docId="'+ (json.id || "") +'" style="font-size:12px;">');
  237. html.push(' <tr>');
  238. html.push(' <td style="width:50px" align="left" class="common">名称:</td>');
  239. html.push(' <td style="width: 300px"><input type="text" size="40" value="'+(json.placeName || "") +'" id="placeName" ></td>');
  240. html.push(' <td style="width: 10px" valign="top"><span style="color:#ff0000">*</span></td>');
  241. html.push(' </tr>');
  242. html.push(' <tr>');
  243. html.push(' <td align="left" class="common">别名:</td>');
  244. html.push(' <td><input type="text" maxlength="300px" size="40" value="'+(json.placeAlias || "") +'" id="placeAlias" ></td>');
  245. html.push(' <td valign="top"></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.errorRange || "") +'" id="errorRange" ></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><textarea rows="4" cols="31" id="description">'+ (json.description || "") +'</textarea></td>');
  255. html.push(' <td valign="top"></td>');
  256. html.push(' </tr>');
  257. html.push(' <tr>');
  258. html.push(' <td align="center" colspan="3">');
  259. html.push(' <input type="button" name="btnOK" id="submitPlace" value="保存">&nbsp;&nbsp;');
  260. if( json.id ){
  261. html.push(' <input type="button" name="btnMove" id="enableMovePlace" value="允许移动">&nbsp;&nbsp;');
  262. }
  263. html.push(' <input type="button" name="btnClear" id="cancelPlace" value="删除">');
  264. html.push(' </td>');
  265. html.push(' </tr>');
  266. html.push('</table>');
  267. return html.join("");
  268. },
  269. createMarker: function(){
  270. var _self = this;
  271. var mkrTool = new BMapLib.MarkerTool(this.map, {autoClose: true});
  272. mkrTool.addEventListener("markend", function(evt){
  273. var infoWin = new BMap.InfoWindow(this.getInfoWindowHtml(), {offset: new BMap.Size(0, -10)});
  274. infoWin.addEventListener("open",function(){
  275. var table = document.id("markerTable");
  276. var submitPlace = table.getElements("[id='submitPlace']");
  277. submitPlace.addEvent("click", function(){
  278. this.obj.ok( this.mkr , this.table );
  279. }.bind({ obj : this, mkr : mkr, table : table }));
  280. var cancelPlace = table.getElements("[id='cancelPlace']");
  281. cancelPlace.addEvent("click", function(){
  282. this.obj.cancel( this.mkr, this.table );
  283. }.bind({ obj : this, mkr : mkr, table : table }));
  284. }.bind( _self ));
  285. var mkr = evt.marker;
  286. mkr.addEventListener("click",function(){
  287. this.openInfoWindow(infoWin);
  288. });
  289. mkr.addEventListener("dragend",function(){
  290. this.openInfoWindow(infoWin);
  291. });
  292. mkr.openInfoWindow(infoWin);
  293. }.bind(this));
  294. mkrTool.open(); //打开工具
  295. var icon = BMapLib.MarkerTool.SYS_ICONS[14]; //设置工具样式,使用系统提供的样式BMapLib.MarkerTool.SYS_ICONS[0] -- BMapLib.MarkerTool.SYS_ICONS[23]
  296. mkrTool.setIcon(icon);
  297. },
  298. //创建marker
  299. addMarkerArray : function ( markerArr ){
  300. for(var i=0;i<markerArr.length;i++){
  301. var json = markerArr[i];
  302. this.addMarker( json );
  303. }
  304. },
  305. addMarker : function( json ){
  306. var _self = this;
  307. var point = new BMap.Point(json.longitude, json.latitude);
  308. var iconImg = BMapLib.MarkerTool.SYS_ICONS[8];
  309. var marker = new BMap.Marker(point,{
  310. icon:iconImg,
  311. enableDragging : false
  312. });
  313. var label = new BMap.Label(json.placeName,{"offset":new BMap.Size(0,-20)});
  314. marker.setLabel(label);
  315. this.map.addOverlay(marker);
  316. label.setStyle({
  317. borderColor:"#808080",
  318. color:"#333",
  319. cursor:"pointer"
  320. });
  321. (function(){
  322. var _iw = new BMap.InfoWindow(this.getInfoWindowHtml( json ), {offset: new BMap.Size(0, -10)});
  323. var _marker = marker;
  324. var _json = json;
  325. _marker.addEventListener("click",function(){
  326. this.openInfoWindow(_iw);
  327. });
  328. _marker.addEventListener("dragend",function(){
  329. this.openInfoWindow(_iw);
  330. });
  331. _iw.addEventListener("open",function(){
  332. _marker.getLabel().hide();
  333. var table = document.id("markerTable");
  334. var enableMovePlace = table.getElements("[id='enableMovePlace']");
  335. enableMovePlace.addEvent("click", function(){
  336. this.obj.enableMove( this.mkr );
  337. }.bind({ obj : this, mkr : _marker, table : table, id : _json.id }));
  338. var submitPlace = table.getElements("[id='submitPlace']");
  339. submitPlace.addEvent("click", function(){
  340. this.obj.ok( this.mkr , this.table, this.id );
  341. }.bind({ obj : this, mkr : _marker, table : table, id : _json.id }));
  342. var cancelPlace = table.getElements("[id='cancelPlace']");
  343. cancelPlace.addEvent("click", function(){
  344. this.obj.cancel( this.mkr, this.table, this.id );
  345. }.bind({ obj : this, mkr : _marker, table : table, id : _json.id }))
  346. }.bind( _self ));
  347. _iw.addEventListener("close",function(){
  348. _marker.getLabel().show();
  349. });
  350. label.addEventListener("click",function(){
  351. _marker.openInfoWindow(_iw);
  352. });
  353. this.markers[ json.id ] = marker;
  354. this.markerInfoWindows[ json.id ] = _iw;
  355. }.bind(this))();
  356. },
  357. enableMove: function( mrk ){
  358. mrk.closeInfoWindow();
  359. mrk.enableDragging();
  360. },
  361. gotoMarker : function( json ){
  362. var marker = this.markers[ json.id ];
  363. this.map.centerAndZoom(marker.point, 15);
  364. marker.openInfoWindow( this.markerInfoWindows[ json.id ] );
  365. },
  366. ok: function( mkr, table, id ){
  367. //var id = table.get("docId");
  368. var placeName = table.getElements("[id='placeName']")[0].get("value");
  369. if( placeName.trim() == "" ){
  370. this.app.notice( "工作场所不能为空", "error" );
  371. return false;
  372. }
  373. var placeAlias = table.getElements("[id='placeAlias']")[0].get("value");
  374. var description = table.getElements("[id='description']")[0].get("value");
  375. var errorRange = table.getElements("[id='errorRange']")[0].get("value");
  376. var data = {
  377. placeName : placeName,
  378. placeAlias : placeAlias,
  379. errorRange : errorRange,
  380. description : description,
  381. longitude : mkr.point.lng,
  382. latitude : mkr.point.lat
  383. };
  384. if(id)data.id = id;
  385. this.actions.saveWorkplace( data, function( json ){
  386. data.id = json.data.message;
  387. mkr.closeInfoWindow();
  388. mkr.remove();
  389. this.addMarker( data );
  390. this.explorer.reloadList();
  391. }.bind(this) )
  392. },
  393. cancel: function( mkr, table, id ){
  394. if( id ){
  395. this.actions.deleteWorkplace( id, function(){
  396. mkr.closeInfoWindow();
  397. var label = mkr.getLabel();
  398. if( label )label.remove();
  399. mkr.remove();
  400. this.explorer.reloadList();
  401. }.bind(this) )
  402. }else{
  403. mkr.closeInfoWindow();
  404. var label = mkr.getLabel();
  405. if( label )label.remove();
  406. mkr.remove();
  407. }
  408. }
  409. });