Skip to content

Commit 4d303eb

Browse files
comratwhoozle
authored andcommitted
Device refactoring (#98)
* Start refactoring: use common object for all platforms * Implement Device backend for web platform * Implement device backend for android * Move location implementation to considered backend * Fix Location declaration * Fix build * Move 'location.js' to 'html' folder * fixed typo
1 parent fee64b8 commit 4d303eb

File tree

12 files changed

+147
-88
lines changed

12 files changed

+147
-88
lines changed

core/Device.qml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Object {
2+
signal propertyUpdated;
3+
property string macAddess;
4+
property string modelName;
5+
property string deviceId;
6+
property string firmware;
7+
property string sdk;
8+
property bool supportingUhd;
9+
property bool supporting3d;
10+
11+
constructor: {
12+
var backend = _globals.core.__deviceBackend
13+
if (!backend)
14+
throw new Error('no backend found')
15+
backend().createDevice(this)
16+
}
17+
18+
onSdkChanged,
19+
onDeviceIdChanged,
20+
onFirmwareChanged,
21+
onMacAddessChanged,
22+
onModelNameChanged,
23+
onSupporting3dChanged,
24+
onSupportingUhdChanged: { this.propertyUpdated() }
25+
}

core/Location.qml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// Window location object
2+
Object {
3+
property string hash; ///< contains current hash value (after '#' charachter)
4+
property string host; ///< current host with port number
5+
property string href; ///< whole current URL
6+
property string port; ///< current port number
7+
property string origin; ///< current protocol, hostname and port number of a URL
8+
property string hostname; ///< current host name
9+
property string pathname; ///< path name of the current URL
10+
property string protocol; ///< current protocol
11+
property string search; ///< query string of the URL
12+
property Object state; ///< current history state
13+
14+
///@private
15+
constructor: {
16+
this.impl = null
17+
this._createLocation()
18+
}
19+
20+
///@private
21+
function _getLocation() {
22+
if (this.impl === null)
23+
this._createPlayer()
24+
return this.impl
25+
}
26+
27+
///@private
28+
function _createLocation() {
29+
if (this.impl)
30+
return this.impl
31+
32+
var backend = _globals.core.__locationBackend
33+
if (!backend)
34+
throw new Error('no backend found')
35+
return this.impl = backend().createLocation(this)
36+
}
37+
38+
pushState(state, title, url): {
39+
var location = this._getLocation()
40+
if (location)
41+
player.pushState(state, title, url)
42+
}
43+
44+
changeHref(href): {
45+
var location = this._getLocation()
46+
if (location)
47+
player.changeHref(href)
48+
}
49+
}

platform/android/.core.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
if (navigator.userAgent.indexOf('Android') >= 0) {
2+
_globals.core.__deviceBackend = function() { return _globals.android.device }
3+
24
log = console.log.bind(console)
35

46
log("Android detected")

platform/android/Device.qml

-10
This file was deleted.

platform/android/device.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var Device = function(ui) {
2+
ui.deviceId = "android_" + Math.random().toString(36).substr(2, 9)
3+
}
4+
5+
exports.createDevice = function(ui) {
6+
return new Device(ui)
7+
}
8+
9+
exports.Device = Device

platform/html5/.core.js

+1
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ else
4646

4747

4848
_globals._backend = function() { return _globals.html5.html }
49+
_globals.core.__locationBackend = function() { return _globals.html5.location }

platform/html5/Location.qml

-54
This file was deleted.

platform/html5/location.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var Location = function(ui) {
2+
this._ui = ui
3+
var location = window.location
4+
this.updateActualValues()
5+
var self = this
6+
var context = ui._context
7+
context.window.on("hashchange", function() { self._ui.hash = location.hash }.bind(this))
8+
context.window.on("popstate", function() { self.updateActualValues() }.bind(this))
9+
}
10+
11+
Location.prototype.updateActualValues = function() {
12+
var ui = this._ui
13+
var windowContext = ui._context.window.dom
14+
ui.hash = windowContext.location.hash
15+
ui.href = windowContext.location.href
16+
ui.port = windowContext.location.port
17+
ui.host = windowContext.location.host
18+
ui.origin = windowContext.location.origin
19+
ui.hostname = windowContext.location.hostname
20+
ui.pathname = windowContext.location.pathname
21+
ui.protocol = windowContext.location.protocol
22+
ui.search = windowContext.location.search
23+
ui.state = windowContext.history.state
24+
}
25+
26+
Location.prototype.changeHref = function(href) {
27+
this._ui._context.window.dom.location.href = href
28+
this.updateActualValues()
29+
}
30+
31+
Location.prototype.pushState = function(state, title, url) {
32+
var ui = this._ui
33+
var windowContext = ui._context.window.dom
34+
if (windowContext.location.hostname) {
35+
windowContext.history.pushState(state, title, url)
36+
this.updateActualValues()
37+
} else {
38+
ui._context.document.title = title
39+
this.state = state
40+
}
41+
}
42+
43+
exports.createLocation = function(ui) {
44+
return new Location(ui)
45+
}
46+
47+
exports.Location = Location

platform/pure/Location.qml

-13
This file was deleted.

platform/web/.core.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
_globals.core.__deviceBackend = function() { return _globals.web.device }
2+
13
exports.core.keyCodes = {
24
13: 'Select',
35
16: 'Shift',

platform/web/Device.qml

-11
This file was deleted.

platform/web/device.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var Device = function(ui) {
2+
var context = ui._context
3+
var deviceString = context.system.os + "_" + context.system.browser
4+
deviceString = deviceString.replace(/\s/g, '')
5+
ui.deviceId = deviceString + "_" + Math.random().toString(36).substr(2, 9)
6+
}
7+
8+
exports.createDevice = function(ui) {
9+
return new Device(ui)
10+
}
11+
12+
exports.Device = Device

0 commit comments

Comments
 (0)