summaryrefslogtreecommitdiff
path: root/src/routeQuery.js
blob: 1e07f04832cff907ceb051719179c18bdd36b6f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * Copyright (c) 2013 Mattias Bengtsson.
 *
 * GNOME Maps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * GNOME Maps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Mattias Bengtsson <mattias.jc.bengtsson@gmail.com>
 */

import GObject from 'gi://GObject';
import GeocodeGlib from 'gi://GeocodeGlib';

import {Application} from './application.js';
import {PlaceStore} from './placeStore.js';
import {TransitOptions} from './transitOptions.js';

export class QueryPoint extends GObject.Object {

    constructor() {
        super();
        this._place = null;
    }

    set place(p) {
        this._place = p;
        this.notify('place');
    }

    get place() {
        return this._place;
    }
}

GObject.registerClass({
    Properties: {
        'place': GObject.ParamSpec.object('place',
                                          '',
                                          '',
                                          GObject.ParamFlags.READABLE |
                                          GObject.ParamFlags.WRITABLE,
                                          GeocodeGlib.Place)
    }
}, QueryPoint);

export class RouteQuery extends GObject.Object {

    static MAX_QUERY_POINTS = 10;

    static Transportation = {
        CAR:        0,
        BIKE:       1,
        PEDESTRIAN: 2,
        TRANSIT:    3,

        toString: function (transportation) {
            switch(transportation) {
            case RouteQuery.Transportation.CAR:        return 'car';
            case RouteQuery.Transportation.BIKE:       return 'bike';
            case RouteQuery.Transportation.PEDESTRIAN: return 'foot';
            case RouteQuery.Transportation.TRANSIT:    return 'transit';
            default:                                   return null;
            }
        }
    }

    get points() {
        return this._points;
    }

    set points(points) {
        if (points.length > MAX_QUERY_POINTS)
            throw new Error('Too many query points');
        this._points = points;
        this.notify('points');
    }

    get filledPoints() {
        return this.points.filter(function(point) {
            return point.place;
        });
    }

    get latest() {
        return this._latest;
    }

    get time() {
        return this._time;
    }

    /* time to leave or arrive, null implies "Leave now" */
    set time(time) {
        this._time = time;
        this.notify('points');
    }

    get date() {
        return this._date;
    }

    /* date to leave or arrive */
    set date(date) {
        this._date = date;
        /* only notify change when an actual date was set, when resetting time
         * time and date (to use "Leave Now" routing) time would be set to null
         * triggering an update */
        if (date)
            this.notify('points');
    }

    get arriveBy() {
        return this._arriveBy;
    }

    /* when set to true, the set time and date means arrive by the specified
     * time */
    set arriveBy(arriveBy) {
        this._arriveBy = arriveBy;
        if (this._time)
            this.notify('points');
    }

    get transitOptions() {
        return this._transitOptions;
    }

    set transitOptions(options) {
        this._transitOptions = options;
        this.notify('points');
    }

    constructor(params) {
        super(params);
        this._points = [];
        this._time = null;
        this._date = null;
        this._arriveBy = false;
        this._transitOptions = new TransitOptions();
        this._initTransportation();
        this.reset();
    }

    _initTransportation() {
        let transportationType = Application.settings.get_enum('transportation-type');

        this.transportation = transportationType;
    }

    addPoint(index) {
        if (this._points.length >= RouteQuery.MAX_QUERY_POINTS)
            throw new Error('Too many query points');
        let point = new QueryPoint();

        if (index === -1)
            index = this.points.length - 1;

        this._points.splice(index, 0, point);
        point.connect('notify::place', () => {
            let placeStore = Application.placeStore;
            if (point.place) {
                if (!placeStore.exists(point.place, null)) {
                    placeStore.addPlace(point.place,
                                        PlaceStore.PlaceType.RECENT);
                }
            }
            this.notify('points');
            this._latest = point;
        });
        this._latest = point;
        this.notify('points');
        this.emit('point-added', point, index);
        return point;
    }

    removePoint(index) {
        let removedPoints = this._points.splice(index, 1);
        let point = removedPoints ? removedPoints[0] : null;
        if (point === this._latest)
            this._latest = null;

        if (point) {
            this.notify('points');
            this.emit('point-removed', point, index);
        }
    }

    set transportation(transportation) {
        Application.settings.set_enum('transportation-type', transportation);
        this._transportation = transportation;
        this.notify('transportation');
        this.notify('points');
    }

    get transportation() {
        return this._transportation;
    }

    reset() {
        this.freeze_notify();
        this._points.forEach(function(point) {
            point.place = null;
        });
        this._latest = null;
        this.thaw_notify();
        this.emit('reset');
    }

    isValid() {
        if (this.filledPoints.length >= 2 &&
            this.filledPoints.length === this.points.length)
            return true;
        else
            return false;
    }

    toString() {
        return "\nPoints: " + this.points +
               "\nTransportation: " + this.transportation;
    }
}

GObject.registerClass({
    Signals: {
        'reset': { },
        'point-added': { param_types: [GObject.TYPE_OBJECT, GObject.TYPE_INT] },
        'point-removed': { param_types: [GObject.TYPE_OBJECT, GObject.TYPE_INT] },
        'run': { }
    },
    Properties: {
        'points': GObject.ParamSpec.object('points',
                                           '',
                                           '',
                                           GObject.ParamFlags.READABLE |
                                           GObject.ParamFlags.WRITABLE,
                                           GObject.Object),
        'transportation': GObject.ParamSpec.int('transportation',
                                                '',
                                                '',
                                                GObject.ParamFlags.READABLE |
                                                GObject.ParamFlags.WRITABLE,
                                                RouteQuery.Transportation.CAR,
                                                RouteQuery.Transportation.PEDESTRIAN,
                                                RouteQuery.Transportation.CAR,
                                                RouteQuery.Transportation.TRANSIT)
    }
}, RouteQuery);