AddressExplorer.js 20 KB

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