Daniel Werner has submitted this change and it was merged.
Change subject: [coordinate.js] Implemented coordinate data value
......................................................................
[coordinate.js] Implemented coordinate data value
Change-Id: Ie6ba7cb78842e14d745c90b67f02bd9d84194561
---
M DataValues/DataValues.resources.php
M DataValues/DataValues.tests.qunit.php
M DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
A DataValues/resources/values/CoordinateValue.js
A DataValues/tests/qunit/values/CoordinateValue.tests.js
5 files changed, 196 insertions(+), 12 deletions(-)
Approvals:
Daniel Werner: Verified; Looks good to me, approved
jenkins-bot: Verified
diff --git a/DataValues/DataValues.resources.php
b/DataValues/DataValues.resources.php
index 22944c7..c67b7f0 100644
--- a/DataValues/DataValues.resources.php
+++ b/DataValues/DataValues.resources.php
@@ -63,6 +63,7 @@
// depend on.
// TODO: Make one module per data value type.
'values/BoolValue.js',
+ 'values/CoordinateValue.js',
'values/MonolingualTextValue.js',
'values/MultilingualTextValue.js',
'values/StringValue.js',
@@ -73,6 +74,7 @@
),
'dependencies' => array(
'dataValues.DataValue',
+ 'coordinate.js', // required by CoordinateValue
'time.js' // required by TimeValue
),
),
diff --git a/DataValues/DataValues.tests.qunit.php
b/DataValues/DataValues.tests.qunit.php
index 16cc411..8fb0d2c 100644
--- a/DataValues/DataValues.tests.qunit.php
+++ b/DataValues/DataValues.tests.qunit.php
@@ -56,6 +56,7 @@
'dataValues.values.tests' => array(
'scripts' => array(
"$bp/values/BoolValue.tests.js",
+ "$bp/values/CoordinateValue.tests.js",
"$bp/values/MonolingualTextValue.tests.js",
"$bp/values/MultilingualTextValue.tests.js",
"$bp/values/StringValue.tests.js",
diff --git a/DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
b/DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
index 5e927ab..dd532ba 100644
--- a/DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
+++ b/DataValues/resources/coordinate.js/src/coordinate.Coordinate.js
@@ -18,8 +18,8 @@
/**
* Constructor for an object representing a coordinate with a certain
precision.
*
- * @param {string} rawInput
- * @param {Object} options
+ * @param {string|Object} coordinateDefinition
+ * @param {Object} [options]
* {number} precision: Precision which will overrule the
automatically detected
* precision.
*
@@ -27,28 +27,48 @@
*
* @constructor
*/
- var Coordinate = function Coordinate( rawInput, options ) {
+ var Coordinate = function Coordinate( coordinateDefinition, options ) {
var parsed;
options = options || {};
- if( !rawInput ) {
+ if( !coordinateDefinition ) {
throw new Error( 'No input given' );
}
- try {
- parsed = coordinateParser.parse( rawInput );
- } catch( e ) {
- throw new Error( 'Could not parse input: ' +
e.toString() );
+ if( typeof coordinateDefinition === 'string' ) {
+ try {
+ parsed = coordinateParser.parse(
coordinateDefinition );
+ } catch( e ) {
+ throw new Error( 'Could not parse input: ' +
e.toString() );
+ }
+
+ this._rawInput = coordinateDefinition;
+ this._latitude = parsed[0];
+ this._longitude = parsed[1];
+ this._precision = ( options.precision !== undefined ) ?
options.precision : parsed[2];
+ } else {
+ this._latitude = coordinateDefinition.latitude;
+ this._longitude = coordinateDefinition.longitude;
+
+ // The backend does not return precision, so we need to
parse the coordinate values:
+ // TODO: Have the backend support precision
+ parsed = coordinateParser.parse( this._latitude + ', '
+ this._longitude );
+ this._precision = parsed[2];
+
+ // TODO: Capture altitude and globe
}
- this._rawInput = rawInput;
- this._latitude = parsed[0];
- this._longitude = parsed[1];
- this._precision = ( options.precision !== undefined ) ?
options.precision : parsed[2];
+ this._globe = 'http://wikidata.org/id/Q2'; // TODO: Support
other globes
};
Coordinate.prototype = {
+ /**
+ * Globe URI
+ * @type {string}
+ */
+ _globe: null,
+
/**
* Raw input
* @type {string}
@@ -86,6 +106,15 @@
},
/**
+ * Returns the coordinate's globe URI.
+ *
+ * @return {string}
+ */
+ getGlobe: function() {
+ return this._globe;
+ },
+
+ /**
* Returns the original (raw) input.
*
* @return {string}
diff --git a/DataValues/resources/values/CoordinateValue.js
b/DataValues/resources/values/CoordinateValue.js
new file mode 100644
index 0000000..7607715
--- /dev/null
+++ b/DataValues/resources/values/CoordinateValue.js
@@ -0,0 +1,106 @@
+/**
+ * @file
+ * @ingroup DataValues
+ *
+ * @licence GNU GPL v2+
+ *
+ * @author H. Snater < [email protected] >
+ */
+( function( dv, $, Coordinate ) {
+ 'use strict';
+
+ var PARENT = dv.DataValue,
+ constructor = function( value ) {
+ if( !( value instanceof Coordinate ) ) {
+ throw new Error( 'The given value has to be a
coordinate.Coordinate object' );
+ }
+ if( !value.isValid() ) {
+ throw new Error( 'The given coordinate object
value has to represent a valid ' +
+ 'coordinate' );
+ }
+
+ this._value = value;
+ };
+
+ /**
+ * Constructor for creating a data value representing a coordinate.
+ *
+ * @constructor
+ * @extends dv.DataValue
+ * @since 0.1
+ *
+ * @param {coordinate.Coordinate} value
+ */
+ var SELF = dv.CoordinateValue = dv.util.inherit( 'DvCoordinateValue',
PARENT, constructor, {
+ /**
+ * @see dv.DataValue.getSortKey
+ *
+ * @since 0.1
+ *
+ * @return {string}
+ */
+ getSortKey: function() {
+ return this.getValue().iso6709();
+ },
+
+ /**
+ * @see dv.DataValue.getValue
+ *
+ * @since 0.1
+ *
+ * @return {coordinate.Coordinate}
+ */
+ getValue: function() {
+ return this._value;
+ },
+
+ /**
+ * @see dv.DataValue.equals
+ *
+ * @since 0.1
+ */
+ equals: function( value ) {
+ if ( !( value instanceof SELF ) ) {
+ return false;
+ }
+ return this.getValue().equals( value.getValue() );
+ },
+
+ /**
+ * @see dv.DataValue.toJSON
+ *
+ * @since 0.1
+ */
+ toJSON: function() {
+ var coordinate = this.getValue();
+
+ // TODO: Backend should interact with a proper JSON
structure and have precision implemented.
+ return coordinate.getLatitude() + '|' +
coordinate.getLongitude();
+ }
+
+ } );
+
+ /**
+ * @see dv.DataValue.newFromJSON
+ */
+ SELF.newFromJSON = function( json ) {
+ var data = json.split( '|' );
+
+ var c = new Coordinate( {
+ latitude: parseFloat( data[0] ),
+ longitude: parseFloat( data[1] ),
+ altitude: ( data[2] ) ? parseFloat( data[2] ) : null,
+ globe: ( data[3] ) ? data[3] : null
+ } );
+
+ return new SELF( c );
+ };
+
+ /**
+ * @see dv.DataValue.TYPE
+ */
+ SELF.TYPE = 'geocoordinate';
+
+ dv.registerDataValue( SELF );
+
+}( dataValues, jQuery, coordinate.Coordinate ) );
diff --git a/DataValues/tests/qunit/values/CoordinateValue.tests.js
b/DataValues/tests/qunit/values/CoordinateValue.tests.js
new file mode 100644
index 0000000..7a44aa5
--- /dev/null
+++ b/DataValues/tests/qunit/values/CoordinateValue.tests.js
@@ -0,0 +1,46 @@
+/**
+ * @since 0.1
+ * @file
+ * @ingroup DataValues
+ *
+ * @licence GNU GPL v2+
+ * @author H. Snater < [email protected] >
+ */
+( function( dv, $, QUnit, Coordinate ) {
+ 'use strict';
+
+ var PARENT = dv.tests.DataValueTest;
+
+ /**
+ * Constructor for creating a test object for the coordinate DataValue.
+ *
+ * @constructor
+ * @extends dv.tests.DataValueTest
+ * @since 0.1
+ */
+ dv.tests.CoordinateValueTest = dv.util.inherit( PARENT, {
+
+ /**
+ * @see dv.tests.DataValueTest.getConstructor
+ */
+ getConstructor: function() {
+ return dv.CoordinateValue;
+ },
+
+ /**
+ * @see dv.tests.DataValueTest.getConstructorArguments
+ */
+ getConstructorArguments: function() {
+ return [
+ [ new Coordinate( '1.5 1.25' ) ],
+ [ new Coordinate( '-50 -20' ) ]
+ ];
+ }
+
+ } );
+
+ var test = new dv.tests.CoordinateValueTest();
+
+ test.runTests( 'dataValues.CoordinateValue' );
+
+}( dataValues, jQuery, QUnit, coordinate.Coordinate ) );
--
To view, visit https://gerrit.wikimedia.org/r/65962
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie6ba7cb78842e14d745c90b67f02bd9d84194561
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits