AddressExplorer.js 20 KB

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