PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/public/javascripts/app/home.js

https://bitbucket.org/ilab/share_gateway_dyrm
JavaScript | 6885 lines | 5412 code | 989 blank | 484 comment | 412 complexity | afa3634176114ef198ff0b14bf34698e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. GoogleMapApi = Service.GoogleMapApi = function() {
  2. this.map = null;
  3. this.bounds = new google.maps.LatLngBounds();
  4. this.infoWindow = new google.maps.InfoWindow();
  5. this.markers = [];
  6. this.markerCluster = null;
  7. };
  8. GoogleMapApi.DEFAULT_LAT = 12.565679;
  9. GoogleMapApi.DEFAULT_LNG = 104.990963
  10. GoogleMapApi.ZOOM_LEVEL = 7;
  11. GoogleMapApi.MAX_ZOOM_LEVEL = 14;
  12. GoogleMapApi.prototype.initializeMap = function(canvas) {
  13. var latlng = new google.maps.LatLng(GoogleMapApi.DEFAULT_LAT, GoogleMapApi.DEFAULT_LNG);
  14. var myOptions = {
  15. zoom: GoogleMapApi.ZOOM_LEVEL,
  16. center: latlng,
  17. mapTypeId: google.maps.MapTypeId.ROADMAP
  18. };
  19. this.map = new google.maps.Map(canvas, myOptions);
  20. var mcOptions = {gridSize: 60, maxZoom:14, zoomOnClick:true };
  21. this.markerCluster = new MarkerClusterer(this.map,this.markers,mcOptions); };
  22. GoogleMapApi.prototype.createMarker = function(optionData){
  23. var latLng = this.newLatLng(optionData.lat, optionData.lng);
  24. var option = optionData;
  25. option.position = latLng;
  26. this._calculateBounds(latLng);
  27. marker = new google.maps.Marker(option);
  28. this.markerCluster.addMarker(marker);
  29. return marker;
  30. };
  31. GoogleMapApi.prototype.refresh = function(){
  32. this.markerclusterer.redraw();
  33. };
  34. GoogleMapApi.prototype.createDraggableMarker = function(title, lat, lng, icon){
  35. var latLng = this.newLatLng(lat, lng);
  36. var option = {
  37. position: latLng,
  38. title: title,
  39. map: this.map,
  40. icon:icon,
  41. draggable:true
  42. };
  43. marker = new google.maps.Marker(option);
  44. return marker;
  45. };
  46. GoogleMapApi.prototype._calculateBounds = function(latLng){
  47. this.bounds.extend(latLng);
  48. };
  49. GoogleMapApi.prototype.fitBounds = function(){
  50. this.map.fitBounds(this.bounds);
  51. var zoomLevel = this.map.getZoom();
  52. if(zoomLevel > GoogleMapApi.MAX_ZOOM_LEVEL){
  53. this.map.setZoom(GoogleMapApi.MAX_ZOOM_LEVEL);
  54. this.bounds = this.map.getBounds();
  55. }
  56. };
  57. GoogleMapApi.prototype.newLatLng = function(lat, lng){
  58. return new google.maps.LatLng(lat, lng);
  59. };
  60. GoogleMapApi.prototype.setMarkerPosition = function(marker, latLng) {
  61. marker.setPosition(latLng);
  62. };
  63. GoogleMapApi.prototype.addMarkerClickListener = function(marker, data, handler){
  64. google.maps.event.addListener(marker, 'click', function() {
  65. handler(data);
  66. });
  67. };
  68. GoogleMapApi.prototype.addMapClickListener = function(e){
  69. google.maps.event.addListener(this.map, 'click', function(point) {
  70. e.click(point.latLng.lat(), point.latLng.lng());
  71. });
  72. };
  73. GoogleMapApi.prototype.moveMarkerTo = function(marker, lat, lng){
  74. marker.setMap(this.map);
  75. marker.setPosition(this.newLatLng(lat, lng));
  76. return marker;
  77. };
  78. GoogleMapApi.prototype.removeMarker = function(marker){
  79. marker.setMap(null);
  80. };
  81. GoogleMapApi.prototype.setIcon = function(marker, icon){
  82. if(icon)
  83. marker.setIcon(icon);
  84. };
  85. GoogleMapApi.prototype.setShadow = function(marker, imageUrl){
  86. var shadow = new google.maps.MarkerImage(imageUrl, null, null, new google.maps.Point(16, 32));
  87. marker.setShadow(shadow);
  88. };
  89. GoogleMapApi.prototype.addListener = function(obj, eventName, handler){
  90. google.maps.event.addListener(obj, eventName, handler);
  91. };
  92. GoogleMapApi.prototype.addMarkerClickListener = function(marker, data, handler){
  93. google.maps.event.addListener(marker, 'click', function(){
  94. handler(data);
  95. });
  96. };
  97. GoogleMapApi.prototype.setMarkerDraggability = function(marker,draggability){
  98. marker.setDraggable(draggability);
  99. };
  100. GoogleMapApi.prototype.getAllMarkers = function(){
  101. return this.markers;
  102. };
  103. GoogleMapApi.prototype.clearAllMarkers = function(){
  104. this.markerCluster.clearMarkers();
  105. };
  106. GoogleMapApi.prototype.attachDataToMarker = function(marker,key,value){
  107. if(!marker.data)
  108. marker.data={};
  109. marker.data[key]=value;
  110. };
  111. GoogleMapApi.prototype.processMarkerVisibility = function(){
  112. this.markerCluster.processMarkersVisibility(true);
  113. };
  114. GoogleMapApi.prototype.updateMarkerClusterer = function(resource,redraw){
  115. var markers = this.markerCluster.getMarkers();
  116. var tempMarker = null;
  117. for(var i=0; i< markers.length; i++){
  118. if(markers[i].id == resource.id ){
  119. var latLng = this.newLatLng(resource.lat, resource.lng);
  120. markers[i].setPosition(latLng);
  121. if(redraw)
  122. this.markerCluster.processMarkersVisibility(true);
  123. return true;
  124. }
  125. }
  126. return false;
  127. };
  128. GoogleMapApi.prototype.getCenter = function(){
  129. return this.map.getCenter();
  130. };
  131. GoogleMapApi.prototype.setCenter = function(latLng){
  132. return this.map.panTo(latLng);
  133. };
  134. GoogleMapApi.prototype.setZoom = function(level){
  135. return this.map.setZoom(level);
  136. };
  137. GoogleMapApi.prototype.getMap = function(){
  138. return this.map;
  139. };
  140. /**
  141. * @name MarkerClusterer for Google Maps v3
  142. * @version version 1.0
  143. * @author Luke Mahe
  144. * @fileoverview
  145. * The library creates and manages per-zoom-level clusters for large amounts of
  146. * markers.
  147. * <br/>
  148. * This is a v3 implementation of the
  149. * <a href="http://gmaps-utility-library-dev.googlecode.com/svn/tags/markerclusterer/"
  150. * >v2 MarkerClusterer</a>.
  151. */
  152. /**
  153. * Licensed under the Apache License, Version 2.0 (the "License");
  154. * you may not use this file except in compliance with the License.
  155. * You may obtain a copy of the License at
  156. *
  157. * http://www.apache.org/licenses/LICENSE-2.0
  158. *
  159. * Unless required by applicable law or agreed to in writing, software
  160. * distributed under the License is distributed on an "AS IS" BASIS,
  161. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  162. * See the License for the specific language governing permissions and
  163. * limitations under the License.
  164. */
  165. /**
  166. * A Marker Clusterer that clusters markers.
  167. *
  168. * @param {google.maps.Map} map The Google map to attach to.
  169. * @param {Array.<google.maps.Marker>} opt_markers Optional markers to add to
  170. * the cluster.
  171. * @param {Object} opt_options support the following options:
  172. * 'gridSize': (number) The grid size of a cluster in pixels.
  173. * 'maxZoom': (number) The maximum zoom level that a marker can be part of a
  174. * cluster.
  175. * 'zoomOnClick': (boolean) Whether the default behaviour of clicking on a
  176. * cluster is to zoom into it.
  177. * 'averageCenter': (boolean) Wether the center of each cluster should be
  178. * the average of all markers in the cluster.
  179. * 'styles': (object) An object that has style properties:
  180. * 'url': (string) The image url.
  181. * 'height': (number) The image height.
  182. * 'width': (number) The image width.
  183. * 'anchor': (Array) The anchor position of the label text.
  184. * 'textColor': (string) The text color.
  185. * 'textSize': (number) The text size.
  186. * @constructor
  187. * @extends google.maps.OverlayView
  188. */
  189. function MarkerClusterer(map, opt_markers, opt_options) {
  190. this.extend(MarkerClusterer, google.maps.OverlayView);
  191. this.map_ = map;
  192. /**
  193. * @type {Array.<google.maps.Marker>}
  194. * @private
  195. */
  196. this.markers_ = [];
  197. /**
  198. * @type {Array.<Cluster>}
  199. */
  200. this.clusters_ = [];
  201. this.sizes = [53, 56, 66, 78, 90];
  202. /**
  203. * @private
  204. */
  205. this.styles_ = [];
  206. /**
  207. * @type {boolean}
  208. * @private
  209. */
  210. this.ready_ = false;
  211. var options = opt_options || {};
  212. /**
  213. * @type {number}
  214. * @private
  215. */
  216. this.gridSize_ = options['gridSize'] || 60;
  217. /**
  218. * @type {?number}
  219. * @private
  220. */
  221. this.maxZoom_ = options['maxZoom'] || 20;
  222. this.styles_ = options['styles'] || [];
  223. /**
  224. * @type {string}
  225. * @private
  226. */
  227. this.imagePath_ = options['imagePath'] ||
  228. this.MARKER_CLUSTER_IMAGE_PATH_;
  229. /**
  230. * @type {string}
  231. * @private
  232. */
  233. this.imageExtension_ = options['imageExtension'] ||
  234. this.MARKER_CLUSTER_IMAGE_EXTENSION_;
  235. /**
  236. * @type {boolean}
  237. * @private
  238. */
  239. this.zoomOnClick_ = true;
  240. if (options['zoomOnClick'] != undefined) {
  241. this.zoomOnClick_ = options['zoomOnClick'];
  242. }
  243. /**
  244. * @type {boolean}
  245. * @private
  246. */
  247. this.averageCenter_ = false;
  248. if (options['averageCenter'] != undefined) {
  249. this.averageCenter_ = options['averageCenter'];
  250. }
  251. this.setupStyles_();
  252. this.setMap(map);
  253. /**
  254. * @type {number}
  255. * @private
  256. */
  257. this.prevZoom_ = this.map_.getZoom();
  258. var that = this;
  259. google.maps.event.addListener(this.map_, 'zoom_changed', function() {
  260. var maxZoom = that.map_.mapTypes[that.map_.getMapTypeId()].maxZoom;
  261. var zoom = that.map_.getZoom();
  262. if (zoom < 0 || zoom > maxZoom) {
  263. return;
  264. }
  265. if (that.prevZoom_ != zoom) {
  266. that.prevZoom_ = that.map_.getZoom();
  267. if(zoom > that.maxZoom_)
  268. that.resetViewport(); //Ilab modification default behavior
  269. else
  270. that.processMarkersVisibility();
  271. }
  272. });
  273. google.maps.event.addListener(this.map_, 'idle', function() {
  274. that.redraw();
  275. });
  276. if (opt_markers && opt_markers.length) {
  277. this.addMarkers(opt_markers, false);
  278. }
  279. }
  280. /**
  281. * The marker cluster image path.
  282. *
  283. * @type {string}
  284. * @private
  285. */
  286. MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_PATH_ = SETTING.WEBROOT + 'images/clusterer/m';
  287. /**
  288. * The marker cluster image path.
  289. *
  290. * @type {string}
  291. * @private
  292. */
  293. MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_EXTENSION_ = 'png';
  294. /**
  295. * Extends a objects prototype by anothers.
  296. *
  297. * @param {Object} obj1 The object to be extended.
  298. * @param {Object} obj2 The object to extend with.
  299. * @return {Object} The new extended object.
  300. * @ignore
  301. */
  302. MarkerClusterer.prototype.extend = function(obj1, obj2) {
  303. return (function(object) {
  304. for (property in object.prototype) {
  305. this.prototype[property] = object.prototype[property];
  306. }
  307. return this;
  308. }).apply(obj1, [obj2]);
  309. };
  310. /**
  311. * Implementaion of the interface method.
  312. * @ignore
  313. */
  314. MarkerClusterer.prototype.onAdd = function() {
  315. this.setReady_(true);
  316. };
  317. /**
  318. * Implementaion of the interface method.
  319. * @ignore
  320. */
  321. MarkerClusterer.prototype.draw = function() {};
  322. /**
  323. * Sets up the styles object.
  324. *
  325. * @private
  326. */
  327. MarkerClusterer.prototype.setupStyles_ = function() {
  328. if (this.styles_.length) {
  329. return;
  330. }
  331. for (var i = 0, size; size = this.sizes[i]; i++) {
  332. this.styles_.push({
  333. url: this.imagePath_ + (i + 1) + '.' + this.imageExtension_,
  334. height: size,
  335. width: size
  336. });
  337. }
  338. };
  339. /**
  340. * Sets the styles.
  341. *
  342. * @param {Object} styles The style to set.
  343. */
  344. MarkerClusterer.prototype.setStyles = function(styles) {
  345. this.styles_ = styles;
  346. };
  347. /**
  348. * Gets the styles.
  349. *
  350. * @return {Object} The styles object.
  351. */
  352. MarkerClusterer.prototype.getStyles = function() {
  353. return this.styles_;
  354. };
  355. /**
  356. * Whether zoom on click is set.
  357. *
  358. * @return {boolean} True if zoomOnClick_ is set.
  359. */
  360. MarkerClusterer.prototype.isZoomOnClick = function() {
  361. return this.zoomOnClick_;
  362. };
  363. /**
  364. * Whether average center is set.
  365. *
  366. * @return {boolean} True if averageCenter_ is set.
  367. */
  368. MarkerClusterer.prototype.isAverageCenter = function() {
  369. return this.averageCenter_;
  370. };
  371. /**
  372. * Returns the array of markers in the clusterer.
  373. *
  374. * @return {Array.<google.maps.Marker>} The markers.
  375. */
  376. MarkerClusterer.prototype.getMarkers = function() {
  377. return this.markers_;
  378. };
  379. /**
  380. * Returns the array of markers in the clusterer.
  381. *
  382. * @return {Array.<google.maps.Marker>} The number of markers.
  383. */
  384. MarkerClusterer.prototype.getTotalMarkers = function() {
  385. return this.markers_;
  386. };
  387. MarkerClusterer.prototype.processMarkersVisibility = function(redraw) {
  388. var markers = this.getMarkers(); // 10 marker.isAdd ==
  389. this.resetViewport();
  390. var data = this.getData(); // data = {1:false,2:false}
  391. if(data){
  392. for(var i=0;i<markers.length;i++){
  393. for(var layer_id in data){
  394. var visible = data[layer_id];
  395. if(markers[i].layer_id == layer_id && visible==false){
  396. markers[i].isAdded = true;
  397. }
  398. }
  399. }
  400. }
  401. if(redraw)
  402. this.redraw();
  403. };
  404. MarkerClusterer.prototype.setData = function(data) {
  405. this.data = data;
  406. };
  407. MarkerClusterer.prototype.getData = function() {
  408. var data = Util.Cookies.getAsObject("activeLayers");
  409. var interestedLayers = {};
  410. for(var layer_id in data){
  411. if(data[layer_id]==false)
  412. interestedLayers[layer_id] = false;
  413. }
  414. return interestedLayers;
  415. };
  416. /**
  417. * Sets the max zoom for the clusterer.
  418. *
  419. * @param {number} maxZoom The max zoom level.
  420. */
  421. MarkerClusterer.prototype.setMaxZoom = function(maxZoom) {
  422. this.maxZoom_ = maxZoom;
  423. };
  424. /**
  425. * Gets the max zoom for the clusterer.
  426. *
  427. * @return {number} The max zoom level.
  428. */
  429. MarkerClusterer.prototype.getMaxZoom = function() {
  430. return this.maxZoom_ || this.map_.mapTypes[this.map_.getMapTypeId()].maxZoom;
  431. };
  432. /**
  433. * The function for calculating the cluster icon image.
  434. *
  435. * @param {Array.<google.maps.Marker>} markers The markers in the clusterer.
  436. * @param {number} numStyles The number of styles available.
  437. * @return {Object} A object properties: 'text' (string) and 'index' (number).
  438. * @private
  439. */
  440. MarkerClusterer.prototype.calculator_ = function(markers, numStyles) {
  441. var index = 0;
  442. var count = markers.length;
  443. var dv = count;
  444. while (dv !== 0) {
  445. dv = parseInt(dv / 10, 10);
  446. index++;
  447. }
  448. index = Math.min(index, numStyles);
  449. return {
  450. text: count,
  451. index: index
  452. };
  453. };
  454. /**
  455. * Set the calculator function.
  456. *
  457. * @param {function(Array, number)} calculator The function to set as the
  458. * calculator. The function should return a object properties:
  459. * 'text' (string) and 'index' (number).
  460. *
  461. */
  462. MarkerClusterer.prototype.setCalculator = function(calculator) {
  463. this.calculator_ = calculator;
  464. };
  465. /**
  466. * Get the calculator function.
  467. *
  468. * @return {function(Array, number)} the calculator function.
  469. */
  470. MarkerClusterer.prototype.getCalculator = function() {
  471. return this.calculator_;
  472. };
  473. /**
  474. * Add an array of markers to the clusterer.
  475. *
  476. * @param {Array.<google.maps.Marker>} markers The markers to add.
  477. * @param {boolean} opt_nodraw Whether to redraw the clusters.
  478. */
  479. MarkerClusterer.prototype.addMarkers = function(markers, opt_nodraw) {
  480. for (var i = 0, marker; marker = markers[i]; i++) {
  481. this.pushMarkerTo_(marker);
  482. }
  483. if (!opt_nodraw) {
  484. this.redraw();
  485. }
  486. };
  487. /**
  488. * Pushes a marker to the clusterer.
  489. *
  490. * @param {google.maps.Marker} marker The marker to add.
  491. * @private
  492. */
  493. MarkerClusterer.prototype.pushMarkerTo_ = function(marker) {
  494. marker.setVisible(false);
  495. marker.setMap(null);
  496. var data = Util.Cookies.getAsObject("activeLayers");
  497. marker.isAdded = false;
  498. for(var layer_id in data){
  499. var visible = data[layer_id];
  500. if(marker.layer_id == layer_id && visible==false){
  501. marker.isAdded = true;
  502. }
  503. }
  504. if (marker['draggable']) {
  505. var that = this;
  506. google.maps.event.addListener(marker, 'dragend', function() {
  507. marker.isAdded = false;
  508. that.resetViewport();
  509. that.redraw();
  510. });
  511. }
  512. this.markers_.push(marker);
  513. };
  514. /**
  515. * Adds a marker to the clusterer and redraws if needed.
  516. *
  517. * @param {google.maps.Marker} marker The marker to add.
  518. * @param {boolean} opt_nodraw Whether to redraw the clusters.
  519. */
  520. MarkerClusterer.prototype.addMarker = function(marker, opt_nodraw) {
  521. this.pushMarkerTo_(marker);
  522. if (!opt_nodraw) {
  523. this.redraw();
  524. }
  525. };
  526. /**
  527. * Remove a marker from the cluster.
  528. *
  529. * @param {google.maps.Marker} marker The marker to remove.
  530. * @return {boolean} True if the marker was removed.
  531. */
  532. MarkerClusterer.prototype.removeMarker = function(marker) {
  533. var index = -1;
  534. if (this.markers_.indexOf) {
  535. index = this.markers_.indexOf(marker);
  536. } else {
  537. for (var i = 0, m; m = this.markers_[i]; i++) {
  538. if (m == marker) {
  539. index = i;
  540. continue;
  541. }
  542. }
  543. }
  544. if (index == -1) {
  545. return false;
  546. }
  547. this.markers_.splice(index, 1);
  548. marker.setVisible(false);
  549. marker.setMap(null);
  550. this.resetViewport();
  551. this.redraw();
  552. return true;
  553. };
  554. /**
  555. * Sets the clusterer's ready state.
  556. *
  557. * @param {boolean} ready The state.
  558. * @private
  559. */
  560. MarkerClusterer.prototype.setReady_ = function(ready) {
  561. if (!this.ready_) {
  562. this.ready_ = ready;
  563. this.createClusters_();
  564. }
  565. };
  566. /**
  567. * Returns the number of clusters in the clusterer.
  568. *
  569. * @return {number} The number of clusters.
  570. */
  571. MarkerClusterer.prototype.getTotalClusters = function() {
  572. return this.clusters_.length;
  573. };
  574. /**
  575. * Returns the google map that the clusterer is associated with.
  576. *
  577. * @return {google.maps.Map} The map.
  578. */
  579. MarkerClusterer.prototype.getMap = function() {
  580. return this.map_;
  581. };
  582. /**
  583. * Sets the google map that the clusterer is associated with.
  584. *
  585. * @param {google.maps.Map} map The map.
  586. */
  587. MarkerClusterer.prototype.setMap = function(map) {
  588. this.map_ = map;
  589. };
  590. /**
  591. * Returns the size of the grid.
  592. *
  593. * @return {number} The grid size.
  594. */
  595. MarkerClusterer.prototype.getGridSize = function() {
  596. /*
  597. var tran = 50;
  598. var Ax = this.maxZoom_;
  599. var By = this.gridSize_- tran ;
  600. var x = (this.map_.getZoom()>this.maxZoom)?this.maxZoom:this.map_.getZoom();
  601. var distance = tran + By-(By/Ax)*x ;
  602. return distance;
  603. */
  604. return this.gridSize_;
  605. };
  606. /**
  607. * Returns the size of the grid.
  608. *
  609. * @param {number} size The grid size.
  610. */
  611. MarkerClusterer.prototype.setGridSize = function(size) {
  612. this.gridSize_ = size;
  613. };
  614. /**
  615. * Extends a bounds object by the grid size.
  616. *
  617. * @param {google.maps.LatLngBounds} bounds The bounds to extend.
  618. * @return {google.maps.LatLngBounds} The extended bounds.
  619. */
  620. MarkerClusterer.prototype.getExtendedBounds = function(bounds) {
  621. var projection = this.getProjection();
  622. var tr = new google.maps.LatLng(bounds.getNorthEast().lat(),
  623. bounds.getNorthEast().lng());
  624. var bl = new google.maps.LatLng(bounds.getSouthWest().lat(),
  625. bounds.getSouthWest().lng());
  626. var trPix = projection.fromLatLngToDivPixel(tr);
  627. trPix.x += this.gridSize_;
  628. trPix.y -= this.gridSize_;
  629. var blPix = projection.fromLatLngToDivPixel(bl);
  630. blPix.x -= this.gridSize_;
  631. blPix.y += this.gridSize_;
  632. var ne = projection.fromDivPixelToLatLng(trPix);
  633. var sw = projection.fromDivPixelToLatLng(blPix);
  634. bounds.extend(ne);
  635. bounds.extend(sw);
  636. return bounds;
  637. };
  638. /**
  639. * Determins if a marker is contained in a bounds.
  640. *
  641. * @param {google.maps.Marker} marker The marker to check.
  642. * @param {google.maps.LatLngBounds} bounds The bounds to check against.
  643. * @return {boolean} True if the marker is in the bounds.
  644. * @private
  645. */
  646. MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) {
  647. return bounds.contains(marker.getPosition());
  648. };
  649. /**
  650. * Clears all clusters and markers from the clusterer.
  651. */
  652. MarkerClusterer.prototype.clearMarkers = function() {
  653. this.resetViewport();
  654. this.markers_ = [];
  655. };
  656. /**
  657. * Clears all existing clusters and recreates them.
  658. */
  659. MarkerClusterer.prototype.resetViewport = function() {
  660. for (var i = 0, cluster; cluster = this.clusters_[i]; i++) {
  661. cluster.remove();
  662. }
  663. for (var i = 0, marker; marker = this.markers_[i]; i++) {
  664. marker.isAdded = false;
  665. marker.setMap(null);
  666. marker.setVisible(false);
  667. }
  668. this.clusters_ = [];
  669. };
  670. /**
  671. * Redraws the clusters.
  672. */
  673. MarkerClusterer.prototype.redraw = function() {
  674. this.createClusters_();
  675. };
  676. /**
  677. * Creates the clusters.
  678. *
  679. * @private
  680. */
  681. MarkerClusterer.prototype.createClusters_ = function() {
  682. if (!this.ready_) {
  683. return;
  684. }
  685. var mapBounds = new google.maps.LatLngBounds(this.map_.getBounds().getSouthWest(),
  686. this.map_.getBounds().getNorthEast());
  687. var bounds = this.getExtendedBounds(mapBounds);
  688. for (var i = 0, marker; marker = this.markers_[i]; i++) {
  689. var added = false;
  690. if (!marker.isAdded && this.isMarkerInBounds_(marker, bounds)) {
  691. for (var j = 0, cluster; cluster = this.clusters_[j]; j++) {
  692. if (!added && cluster.getCenter() &&
  693. cluster.isMarkerInClusterBounds(marker)) {
  694. added = true;
  695. cluster.addMarker(marker);
  696. break;
  697. }
  698. }
  699. if (!added) {
  700. var cluster = new Cluster(this);
  701. cluster.addMarker(marker);
  702. this.clusters_.push(cluster);
  703. }
  704. }
  705. }
  706. };
  707. /**
  708. * A cluster that contains markers.
  709. *
  710. * @param {MarkerClusterer} markerClusterer The markerclusterer that this
  711. * cluster is associated with.
  712. * @constructor
  713. * @ignore
  714. */
  715. function Cluster(markerClusterer) {
  716. this.markerClusterer_ = markerClusterer;
  717. this.map_ = markerClusterer.getMap();
  718. this.gridSize_ = markerClusterer.getGridSize();
  719. this.averageCenter_ = markerClusterer.isAverageCenter();
  720. this.center_ = null;
  721. this.markers_ = [];
  722. this.bounds_ = null;
  723. this.clusterIcon_ = new ClusterIcon(this, markerClusterer.getStyles(),
  724. markerClusterer.getGridSize());
  725. }
  726. /**
  727. * Determins if a marker is already added to the cluster.
  728. *
  729. * @param {google.maps.Marker} marker The marker to check.
  730. * @return {boolean} True if the marker is already added.
  731. */
  732. Cluster.prototype.isMarkerAlreadyAdded = function(marker) {
  733. if (this.markers_.indexOf) {
  734. return this.markers_.indexOf(marker) != -1;
  735. } else {
  736. for (var i = 0, m; m = this.markers_[i]; i++) {
  737. if (m == marker) {
  738. return true;
  739. }
  740. }
  741. }
  742. return false;
  743. };
  744. /**
  745. * Add a marker the cluster.
  746. *
  747. * @param {google.maps.Marker} marker The marker to add.
  748. * @return {boolean} True if the marker was added.
  749. */
  750. Cluster.prototype.addMarker = function(marker) {
  751. if (this.isMarkerAlreadyAdded(marker)) {
  752. return false;
  753. }
  754. if (!this.center_) {
  755. this.center_ = marker.getPosition();
  756. this.calculateBounds_();
  757. } else {
  758. if (this.averageCenter_) {
  759. var lat = (this.center_.lat() + marker.getPosition().lat()) / 2;
  760. var lng = (this.center_.lng() + marker.getPosition().lng()) / 2;
  761. this.center_ = new google.maps.LatLng(lat, lng);
  762. this.calculateBounds_();
  763. }
  764. }
  765. if (this.markers_.length == 0) {
  766. marker.setMap(this.map_);
  767. marker.setVisible(true);
  768. } else if (this.markers_.length == 1) {
  769. this.markers_[0].setMap(null);
  770. this.markers_[0].setVisible(false);
  771. }
  772. marker.isAdded = true;
  773. this.markers_.push(marker);
  774. this.updateIcon();
  775. return true;
  776. };
  777. /**
  778. * Returns the marker clusterer that the cluster is associated with.
  779. *
  780. * @return {MarkerClusterer} The associated marker clusterer.
  781. */
  782. Cluster.prototype.getMarkerClusterer = function() {
  783. return this.markerClusterer_;
  784. };
  785. /**
  786. * Returns the bounds of the cluster.
  787. *
  788. * @return {google.maps.LatLngBounds} the cluster bounds.
  789. */
  790. Cluster.prototype.getBounds = function() {
  791. this.calculateBounds_();
  792. return this.bounds_;
  793. };
  794. /**
  795. * Removes the cluster
  796. */
  797. Cluster.prototype.remove = function() {
  798. this.clusterIcon_.remove();
  799. this.markers_.length = 0;
  800. delete this.markers_;
  801. };
  802. /**
  803. * Returns the center of the cluster.
  804. *
  805. * @return {number} The cluster center.
  806. */
  807. Cluster.prototype.getSize = function() {
  808. return this.markers_.length;
  809. };
  810. /**
  811. * Returns the center of the cluster.
  812. *
  813. * @return {Array.<google.maps.Marker>} The cluster center.
  814. */
  815. Cluster.prototype.getMarkers = function() {
  816. return this.markers_;
  817. };
  818. /**
  819. * Returns the center of the cluster.
  820. *
  821. * @return {google.maps.LatLng} The cluster center.
  822. */
  823. Cluster.prototype.getCenter = function() {
  824. return this.center_;
  825. };
  826. /**
  827. * Calculated the bounds of the cluster with the grid.
  828. *
  829. * @private
  830. */
  831. Cluster.prototype.calculateBounds_ = function() {
  832. var bounds = new google.maps.LatLngBounds(this.center_, this.center_);
  833. this.bounds_ = this.markerClusterer_.getExtendedBounds(bounds);
  834. };
  835. /**
  836. * Determines if a marker lies in the clusters bounds.
  837. *
  838. * @param {google.maps.Marker} marker The marker to check.
  839. * @return {boolean} True if the marker lies in the bounds.
  840. */
  841. Cluster.prototype.isMarkerInClusterBounds = function(marker) {
  842. return this.bounds_.contains(marker.getPosition());
  843. };
  844. /**
  845. * Returns the map that the cluster is associated with.
  846. *
  847. * @return {google.maps.Map} The map.
  848. */
  849. Cluster.prototype.getMap = function() {
  850. return this.map_;
  851. };
  852. /**
  853. * Updates the cluster icon
  854. */
  855. Cluster.prototype.updateIcon = function() {
  856. var zoom = this.map_.getZoom();
  857. var mz = this.markerClusterer_.getMaxZoom();
  858. if (zoom > mz) {
  859. for (var i = 0, marker; marker = this.markers_[i]; i++) {
  860. marker.setMap(this.map_);
  861. marker.setVisible(true);
  862. }
  863. return;
  864. }
  865. if (this.markers_.length < 2) {
  866. this.clusterIcon_.hide();
  867. return;
  868. }
  869. var numStyles = this.markerClusterer_.getStyles().length;
  870. var sums = this.markerClusterer_.getCalculator()(this.markers_, numStyles);
  871. this.clusterIcon_.setCenter(this.center_);
  872. this.clusterIcon_.setSums(sums);
  873. this.clusterIcon_.show();
  874. };
  875. /**
  876. * A cluster icon
  877. *
  878. * @param {Cluster} cluster The cluster to be associated with.
  879. * @param {Object} styles An object that has style properties:
  880. * 'url': (string) The image url.
  881. * 'height': (number) The image height.
  882. * 'width': (number) The image width.
  883. * 'anchor': (Array) The anchor position of the label text.
  884. * 'textColor': (string) The text color.
  885. * 'textSize': (number) The text size.
  886. * @param {number} opt_padding Optional padding to apply to the cluster icon.
  887. * @constructor
  888. * @extends google.maps.OverlayView
  889. * @ignore
  890. */
  891. function ClusterIcon(cluster, styles, opt_padding) {
  892. cluster.getMarkerClusterer().extend(ClusterIcon, google.maps.OverlayView);
  893. this.styles_ = styles;
  894. this.padding_ = opt_padding || 0;
  895. this.cluster_ = cluster;
  896. this.center_ = null;
  897. this.map_ = cluster.getMap();
  898. this.div_ = null;
  899. this.sums_ = null;
  900. this.visible_ = false;
  901. this.setMap(this.map_);
  902. }
  903. /**
  904. * Triggers the clusterclick event and zoom's if the option is set.
  905. */
  906. ClusterIcon.prototype.triggerClusterClick = function() {
  907. var markerClusterer = this.cluster_.getMarkerClusterer();
  908. google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_);
  909. if (markerClusterer.isZoomOnClick()) {
  910. this.map_.panTo(this.cluster_.getCenter());
  911. this.map_.fitBounds(this.cluster_.getBounds());
  912. }
  913. };
  914. /**
  915. * Adding the cluster icon to the dom.
  916. * @ignore
  917. */
  918. ClusterIcon.prototype.onAdd = function() {
  919. this.div_ = document.createElement('DIV');
  920. if (this.visible_) {
  921. var pos = this.getPosFromLatLng_(this.center_);
  922. this.div_.style.cssText = this.createCss(pos);
  923. this.div_.innerHTML = this.sums_.text;
  924. }
  925. var panes = this.getPanes();
  926. panes.overlayImage.appendChild(this.div_);
  927. var that = this;
  928. google.maps.event.addDomListener(this.div_, 'click', function() {
  929. that.triggerClusterClick();
  930. });
  931. };
  932. /**
  933. * Returns the position to place the div dending on the latlng.
  934. *
  935. * @param {google.maps.LatLng} latlng The position in latlng.
  936. * @return {google.maps.Point} The position in pixels.
  937. * @private
  938. */
  939. ClusterIcon.prototype.getPosFromLatLng_ = function(latlng) {
  940. var pos = this.getProjection().fromLatLngToDivPixel(latlng);
  941. pos.x -= parseInt(this.width_ / 2, 10);
  942. pos.y -= parseInt(this.height_ / 2, 10);
  943. return pos;
  944. };
  945. /**
  946. * Draw the icon.
  947. * @ignore
  948. */
  949. ClusterIcon.prototype.draw = function() {
  950. if (this.visible_) {
  951. var pos = this.getPosFromLatLng_(this.center_);
  952. this.div_.style.top = pos.y + 'px';
  953. this.div_.style.left = pos.x + 'px';
  954. }
  955. };
  956. /**
  957. * Hide the icon.
  958. */
  959. ClusterIcon.prototype.hide = function() {
  960. if (this.div_) {
  961. this.div_.style.display = 'none';
  962. }
  963. this.visible_ = false;
  964. };
  965. /**
  966. * Position and show the icon.
  967. */
  968. ClusterIcon.prototype.show = function() {
  969. if (this.div_) {
  970. var pos = this.getPosFromLatLng_(this.center_);
  971. this.div_.style.cssText = this.createCss(pos);
  972. this.div_.style.display = '';
  973. }
  974. this.visible_ = true;
  975. };
  976. /**
  977. * Remove the icon from the map
  978. */
  979. ClusterIcon.prototype.remove = function() {
  980. this.setMap(null);
  981. };
  982. /**
  983. * Implementation of the onRemove interface.
  984. * @ignore
  985. */
  986. ClusterIcon.prototype.onRemove = function() {
  987. if (this.div_ && this.div_.parentNode) {
  988. this.hide();
  989. this.div_.parentNode.removeChild(this.div_);
  990. this.div_ = null;
  991. }
  992. };
  993. /**
  994. * Set the sums of the icon.
  995. *
  996. * @param {Object} sums The sums containing:
  997. * 'text': (string) The text to display in the icon.
  998. * 'index': (number) The style index of the icon.
  999. */
  1000. ClusterIcon.prototype.setSums = function(sums) {
  1001. this.sums_ = sums;
  1002. this.text_ = sums.text;
  1003. this.index_ = sums.index;
  1004. if (this.div_) {
  1005. this.div_.innerHTML = sums.text;
  1006. }
  1007. this.useStyle();
  1008. };
  1009. /**
  1010. * Sets the icon to the the styles.
  1011. */
  1012. ClusterIcon.prototype.useStyle = function() {
  1013. var index = Math.max(0, this.sums_.index - 1);
  1014. index = Math.min(this.styles_.length - 1, index);
  1015. var style = this.styles_[index];
  1016. this.url_ = style['url'];
  1017. this.height_ = style['height'];
  1018. this.width_ = style['width'];
  1019. this.textColor_ = style['textColor'];
  1020. this.anchor = style['anchor'];
  1021. this.textSize_ = style['textSize'];
  1022. };
  1023. /**
  1024. * Sets the center of the icon.
  1025. *
  1026. * @param {google.maps.LatLng} center The latlng to set as the center.
  1027. */
  1028. ClusterIcon.prototype.setCenter = function(center) {
  1029. this.center_ = center;
  1030. };
  1031. /**
  1032. * Create the css text based on the position of the icon.
  1033. *
  1034. * @param {google.maps.Point} pos The position.
  1035. * @return {string} The css style text.
  1036. */
  1037. ClusterIcon.prototype.createCss = function(pos) {
  1038. var style = [];
  1039. if (document.all) {
  1040. style.push('filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(' +
  1041. 'sizingMethod=scale,src="' + this.url_ + '");');
  1042. } else {
  1043. style.push('background:url(' + this.url_ + ');');
  1044. }
  1045. if (typeof this.anchor_ === 'object') {
  1046. if (typeof this.anchor_[0] === 'number' && this.anchor_[0] > 0 &&
  1047. this.anchor_[0] < this.height_) {
  1048. style.push('height:' + (this.height_ - this.anchor_[0]) +
  1049. 'px; padding-top:' + this.anchor_[0] + 'px;');
  1050. } else {
  1051. style.push('height:' + this.height_ + 'px; line-height:' + this.height_ +
  1052. 'px;');
  1053. }
  1054. if (typeof this.anchor_[1] === 'number' && this.anchor_[1] > 0 &&
  1055. this.anchor_[1] < this.width_) {
  1056. style.push('width:' + (this.width_ - this.anchor_[1]) +
  1057. 'px; padding-left:' + this.anchor_[1] + 'px;');
  1058. } else {
  1059. style.push('width:' + this.width_ + 'px; text-align:center;');
  1060. }
  1061. } else {
  1062. style.push('height:' + this.height_ + 'px; line-height:' +
  1063. this.height_ + 'px; width:' + this.width_ + 'px; text-align:center;');
  1064. }
  1065. var txtColor = this.textColor_ ? this.textColor_ : 'black';
  1066. var txtSize = this.textSize_ ? this.textSize_ : 11;
  1067. style.push('cursor:pointer; top:' + pos.y + 'px; left:' +
  1068. pos.x + 'px; color:' + txtColor + '; position:absolute; font-size:' +
  1069. txtSize + 'px; font-family:Arial,sans-serif; font-weight:bold');
  1070. return style.join('');
  1071. };
  1072. window['MarkerClusterer'] = MarkerClusterer;
  1073. MarkerClusterer.prototype['addMarker'] = MarkerClusterer.prototype.addMarker;
  1074. MarkerClusterer.prototype['addMarkers'] = MarkerClusterer.prototype.addMarkers;
  1075. MarkerClusterer.prototype['clearMarkers'] =
  1076. MarkerClusterer.prototype.clearMarkers;
  1077. MarkerClusterer.prototype['getCalculator'] =
  1078. MarkerClusterer.prototype.getCalculator;
  1079. MarkerClusterer.prototype['getGridSize'] =
  1080. MarkerClusterer.prototype.getGridSize;
  1081. MarkerClusterer.prototype['getMap'] = MarkerClusterer.prototype.getMap;
  1082. MarkerClusterer.prototype['getMarkers'] = MarkerClusterer.prototype.getMarkers;
  1083. MarkerClusterer.prototype['getMaxZoom'] = MarkerClusterer.prototype.getMaxZoom;
  1084. MarkerClusterer.prototype['getStyles'] = MarkerClusterer.prototype.getStyles;
  1085. MarkerClusterer.prototype['getTotalClusters'] =
  1086. MarkerClusterer.prototype.getTotalClusters;
  1087. MarkerClusterer.prototype['getTotalMarkers'] =
  1088. MarkerClusterer.prototype.getTotalMarkers;
  1089. MarkerClusterer.prototype['redraw'] = MarkerClusterer.prototype.redraw;
  1090. MarkerClusterer.prototype['removeMarker'] =
  1091. MarkerClusterer.prototype.removeMarker;
  1092. MarkerClusterer.prototype['resetViewport'] =
  1093. MarkerClusterer.prototype.resetViewport;
  1094. MarkerClusterer.prototype['setCalculator'] =
  1095. MarkerClusterer.prototype.setCalculator;
  1096. MarkerClusterer.prototype['setGridSize'] =
  1097. MarkerClusterer.prototype.setGridSize;
  1098. MarkerClusterer.prototype['onAdd'] = MarkerClusterer.prototype.onAdd;
  1099. MarkerClusterer.prototype['draw'] = MarkerClusterer.prototype.draw;
  1100. Cluster.prototype['getCenter'] = Cluster.prototype.getCenter;
  1101. Cluster.prototype['getSize'] = Cluster.prototype.getSize;
  1102. Cluster.prototype['getMarkers'] = Cluster.prototype.getMarkers;
  1103. ClusterIcon.prototype['onAdd'] = ClusterIcon.prototype.onAdd;
  1104. ClusterIcon.prototype['draw'] = ClusterIcon.prototype.draw;
  1105. ClusterIcon.prototype['onRemove'] = ClusterIcon.prototype.onRemove;
  1106. Model.Factory = {
  1107. models: {},
  1108. create: function(modelName){
  1109. if(!this.models[modelName]){
  1110. this.models[modelName] = new Model[modelName];
  1111. }
  1112. return this.models[modelName];
  1113. }
  1114. }
  1115. Model.LayerManager = function(){
  1116. var _self = this;
  1117. this.layers = [];
  1118. this.myLayers = [];
  1119. Helper.layerHelper = new Helper.LayerHelper(this);
  1120. this.getAllLayers = function(){
  1121. AjaxRequest.get("/layers", function(response) {
  1122. _self._getLayersEventHandler(response);
  1123. });
  1124. }
  1125. this.getLayers = function(user) {
  1126. AjaxRequest.get("/layers?user=" + user, function(response) {
  1127. _self._getLayersEventHandler(response);
  1128. });
  1129. }
  1130. this._getLayersEventHandler = function(response) {
  1131. if(response.status == 200){
  1132. var layersChanged = new Events.LayersEvent(Events.LayersEvent.LAYERS_CHANGED);
  1133. _self.layers = Json.decode(response.responseText);
  1134. GEvent.dispatchEvent(layersChanged);
  1135. }
  1136. }
  1137. this.createLayer = function(data){
  1138. AjaxRequest.post('/layers.json', data, function(response){
  1139. _self._createLayerEventHandler(response);
  1140. });
  1141. }
  1142. this._createLayerEventHandler = function(response){
  1143. if(response.status == 201){
  1144. GEvent.dispatchEvent(new Events.LayersEvent(Events.LayersEvent.LAYER_ADDED));
  1145. _self.layers.push(Json.decode(response.responseText));
  1146. GEvent.dispatchEvent(new Events.LayersEvent(Events.LayersEvent.LAYERS_CHANGED));
  1147. GEvent.dispatchEvent(new Events.UsersEvent(Events.UsersEvent.USERS_RELOAD));
  1148. }
  1149. }
  1150. this.loadMyLayer = function() {
  1151. var options = {
  1152. url:"/mylayers.json",
  1153. headers:{"auth_token":Authentication.getAuthToken()},
  1154. method:'get',
  1155. success:_self._getMyLayersEventHandler
  1156. };
  1157. Util.Ajax(options);
  1158. }
  1159. this._getMyLayersEventHandler = function(response){
  1160. var _self = this;
  1161. if (response.status == 200){
  1162. _self.myLayers = Json.decode(response.responseText);
  1163. GEvent.dispatchEvent(new Events.LayersEvent(Events.LayersEvent.MYLAYERS_CHANGED,_self.myLayers));
  1164. }
  1165. }
  1166. this.createMyLayer = function(data){
  1167. var options = {
  1168. url:"/layers.json",
  1169. method:'post',
  1170. params:data,
  1171. success:_self._createMyLayerEventHandler
  1172. };
  1173. Util.Ajax(options);
  1174. }
  1175. this._createMyLayerEventHandler = function(response){
  1176. if(response.status == 201){
  1177. var layer = Json.decode(response.responseText);
  1178. Util.func.setCurrentUserAsLayerOwner(layer);
  1179. _self.layers.push(layer);
  1180. GEvent.dispatchEvent(new Events.LayersEvent(Events.LayersEvent.MY_LAYER_ADDED,layer));
  1181. }
  1182. else{
  1183. var errors = Json.decode(response.responseText);
  1184. GEvent.dispatchEvent(new Events.LayersEvent(Events.LayersEvent.MY_LAYER_ADDED_FAILED, errors));
  1185. }
  1186. }
  1187. this.getPublicLayers = function(){
  1188. AjaxRequest.get('/public_layers', function(response){
  1189. _self._getPublicLayersEventHandler(response);
  1190. });
  1191. }
  1192. this._getPublicLayersEventHandler = function(response){
  1193. if(response.status == 200){
  1194. _self.layers = Json.decode(response.responseText);
  1195. var layersChanged = new Events.LayersEvent(Events.LayersEvent.LAYERS_CHANGED);
  1196. GEvent.dispatchEvent(layersChanged);
  1197. }
  1198. }
  1199. this.updateMyLayerStatus = function(data){
  1200. var _self = this;
  1201. var options = {
  1202. url:"/update_my_layer_status.json" ,
  1203. method:'post',
  1204. params: data,
  1205. headers:{"Content-Type":"application/json; charset=utf-8"},
  1206. success:_self._updateMyLayerStatusHandler
  1207. };
  1208. Util.Ajax(options);
  1209. };
  1210. this._updateMyLayerStatusHandler = function(response){
  1211. if(response.status == 200){
  1212. var datas = Json.decode(response.responseText);
  1213. var layersChanged = new Events.LayersEvent(Events.LayersEvent.LAYERS_MY_LAYER_STATUS_SAVED);
  1214. GEvent.dispatchEvent(layersChanged);
  1215. }
  1216. else{
  1217. var datas = Json.decode(response.responseText);
  1218. var layersChanged = new Events.LayersEvent(Events.LayersEvent.LAYERS_MY_LAYER_STATUS_SAVED_FAILED,datas);
  1219. GEvent.dispatchEvent(layersChanged);
  1220. }
  1221. };
  1222. this.findById = function(id){
  1223. for(var i = 0; i < this.layers.length; i++){
  1224. if(this.layers[i].id == id){
  1225. return this.layers[i];
  1226. }
  1227. }
  1228. return null;
  1229. };
  1230. this.saveMember = function(data){
  1231. var _self = this;
  1232. data = Json.encode(data);
  1233. var options = {
  1234. url:"/create_membership.json",
  1235. method:'post',
  1236. params: data,
  1237. headers:{"Content-Type":"application/json; charset=utf-8"},
  1238. success:_self._saveMemberHandler
  1239. };
  1240. Util.Ajax(options);
  1241. };
  1242. this._saveMemberHandler = function(response){
  1243. var data = Json.decode(response.responseText);
  1244. if(response.status == 201) {
  1245. var memberSaved = new Events.MemberEvent(Events.MemberEvent.MEMBER_CREATED,data);
  1246. GEvent.dispatchEvent(memberSaved);
  1247. }
  1248. else if (response.status == 200) {
  1249. var memberSavedFailed = new Events.MemberEvent(Events.MemberEvent.MEMBER_CREATED_FAILED,data);
  1250. GEvent.dispatchEvent(memberSavedFailed);
  1251. }
  1252. };
  1253. this.editMember = function(data){
  1254. var _self = this;
  1255. params = Json.encode(data);
  1256. var options = {
  1257. url: "/getMembership?user_id=" + data.userId + "&layer_id=" + data.layerId ,
  1258. method:'get',
  1259. params: data,
  1260. headers:{"Content-Type":"application/json; charset=utf-8"},
  1261. success:_self._editMemberHandler
  1262. };
  1263. Util.Ajax(options);
  1264. };
  1265. this._editMemberHandler = function(response){
  1266. var data = Json.decode(response.responseText);
  1267. if(response.status == 200) {
  1268. var getMemberSuccess= new Events.MemberEvent(Events.MemberEvent.GET_MEMBER_SUCCESS,data);
  1269. GEvent.dispatchEvent(getMemberSuccess);
  1270. }
  1271. else if (response.status == 404) {
  1272. alert('could not find this member');
  1273. }
  1274. };
  1275. this.updateMember = function(data){
  1276. var _self = this;
  1277. params = Json.encode(data);
  1278. var options = {
  1279. url: "/memberships/" + data.id + ".json" ,
  1280. method:'put',
  1281. params: params,
  1282. headers:{"Content-Type":"application/json; charset=utf-8"},
  1283. success:_self._updateMemberHandler
  1284. };
  1285. Util.Ajax(options);
  1286. };
  1287. this._updateMemberHandler = function(response){
  1288. var data = Json.decode(response.responseText);
  1289. if(response.status == 201) {
  1290. var memberGetUpdated = new Events.MemberEvent(Events.MemberEvent.MEMBER_GET_UPDATED,data);
  1291. GEvent.dispatchEvent(memberGetUpdated);
  1292. }
  1293. else if (response.status == 200) {
  1294. var memberGetUpdatedFailed = new Events.MemberEvent(Events.MemberEvent.MEMBER_GET_UPDATED_FAILED,data);
  1295. GEvent.dispatchEvent(memberGetUpdatedFailed);
  1296. }
  1297. };
  1298. }
  1299. Model.ResourceManager = function(){
  1300. var _self = this;
  1301. this.resources = [];
  1302. this.currentResource = null
  1303. this.getResourceDetailForResourceList = function(id){
  1304. AjaxRequest.get("/resources/" + id, function(response){
  1305. _self._getResourceDetailForResourceList(response);
  1306. })
  1307. };
  1308. this._getResourceDetailForResourceList = function(response){
  1309. var resouceDetailEvent = null;
  1310. if(response.status == 200){
  1311. var resource = Json.decode(response.responseText);
  1312. resouceDetailEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_GET_DETAIL_FOR_RESOURCE_LIST, resource);
  1313. }
  1314. else{
  1315. resouceDetailEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_GET_DETAIL_FOR_RESOURCE_LIST_FAILED);
  1316. }
  1317. GEvent.dispatchEvent(resouceDetailEvent);
  1318. };
  1319. this.loadResources = function(){
  1320. AjaxRequest.get("/resources", function(response){
  1321. _self._loadResourcesEventHandler(response);
  1322. });
  1323. };
  1324. this.loadResourcesOf = function(queryString){
  1325. GEvent.dispatchEvent(new Events.MapsEvent(Events.MapsEvent.LOADING_WINDOW_SHOW));
  1326. AjaxRequest.get("/resources?" +queryString, function(response){
  1327. _self._loadResourcesEventHandler(response);
  1328. });
  1329. };
  1330. this._loadResourcesEventHandler = function(response){
  1331. if(response.status == 200){
  1332. _self.resources = Json.decode(response.responseText);
  1333. GEvent.dispatchEvent(new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCES_CHANGED));
  1334. }
  1335. GEvent.dispatchEvent(new Events.MapsEvent(Events.MapsEvent.LOADING_WINDOW_CLOSE));
  1336. };
  1337. this.createResource = function(data){
  1338. var param = Json.encode({resource: data});
  1339. var options = {
  1340. url:"/resources",
  1341. method:'post',
  1342. params: param,
  1343. headers:{"Content-Type":"application/json; charset=utf-8"},
  1344. success:_self._createResourceEventHandler
  1345. };
  1346. Util.Ajax(options);
  1347. };
  1348. this._createResourceEventHandler = function(response){
  1349. var resouceAddedEvent = null;
  1350. if(response.status == 200){
  1351. var resource = Json.decode(response.responseText);
  1352. resouceAddedEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_ADDED, resource);
  1353. _self.resources.push(Json.decode(response.responseText));
  1354. }
  1355. else{
  1356. resouceAddedEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_ADDED, false);
  1357. }
  1358. GEvent.dispatchEvent(resouceAddedEvent);
  1359. };
  1360. this.getResourceDetail = function(id){
  1361. AjaxRequest.get("/resources/" + id, function(response){
  1362. _self._getResourceDetailEventHandler(response);
  1363. })
  1364. };
  1365. this._getResourceDetailEventHandler = function(response){
  1366. var resouceDetailEvent = null;
  1367. if(response.status == 200){
  1368. this.currentResource = Json.decode(response.responseText);
  1369. resouceDetailEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_GET_DETAIL, this.currentResource);
  1370. }
  1371. else{
  1372. resouceDetailEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_GET_DETAIL_FAILED);
  1373. }
  1374. GEvent.dispatchEvent(resouceDetailEvent);
  1375. };
  1376. this.loadResourcesPaging = function(data){
  1377. var options = {
  1378. url:"/layers/" + data.layerId + "/resources.json?page=" + data.page ,
  1379. method:'get',
  1380. headers:{"Content-Type":"application/json; charset=utf-8"},
  1381. success:_self._loadResourcesPaging
  1382. };
  1383. Util.Ajax(options);
  1384. };
  1385. this._loadResourcesPaging = function(response){
  1386. var resouceDetailEvent = null;
  1387. if(response.status == 200){
  1388. var resources = Json.decode(response.responseText);
  1389. resoucePaginglEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_GET_PAGING, resources);
  1390. }
  1391. else{
  1392. resoucePaginglEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_GET_PAGING_FAILED);
  1393. }
  1394. GEvent.dispatchEvent(resoucePaginglEvent);
  1395. };
  1396. this.getResourceTemplate = function(layerId){
  1397. var options = {
  1398. url:"/layers/" + data.layerId + "/property_templates.json",
  1399. method:'get',
  1400. headers:{"Content-Type":"application/json; charset=utf-8"},
  1401. success:_self._getResourceTemplateEventHandler
  1402. };
  1403. Util.Ajax(options);
  1404. };
  1405. this._getResourceTemplateEventHandler = function(response){
  1406. var resouceDetailEvent = null;
  1407. if(response.status == 200){
  1408. var template = Json.decode(response.responseText);
  1409. resouceDetailEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_GET_TEMPLATE, template);
  1410. }
  1411. else{
  1412. resouceDetailEvent = new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_GET_TEMPLATE_FAILED);
  1413. }
  1414. GEvent.dispatchEvent(resouceDetailEvent);
  1415. };
  1416. this.confirmImport = function(fileName, layerId){
  1417. GEvent.dispatchEvent(new Events.MapsEvent(Events.ResourcesEvent.LOADING_IMPORT_WINDOW_SHOW));
  1418. var options = {
  1419. url:"/import_from_csv",
  1420. params: {fileName: fileName, layerId: layerId},
  1421. method:'post',
  1422. success:_self._confirmImportEventHandler
  1423. };
  1424. Util.Ajax(options);
  1425. };
  1426. this._confirmImportEventHandler = function(response){
  1427. if (response.status == 200){
  1428. var resources = Json.decode(response.responseText);
  1429. for(var i = 0; i < resources.length; i++){
  1430. _self.addResource(resources[i]);
  1431. }
  1432. GEvent.dispatchEvent(new Events.MapsEvent(Events.ResourcesEvent.LOADING_IMPORT_WINDOW_CLOSE));
  1433. GEvent.dispatchEvent(new Events.ResourcesEvent(Events.ResourcesEvent.IMPORT_RESOURCES_FROM_CSV_SUCCESS));
  1434. }
  1435. };
  1436. this.addResource = function(resource){
  1437. this.resources.push(resource);
  1438. GEvent.dispatchEvent(new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_ITEM_ADDED, resource));
  1439. };
  1440. this.editResource = function(resourceId){
  1441. AjaxRequest.get("/resources/" +resourceId +"/edit", function(response){
  1442. _self._editResourceHandler(response);
  1443. });
  1444. };
  1445. this._editResourceHandler = function(response){
  1446. if(response.status == 200){
  1447. var resource = Json.decode(response.responseText);
  1448. GEvent.dispatchEvent(new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCE_EDIT_SUCCESS, resource));
  1449. }
  1450. };
  1451. this.updateResource = function(data){
  1452. window.data = data ;
  1453. var param = Json.encode({resource: data});
  1454. var options = {
  1455. url:"/resources/" + data.id ,
  1456. method:'put',
  1457. params: param,
  1458. headers:{"Content-Type":"application/json; charset=utf-8"},
  1459. success:_self._updateResourceEventHandler
  1460. };
  1461. Util.Ajax(options);
  1462. };
  1463. this._updateResourceEventHandler = function(response){
  1464. if(response.status == 200){
  1465. var resource = Json.decode(response.responseText);
  1466. _self._updateResourceInCollection(resource);
  1467. GEvent.dispatchEvent(new Events.ResourcesEvent(Events.ResourcesEvent.RESOURCES_UPDATE_SUCCESS, resource));
  1468. }
  1469. };
  1470. this._updateResourceInCollection = function(resource){
  1471. var resources = _self.resources ;
  1472. for(var i=0;i<resources.length;i++){
  1473. if(resources[i].id == resource.id){
  1474. resources[i].name = resource.name;
  1475. resources[i].lat = resource.lat;
  1476. resources[i].lng = resource.lng;
  1477. return true;
  1478. }
  1479. }
  1480. return false;
  1481. };
  1482. };
  1483. Model.AuthenticationManager = function(){
  1484. var _self = this;
  1485. this.session = {};
  1486. this.login = function(user){
  1487. AjaxRequest.post('/login_rpc', user, function(response){
  1488. _self._loginEventHandler(response);
  1489. });
  1490. };
  1491. this._loginEventHandler = function(response){
  1492. if(response.status == 200){
  1493. _self.session = Json.decode(response.responseText);
  1494. var loginSuccess = new Events.AuthenticationEvent(Events.AuthenticationEvent.LOGIN_SUCCESS);
  1495. GEvent.dispatchEvent(loginSuccess);
  1496. }
  1497. else{
  1498. var loginFailed = new Events.AuthenticationEvent(Events.AuthenticationEvent.LOGIN_FAILED);
  1499. GEvent.dispatchEvent(loginFailed);
  1500. }
  1501. };
  1502. this.logout = function(){
  1503. AjaxRequest.post('/logout_rpc', 'logout', function(response){
  1504. _self._logoutEventHandler(response);
  1505. });
  1506. },
  1507. this._logoutEventHandler = function(response){
  1508. if(response.status == 200){
  1509. var logoutSuccess = new Events.AuthenticationEvent(Events.AuthenticationEvent.LOGOUT_SUCCESS);
  1510. GEvent.dispatchEvent(logoutSuccess);
  1511. }
  1512. }
  1513. }
  1514. Model.UploadManager = function(){
  1515. var _self = this;
  1516. this.getSWFUploadObject = function(){
  1517. return window.SETTING.swfUpload;
  1518. };
  1519. this.startUploadIcon = function(swf){
  1520. swf.startUpload(swf.QueueFile.id);
  1521. };
  1522. this.startImportResources = function(swf){
  1523. swf.startUpload(swf.QueueFile.id);
  1524. }
  1525. };
  1526. Helper.LayerHelper = function(layerManager){
  1527. this.layerManager = layerManager;
  1528. this.getIcon = function(layerId){
  1529. var layer = this.layerManager.findById(layerId);
  1530. return layer && layer.icon? layer.icon : SETTING.MARKER_ICON_DEFAULT;
  1531. };
  1532. this.getLayer = function(layerId){
  1533. return this.layerManager.findById(layerId);
  1534. };
  1535. }
  1536. Events.LayersEvent = function(type, data){
  1537. this.type = type;
  1538. this.data = data;
  1539. }
  1540. Events.LayersEvent.LAYERS_CHANGED = 'layersChangedLayersEvent';
  1541. Events.LayersEvent.LAYER_ADDED = 'layerAddedLayersEvent';
  1542. Events.LayersEvent.LAYER_NEW = 'layerNewLayersEvent';
  1543. Events.LayersEvent.LAYERS_LOAD = 'layersLoadLayersEvent';
  1544. Events.LayersEvent.MYLAYERS_SHOW = 'layersShowMyLayerEvent';
  1545. Events.LayersEvent.MYLAYER_LOAD = 'layersLoadMyLayerEvent';
  1546. Events.LayersEvent.MYLAYERS_CHANGED = 'layersChangeMyLayerEvent';
  1547. Events.LayersEvent.MYLAYERS_HIDE = 'layersHideMyLayerEvent';
  1548. Events.LayersEvent.MYLAYERS_SHOW = 'layersShowMyLayerEvent' ;
  1549. Events.LayersEvent.CREATE_MY_LAYER = 'layerCreateMyLayerEvent';
  1550. Events.LayersEvent.MY_LAYER_ADDED = 'layerMyLayerAddedEvent';
  1551. Events.LayersEvent.MY_LAYER_ADDED_FAILED = 'layerMyLayerAddedFailedEvent';
  1552. Events.LayersEvent.UPDATE_MY_LAYER_STATUS = 'layerUpdateMylayerEvent';
  1553. Events.LayersEvent.LAYERS_MY_LAYER_STATUS_SAVED = 'layerMyLayerStatusSaveEvent';
  1554. Events.LayersEvent.LAYERS_MY_LAYER_STATUS_SAVED_FAILED = "layerMyLayerStatusSavedFailedEvent";
  1555. Events.LayersEvent.LAYERS_UPDATE_RESOURCE_LOCATION_DRAGGING = "layerUpdateResourceLocationDraggingEvent";
  1556. Events.LayersEvent.LAYERS_LOAD_COMPLETED = "layersLoadCompletedLayerEvent";
  1557. Events.LayersEvent.SHOW_FRONT_LAYER_EVENT = "layersShowFrontLayerEvent";
  1558. Events.ResourcesEvent = function(type, data){
  1559. this.type = type;
  1560. this.data = data;
  1561. }
  1562. Events.ResourcesEvent.RESOURCES_CHANGED = 'resourcesChangedResourcesEvent';
  1563. Events.ResourcesEvent.RESOURCES_SAVE = 'resourcesSaveResourcesEvent';
  1564. Events.ResourcesEvent.RESOURCE_ADDED = "resourcesAddedResourceEvent";
  1565. Events.ResourcesEvent.RESOURCE_GET_DETAIL = 'resourceGetDetailEvent';
  1566. Events.ResourcesEvent.RESOURCE_GET_DETAIL_FAILED = 'resourceGetDetailFailedEvent';
  1567. Events.ResourcesEvent.RESOURCE_REQUEST_GET_DETAIL = 'resourceRequestsGetDetailEvent';
  1568. Events.ResourcesEvent.RESOURCE_LOAD = 'resourceLoadResourcesEvent';
  1569. Events.ResourcesEvent.RESOURCES_LOAD_PAGING = 'resourcesLoadPagingResourcesEvent';
  1570. Events.ResourcesEvent.RESOURCES_LOAD_TEMPLATE = 'resourcesLoadTemplateResourcesEvent';
  1571. Events.ResourcesEvent.RESOURCE_GET_TEMPLATE = 'resourcesGetTemplateResourcesEvent';
  1572. Events.ResourcesEvent.RESOURCE_GET_PAGING = "resourcesGetPagingResourcesEvent";
  1573. Events.ResourcesEvent.RESOURCE_GET_PAGING_FAILED = "resourcesGetPagingFailedResourcesEvent";
  1574. Events.ResourcesEvent.RESOURCE_LOAD_DETAIL_FOR_RESOURCE_LIST = "resourceLoadDetailForResourceList";
  1575. Events.ResourcesEvent.RESOURCE_GET_DETAIL_FOR_RESOURCE_LIST = "resourceGetDetailForResourceList";
  1576. Events.ResourcesEvent.RESOURCE_GET_DETAIL_FOR_RESOURCE_LIST_FAILED = "resourceGetDetailForResourceListFailed";
  1577. Events.ResourcesEvent.LOAD_LAYER_RESOURCES = "loadLayerResourcesResourcesEvent";
  1578. Events.ResourcesEvent.SHOW_IMPORT_RESOURCES = "showImportResourcesEvent";
  1579. Events.ResourcesEvent.LOAD_CONFIRMED_RESOURCES = "loadConfirmedResourcesEvent";

Large files files are truncated, but you can click here to view the full file