|
1 | | -// Dropdown Select Widget |
2 | | -dojo.provide('GeoLocationForPhoneGap.widget.GeoLocationForPhoneGap'); |
3 | | -dojo.declare('GeoLocationForPhoneGap.widget.GeoLocationForPhoneGap', mxui.widget._WidgetBase, { |
4 | | - |
5 | | - /* Inputargs |
6 | | - |
7 | | - this.buttonLabel : string, |
8 | | - this.latAttr : float, |
9 | | - this.longAttr : float, |
10 | | - this.onchangemf : string |
11 | | -
|
12 | | - */ |
13 | | - |
14 | | - // Coding guideline, internal variables start with '_'. |
15 | | - // internal variables. |
16 | | - |
17 | | - // * |
18 | | - _result : null, |
19 | | - _button : null, |
20 | | - _hasStarted : false, |
21 | | - _obj : null, |
22 | | - _objSub : null, |
23 | | - |
24 | | - // Externally executed mendix function to create widget. |
25 | | - startup: function() { |
26 | | - 'use strict'; |
27 | | - |
28 | | - if (this._hasStarted) |
29 | | - return; |
30 | | - |
31 | | - this._hasStarted = true; |
32 | | - |
33 | | - // Setup widget |
34 | | - this._setupWX(); |
35 | | - |
36 | | - // Create childnodes |
37 | | - this._createChildnodes(); |
38 | | - |
39 | | - // Setup events |
40 | | - this._setupEvents(); |
41 | | - |
42 | | - }, |
43 | | - |
44 | | - update : function (obj, callback) { |
45 | | - 'use strict'; |
46 | | - |
47 | | - if(typeof obj === 'string'){ |
48 | | - this._contextGuid = obj; |
49 | | - mx.data.get({ |
50 | | - guids : [obj], |
51 | | - callback : dojo.hitch(this, function (objArr) { |
52 | | - if (objArr.length === 1) |
53 | | - this._loadData(objArr[0]); |
54 | | - else |
55 | | - console.log('Could not find the object corresponding to the received object ID.') |
56 | | - }) |
57 | | - }); |
58 | | - } else if(obj === null){ |
59 | | - // Sorry no data no show! |
60 | | - console.log('Whoops... the GEO Location has no data!'); |
61 | | - } else { |
62 | | - // Attach to data refresh. |
63 | | - if (this._objSub) |
64 | | - this.unsubscribe(this._objSub); |
65 | | - |
66 | | - this._objSub = mx.data.subscribe({ |
67 | | - guid : obj.getGuid(), |
68 | | - callback : dojo.hitch(this, this.update) |
69 | | - }); |
70 | | - // Load data |
71 | | - this._loadData(obj); |
72 | | - } |
| 1 | +define([ |
| 2 | + "mxui/widget/_WidgetBase", "mxui/dom", "dojo/dom-class", "dojo/dom-construct", |
| 3 | + "dojo/_base/declare" |
| 4 | +], function(_WidgetBase, mxuiDom, dojoClass, dojoConstruct, declare) { |
| 5 | + "use strict"; |
73 | 6 |
|
74 | | - if(typeof callback !== 'undefined') { |
75 | | - callback(); |
76 | | - } |
77 | | - }, |
78 | | - |
79 | | - // Loading data |
80 | | - _loadData : function(obj){ |
81 | | - this._obj = obj; |
82 | | - }, |
83 | | - |
84 | | - // Setup |
85 | | - _setupWX: function() { |
86 | | - 'use strict'; |
87 | | - |
88 | | - // Set class for domNode |
89 | | - dojo.addClass(this.domNode, 'wx-geolocation-container'); |
90 | | - |
91 | | - // Empty domnode of this and appand new input |
92 | | - dojo.empty(this.domNode); |
93 | | - }, |
94 | | - |
95 | | - _createChildnodes: function() { |
96 | | - 'use strict'; |
97 | | - |
98 | | - // Placeholder container |
99 | | - this._button = mxui.dom.div(); |
100 | | - dojo.addClass(this._button, 'wx-mxwxgeolocation-button btn btn-primary'); |
101 | | - if (this.buttonClass) |
102 | | - dojo.addClass(this._button, this.buttonClass); |
103 | | - |
104 | | - dojo.html.set(this._button, this.buttonLabel || 'GEO Location'); |
105 | | - |
106 | | - // Add to wxnode |
107 | | - this.domNode.appendChild(this._button); |
108 | | - }, |
109 | | - |
110 | | - // Internal event setup. |
111 | | - _setupEvents : function() { |
112 | | - 'use strict'; |
113 | | - |
114 | | - // Scope is now inside me variable. |
115 | | - var me = this; |
116 | | - |
117 | | - // Attach only one event to dropdown list. |
118 | | - dojo.connect( this._button, "click", dojo.hitch(this, function(evt){ |
119 | | - console.log('GEO Location start getting location.'); |
120 | | - |
121 | | - // The camera function has a success, failure and a reference to this. |
122 | | - navigator.geolocation.getCurrentPosition( |
123 | | - dojo.hitch(this, this._geolocationSuccess), |
124 | | - dojo.hitch(this, this._geolocationFailure), |
125 | | - {timeout: 10000, enableHighAccuracy: true}); |
126 | | - })); |
127 | | - |
128 | | - }, |
129 | | - |
130 | | - _geolocationSuccess : function(position){ |
131 | | - 'use strict'; |
132 | | - |
133 | | - this._obj.set(this.latAttr, position.coords.latitude); |
134 | | - this._obj.set(this.longAttr, position.coords.longitude); |
135 | | - this._executeMicroflow(); |
136 | | - }, |
137 | | - |
138 | | - _geolocationFailure : function(error){ |
139 | | - 'use strict'; |
140 | | - |
141 | | - console.log('GEO Location failure!'); |
142 | | - console.log(error.message); |
143 | | - |
144 | | - if(this._result){ |
145 | | - dojo.html.set(this._result, 'GEO Location failure...' ); |
146 | | - } else { |
147 | | - this._result = mxui.dom.div(); |
148 | | - dojo.html.set(this._result, 'GEO Location failure...' ); |
149 | | - this.domNode.appendChild(this._result); |
150 | | - } |
151 | | - }, |
| 7 | + return declare("GeoLocationForPhoneGap.widget.GeoLocationForPhoneGap", _WidgetBase, { |
| 8 | + |
| 9 | + buttonLabel: "", |
| 10 | + latAttr: 0.0, |
| 11 | + longAttr: 0.0, |
| 12 | + onchangemf: "", |
| 13 | + |
| 14 | + _result: null, |
| 15 | + _button: null, |
| 16 | + _hasStarted: false, |
| 17 | + _obj: null, |
| 18 | + |
| 19 | + // Externally executed mendix function to create widget. |
| 20 | + startup: function() { |
| 21 | + if (this._hasStarted) |
| 22 | + return; |
| 23 | + |
| 24 | + this._hasStarted = true; |
152 | 25 |
|
153 | | - _executeMicroflow : function () { |
154 | | - 'use strict'; |
| 26 | + // Setup widget |
| 27 | + this._setupWX(); |
155 | 28 |
|
156 | | - if (this.onchangemf && this._obj) { |
157 | | - mx.processor.xasAction({ |
158 | | - error : function() {}, |
159 | | - actionname : this.onchangemf, |
160 | | - applyto : 'selection', |
161 | | - guids : [this._obj.getGuid()] |
| 29 | + // Create childnodes |
| 30 | + this._createChildnodes(); |
| 31 | + |
| 32 | + // Setup events |
| 33 | + this._setupEvents(); |
| 34 | + |
| 35 | + }, |
| 36 | + |
| 37 | + update: function(obj, callback) { |
| 38 | + this._obj = obj; |
| 39 | + |
| 40 | + if (callback) callback(); |
| 41 | + }, |
| 42 | + |
| 43 | + // Setup |
| 44 | + _setupWX: function() { |
| 45 | + // Set class for domNode |
| 46 | + dojoClass.add(this.domNode, "wx-geolocation-container"); |
| 47 | + |
| 48 | + // Empty domnode of this and appand new input |
| 49 | + dojoConstruct.empty(this.domNode); |
| 50 | + }, |
| 51 | + |
| 52 | + _createChildnodes: function() { |
| 53 | + // Placeholder container |
| 54 | + this._button = mxuiDom.create("div", { |
| 55 | + "class": "wx-mxwxgeolocation-button btn btn-primary" |
162 | 56 | }); |
| 57 | + if (this.buttonClass) |
| 58 | + dojoClass.add(this._button, this.buttonClass); |
| 59 | + |
| 60 | + this._button.textContent = this.buttonLabel || "GEO Location"; |
| 61 | + |
| 62 | + // Add to wxnode |
| 63 | + this.domNode.appendChild(this._button); |
| 64 | + }, |
| 65 | + |
| 66 | + // Internal event setup. |
| 67 | + _setupEvents: function() { |
| 68 | + this.connect(this._button, "click", function(evt) { |
| 69 | + console.log("GEO Location start getting location."); |
| 70 | + |
| 71 | + navigator.geolocation.getCurrentPosition( |
| 72 | + this._geolocationSuccess.bind(this), |
| 73 | + this._geolocationFailure.bind(this), { |
| 74 | + timeout: 10000, |
| 75 | + enableHighAccuracy: true |
| 76 | + }); |
| 77 | + }); |
| 78 | + }, |
| 79 | + |
| 80 | + _geolocationSuccess: function(position) { |
| 81 | + this._obj.set(this.latAttr, position.coords.latitude); |
| 82 | + this._obj.set(this.longAttr, position.coords.longitude); |
| 83 | + this._executeMicroflow(); |
| 84 | + }, |
| 85 | + |
| 86 | + _geolocationFailure: function(error) { |
| 87 | + console.log("GEO Location failure!"); |
| 88 | + console.log(error.message); |
| 89 | + |
| 90 | + if (this._result) { |
| 91 | + this._result.textContent = "GEO Location failure..."; |
| 92 | + } else { |
| 93 | + this._result = mxuiDom.create("div"); |
| 94 | + this._result.textContent = "GEO Location failure..."; |
| 95 | + this.domNode.appendChild(this._result); |
| 96 | + } |
| 97 | + }, |
| 98 | + |
| 99 | + _executeMicroflow: function() { |
| 100 | + if (this.onchangemf && this._obj) { |
| 101 | + mx.data.action({ |
| 102 | + params: { |
| 103 | + actionname: this.onchangemf, |
| 104 | + applyto: "selection", |
| 105 | + guids: [ this._obj.getGuid() ] |
| 106 | + }, |
| 107 | + error: function() {}, |
| 108 | + }); |
| 109 | + } |
163 | 110 | } |
164 | | - } |
| 111 | + }); |
165 | 112 | }); |
| 113 | + |
| 114 | +// Compatibility with older mendix versions. |
| 115 | +require([ "GeoLocationForPhoneGap/widget/GeoLocationForPhoneGap" ], function() {}); |
0 commit comments