AddressExplorer.js 20 KB

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