-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGeoLocationForPhoneGap.js
More file actions
141 lines (115 loc) · 4.4 KB
/
GeoLocationForPhoneGap.js
File metadata and controls
141 lines (115 loc) · 4.4 KB
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
define([
"mxui/widget/_WidgetBase", "mxui/dom", "dojo/dom-class", "dojo/dom-construct",
"dojo/_base/declare"
], function(_WidgetBase, mxuiDom, dojoClass, dojoConstruct, declare) {
"use strict";
return declare("GeoLocationForPhoneGap.widget.GeoLocationForPhoneGap", _WidgetBase, {
buttonLabel: "",
latAttr: 0.0,
longAttr: 0.0,
onchangemf: "",
onChangeNanoflow: null,
_result: null,
_button: null,
_hasStarted: false,
_obj: null,
// Externally executed mendix function to create widget.
startup: function() {
if (this._hasStarted) {
return;
}
this._hasStarted = true;
// Setup widget
this._setupWX();
// Create childnodes
this._createChildnodes();
// Setup events
this._setupEvents();
},
update: function(obj, callback) {
this._obj = obj;
// If configured, automatically update geolocation
if (this.getPositionOnLoad) {
this._getCurrentPosition();
}
if (callback) {
callback();
}
},
// Setup
_setupWX: function() {
// Set class for domNode
dojoClass.add(this.domNode, "wx-geolocation-container");
// Empty domnode of this and appand new input
dojoConstruct.empty(this.domNode);
},
_createChildnodes: function() {
// Placeholder container
this._button = mxuiDom.create("div", {
"class": "wx-mxwxgeolocation-button btn btn-primary"
});
if (this.buttonClass) {
dojoClass.add(this._button, this.buttonClass);
}
this._button.textContent = this.buttonLabel || "GEO Location";
// Add to wxnode
this.domNode.appendChild(this._button);
},
// Internal event setup.
_setupEvents: function() {
this.connect(this._button, "click", this._getCurrentPosition);
},
_getCurrentPosition: function() {
console.log("GEO Location start getting location.");
navigator.geolocation.getCurrentPosition(
this._geolocationSuccess.bind(this),
this._geolocationFailure.bind(this), {
timeout: 10000,
enableHighAccuracy: this.enableHighAccuracy
});
},
_geolocationSuccess: function(position) {
this._obj.set(this.latAttr, +position.coords.latitude.toFixed(8));
this._obj.set(this.longAttr, +position.coords.longitude.toFixed(8));
this._executeAction();
},
_geolocationFailure: function(error) {
console.log("GEO Location failure!");
console.log(error.message);
if (this._result) {
this._result.textContent = "GEO Location failure...";
} else {
this._result = mxuiDom.create("div");
this._result.textContent = "GEO Location failure...";
this.domNode.appendChild(this._result);
}
},
_executeAction: function() {
if (this.onchangemf && this._obj) {
mx.data.action({
params: {
actionname: this.onchangemf,
applyto: "selection",
guids: [ this._obj.getGuid() ]
},
origin: this.mxform,
error: function (error) {
mx.ui.error("An error occurred while executing the " + this.onchangemf + ": " + error.message);
},
});
}
if (this.onChangeNanoflow.nanoflow && this.mxcontext) {
mx.data.callNanoflow({
nanoflow: this.onChangeNanoflow,
origin: this.mxform,
context: this.mxcontext,
error: function (error) {
mx.ui.error("An error occurred while executing the on change nanoflow: " + error.message);
}
});
}
}
});
});
// Compatibility with older mendix versions.
require([ "GeoLocationForPhoneGap/widget/GeoLocationForPhoneGap" ], function() {});