summaryrefslogtreecommitdiff
path: root/src/placeFormatter.js
blob: 71dadf69b33e53b6b2d486bc4967225a15b49878 (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
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * Copyright (c) 2014 Damián Nohales
 *
 * 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: Damián Nohales <damiannohales@gmail.com>
 */

const Geocode = imports.gi.GeocodeGlib;
const Lang = imports.lang;

const StoredRoute = imports.storedRoute;

var PlaceFormatter = new Lang.Class({
    Name: "PlaceFormatter",

    _init: function(place) {
        this._place = place;
        this._rows = [];
        this._titleProperty = 'name';

        this._update();
    },

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

    get title() {
        let title;
        // For the 'name' property we split after comma to avoid
        // duplicating information in the title from the Geocode
        // display name.
        if (this._titleProperty === 'name')
            title = this._place[this._titleProperty].split(',')[0];
        else
            title = this.place[this._titleProperty];

        return title;
    },

    get rows() {
        return this._rows;
    },

    getDetailsString: function() {
        if (this._place instanceof StoredRoute.StoredRoute)
            return this._place.viaString;

        return this.rows.map((row) => {
            return row.map((prop) => {
                return this._place[prop];
            }).join(', ');
        }).join(', ');
    },

    _update: function() {
        switch (this._place.place_type) {
        case Geocode.PlaceType.COUNTRY:
            if (this._place.country)
                this._titleProperty = 'country';

            this._addRow(['country_code']);
            break;

        case Geocode.PlaceType.STATE:
            if (this._place.state)
                this._titleProperty = 'state';
            break;

        case Geocode.PlaceType.COUNTY:
            if (this._place.county)
                this._titleProperty = 'county';
            break;

        case Geocode.PlaceType.TOWN:
            if (this._place.town)
                this._titleProperty = 'town';

            if (this._place.county)
                this._addRow(['county']);
            else if (this._place.state)
                this._addRow(['state']);
            else if (this._place.area)
                this._addRow(['area']);
            break;

        default:
            if (this._place.street_address)
                this._addRow(['street_address']);
            else if (this._place.street)
                this._addRow(['street']);

            if (this._place.town !== this._place[this._titleProperty])
                this._addRow(['postal_code', 'town']);
            break;
        }
    },

    _addRow: function(properties) {
        properties = properties.filter((prop) => this._place[prop] ? true : false);

        if (properties.length > 0)
            this._rows.push(properties);
    }
});