Tobias Gritschacher has submitted this change and it was merged. Change subject: (bug 48145) Introduction of TimeValue data value type in JavaScript ......................................................................
(bug 48145) Introduction of TimeValue data value type in JavaScript Changed the backend's TimeValue precision numbers to match the one from the frontend since the time.js's options and precision handling is quite a mess right now. For having a simple version of the time value frontend running asap, this is the faster way to get there. Change-Id: Id9db92258aa667cc625df1483b0dd5aaf8dd3c7b --- M DataTypes/DataTypes.mw.php M DataValues/DataValues.resources.php M DataValues/DataValues.tests.qunit.php M DataValues/includes/values/TimeValue.php A DataValues/resources/values/TimeValue.js A DataValues/tests/qunit/values/TimeValue.tests.js 6 files changed, 185 insertions(+), 17 deletions(-) Approvals: Tobias Gritschacher: Looks good to me, approved jenkins-bot: Verified diff --git a/DataTypes/DataTypes.mw.php b/DataTypes/DataTypes.mw.php index 39d7c68..23622ac 100644 --- a/DataTypes/DataTypes.mw.php +++ b/DataTypes/DataTypes.mw.php @@ -124,7 +124,8 @@ ), 'dependencies' => array( 'dataTypes', - 'qunit.parameterize' + 'dataValues.values', + 'qunit.parameterize', ), ); diff --git a/DataValues/DataValues.resources.php b/DataValues/DataValues.resources.php index 70eff85..da979e9 100644 --- a/DataValues/DataValues.resources.php +++ b/DataValues/DataValues.resources.php @@ -57,16 +57,20 @@ 'dataValues.values' => $moduleTemplate + array( 'scripts' => array( - // Note: the order here is relevant, scripts should be places after the ones they depend on + // Note: The order here is relevant, scripts should be places after the ones they + // depend on. + // TODO: Make one module per data value type. 'values/BoolValue.js', 'values/MonolingualTextValue.js', 'values/MultilingualTextValue.js', 'values/StringValue.js', 'values/NumberValue.js', + 'values/TimeValue.js', 'values/UnknownValue.js', ), 'dependencies' => array( 'dataValues.DataValue', + 'time.js' // required by TimeValue ), ), diff --git a/DataValues/DataValues.tests.qunit.php b/DataValues/DataValues.tests.qunit.php index b3309b6..a2fa7d5 100644 --- a/DataValues/DataValues.tests.qunit.php +++ b/DataValues/DataValues.tests.qunit.php @@ -60,6 +60,7 @@ "$bp/values/MultilingualTextValue.tests.js", "$bp/values/StringValue.tests.js", "$bp/values/NumberValue.tests.js", + "$bp/values/TimeValue.tests.js", "$bp/values/UnknownValue.tests.js", ), 'dependencies' => array( diff --git a/DataValues/includes/values/TimeValue.php b/DataValues/includes/values/TimeValue.php index 5adbb08..3a0db4e 100644 --- a/DataValues/includes/values/TimeValue.php +++ b/DataValues/includes/values/TimeValue.php @@ -34,21 +34,21 @@ */ class TimeValue extends DataValueObject { - const PRECISION_Ga = 28; // Gigayear - const PRECISION_100Ma = 29; // 100 Megayears - const PRECISION_10Ma = 30; // 10 Megayears - const PRECISION_Ma = 31; // Megayear - const PRECISION_100ka = 32; // 100 Kiloyears - const PRECISION_10ka = 33; // 10 Kiloyears - const PRECISION_ka = 34; // Kiloyear - const PRECISION_100a = 35; // 100 years - const PRECISION_10a = 36; // 10 years - const PRECISION_YEAR = 37; - const PRECISION_MONTH = 38; - const PRECISION_DAY = 39; - const PRECISION_HOUR = 40; - const PRECISION_MINUTE = 41; - const PRECISION_SECOND = 42; + const PRECISION_Ga = 0; // Gigayear + const PRECISION_100Ma = 1; // 100 Megayears + const PRECISION_10Ma = 2; // 10 Megayears + const PRECISION_Ma = 3; // Megayear + const PRECISION_100ka = 4; // 100 Kiloyears + const PRECISION_10ka = 5; // 10 Kiloyears + const PRECISION_ka = 6; // Kiloyear + const PRECISION_100a = 7; // 100 years + const PRECISION_10a = 8; // 10 years + const PRECISION_YEAR = 9; + const PRECISION_MONTH = 10; + const PRECISION_DAY = 11; + const PRECISION_HOUR = 12; + const PRECISION_MINUTE = 13; + const PRECISION_SECOND = 14; /** * Point in time, represented per ISO8601. diff --git a/DataValues/resources/values/TimeValue.js b/DataValues/resources/values/TimeValue.js new file mode 100644 index 0000000..3436ee5 --- /dev/null +++ b/DataValues/resources/values/TimeValue.js @@ -0,0 +1,115 @@ +/** + * @file + * @ingroup DataValues + * + * @licence GNU GPL v2+ + * + * @author Daniel Werner < daniel.wer...@wikimedia.de > + */ +( function( dv, $, Time ) { + 'use strict'; + + var PARENT = dv.DataValue, + constructor = function( value ) { + if( !( value instanceof Time ) ) { + throw new Error( 'The given value has to be a time.Time object' ); + } + if( !value.isValid() ) { + throw new Error( 'The given time value has to represent a valid time' ); + } + + this._value = value; + }; + + /** + * Constructor for creating a data value representing time. + * + * @constructor + * @extends dv.DataValue + * @since 0.1 + * + * @param {String} value + */ + var SELF = dv.TimeValue = dv.util.inherit( 'DvTimeValue', PARENT, constructor, { + /** + * @see dv.DataValue.getSortKey + * + * @since 0.1 + * + * @return String + */ + getSortKey: function() { + return this.getValue().iso8601(); + }, + + /** + * @see dv.DataValue.getValue + * + * @since 0.1 + * + * @return time.Time + */ + getValue: function() { + return this._value; + }, + + /** + * @see dv.DataValue.equals + * + * @since 0.1 + */ + equals: function( value ) { + if ( !( value instanceof SELF ) ) { + return false; + } + + var ownTime = this.getValue(), + otherTime = value.getValue(); + + // no need to check for isValid() since constructor won't allow invalid Time values + + return ownTime.precision() === otherTime.precision() + && ownTime.iso8601() === otherTime.iso8601(); + }, + + /** + * @see dv.DataValue.toJSON + * + * @since 0.1 + */ + toJSON: function() { + var time = this.getValue(); + + return { + time: time.iso8601(), + timezone: 0, // TODO timezone (offset in minutes) + before: 0, // TODO + after: 0, // TODO + precision: time.precision(), + calendarmodel: time.calendarURI() + }; + } + + } ); + + /** + * @see dv.DataValue.newFromJSON + */ + SELF.newFromJSON = function( json ) { + // TODO: not good to do it this way, there are some lost information, e.g. the calendar + // model as well as before/after and UTC offset! + // Could simply fix this by creating a second Time object where we use those infos as well + // as the first Time object's year(), month(), day() etc. The Time constructor currently + // only takes a string for parsing though, which is very bad as well. + var time = Time.newFromIso8601( json.time, json.precision ); + return new SELF( time ); + }; + + /** + * @see dv.DataValue.TYPE + */ + SELF.TYPE = 'time'; + + dv.registerDataValue( SELF ); + +}( dataValues, jQuery, time.Time ) ); diff --git a/DataValues/tests/qunit/values/TimeValue.tests.js b/DataValues/tests/qunit/values/TimeValue.tests.js new file mode 100644 index 0000000..b63b3c4 --- /dev/null +++ b/DataValues/tests/qunit/values/TimeValue.tests.js @@ -0,0 +1,47 @@ +/** + * @since 0.1 + * @file + * @ingroup DataValues + * + * @licence GNU GPL v2+ + * @author Daniel Werner < daniel.wer...@wikimedia.de > + */ +( function( dv, $, QUnit, Time ) { + 'use strict'; + + var PARENT = dv.tests.DataValueTest; + + /** + * Constructor for creating a test object for the time DataValue. + * + * @constructor + * @extends dv.tests.DataValueTest + * @since 0.1 + */ + dv.tests.TimeValueTest = dv.util.inherit( PARENT, { + + /** + * @see dv.tests.DataValueTest.getConstructor + */ + getConstructor: function() { + return dv.TimeValue; + }, + + /** + * @see dv.tests.DataValueTest.getConstructorArguments + */ + getConstructorArguments: function() { + return [ + [ new Time( 'April 1, 1942' ) ], + [ new Time( '123456 BC' ) ], + [ new Time( '-42' ) ] + ]; + } + + } ); + + var test = new dv.tests.TimeValueTest(); + + test.runTests( 'dataValues.TimeValue' ); + +}( dataValues, jQuery, QUnit, time.Time ) ); -- To view, visit https://gerrit.wikimedia.org/r/61527 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Id9db92258aa667cc625df1483b0dd5aaf8dd3c7b Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/extensions/DataValues Gerrit-Branch: master Gerrit-Owner: Daniel Werner <daniel.wer...@wikimedia.de> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits