]> dev.renevier.net Git - syj.git/blob - public/js/list.js
fe0eb62aa86e7d9005a065f49f0f88b286076c19
[syj.git] / public / js / list.js
1 var WGS84 = new OpenLayers.Projection("EPSG:4326");
2 var Mercator = new OpenLayers.Projection("EPSG:900913");
3
4
5 document.observe("dom:loaded", function() {
6     $("message").hide();
7
8     $$(".item").each(function(elt) {
9         new item(elt);
10     });
11 });
12
13 function item(elt) {
14     this.deleteHandler = elt.on('click', '.delete-link', this.remove.bindAsEventListener(this));
15     this.elt = elt;
16     this.map = this.createmap(elt.down('.map'));
17 }
18 item.prototype = {
19     createmap: function(elt) {
20         var geom = elt.getAttribute('data-geom'),
21             baseLayer = new OpenLayers.Layer.OSM("OSM"),
22             map = new OpenLayers.Map(elt, { controls: [], theme: null}),
23             layerOptions = {format:     OpenLayers.Format.WKT,
24                             projection: WGS84,
25                             styleMap:   new OpenLayers.StyleMap({
26                                             "default": new OpenLayers.Style({
27                                                 strokeColor: "blue",
28                                                 strokeWidth: 5,
29                                                 strokeOpacity: 0.7
30                                             })
31                                          })},
32             wkt = new OpenLayers.Format.WKT({ internalProjection: Mercator, externalProjection: WGS84 }),
33             viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions),
34             extent;
35
36         map.addLayers([baseLayer, viewLayer]);
37         viewLayer.addFeatures([wkt.read(geom)]);
38         map.zoomToExtent(viewLayer.getDataExtent());
39         return map;
40     },
41
42     deleteSuccess: function() {
43         this.deactivate();
44         $("message").setMessage(SyjStrings.deleteSuccess, "success");
45     },
46
47     deactivate: function() {
48         this.map.destroy();
49         this.elt.down('.title').update();
50         this.elt.down('.geom').update().setStyle({backgroundColor: 'gray'});
51         this.deleteHandler.stop();
52         this.elt.on('click', 'a', function(evt) { evt.stop(); });
53         this.elt.select('a').invoke('setStyle', {textDecoration: 'line-through'});
54     },
55
56     deleteFailure: function(transport) {
57         var httpCode = 0, message = "";
58         if (transport) {
59             httpCode = transport.getStatus();
60         }
61         switch (httpCode) {
62             case 0:
63                 message = SyjStrings.notReachedError;
64             break;
65             case 400:
66             case 403:
67                 location = loginlink();
68                 return;
69             break;
70             case 404:
71                  message = SyjStrings.requestError;
72             break;
73             case 410:
74                 this.deactivate();
75                 message = SyjStrings.gonePathError;
76             break;
77             case 500:
78                 message = SyjStrings.serverError;
79             break;
80             default:
81                 message = SyjStrings.unknownError;
82             break;
83         }
84         $("message").setMessage(message, "error");
85     },
86
87     remove: function(evt) {
88         evt.stop();
89         if (!confirm(SyjStrings.confirmDelete)) {
90             return;
91         }
92         var id = this.elt.getAttribute('data-id');
93
94         $("message").hide();
95         new Ajax.Request('path/' + id.toString() + '/delete', {
96             method: 'post',
97             onSuccess: this.deleteSuccess.bind(this),
98             onFailure: this.deleteFailure.bind(this)
99         });
100     }
101 };
102
103 function loginlink() {
104     var lang;
105     if (location.search && location.search.length && location.search[0] === '?') {
106         lang = location.search.slice(1).split('&').find(function(str) {
107             return str.startsWith('lang=');
108         });
109         if (lang) {
110             lang = lang.slice('lang='.length);
111         }
112     }
113     return 'login?redirect=' + encodeURIComponent(location.pathname + location.search) + ((lang) ? '&lang=' + lang: "");
114 }