Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

// Globals
"predef": [
"dataTypes", // TODO: Remove dataTypes dependency
"dataValues",
"globeCoordinate",
"jQuery",
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
},
"require": {
"composer/installers": ">=1.0.1",
"data-values/interfaces": "~0.1",
"data-values/data-types": "0.*,>=0.1.1"
"data-values/interfaces": "~0.1"
},
"autoload": {
"files" : [
Expand Down
1 change: 0 additions & 1 deletion js/ValueFormatters.resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
'ValueFormatterFactory.js',
),
'dependencies' => array(
'dataTypes',
'valueFormatters',
),
),
Expand Down
1 change: 0 additions & 1 deletion js/ValueParsers.resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
'ValueParserFactory.js',
),
'dependencies' => array(
'dataTypes',
'valueParsers',
),
),
Expand Down
79 changes: 28 additions & 51 deletions js/src/ValueFormatters/ValueFormatterFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @licence GNU GPL v2+
* @author H. Snater < mediawiki@snater.com >
*/
( function( $, vf, dt ) {
( function( $, vf ) {
'use strict';

/**
Expand Down Expand Up @@ -37,63 +37,48 @@
*/
_formattersForDataValueTypes: null,

/**
* Registers a formatter.
* @since 0.1
*
* @param {dataTypes.DataType|string} purpose DataType object or DataValue type.
* @param {Function} Formatter ValueFormatter constructor.
*
* @throws {Error} if the formatter constructor is invalid.
* @throws {Error} no proper purpose is provided.
*/
registerFormatter: function( purpose, Formatter ) {
if( !$.isFunction( Formatter ) ) {
throw new Error( 'Invalid ValueFormatter constructor' );
}

if( purpose instanceof dt.DataType ) {
this._registerDataTypeFormatter( purpose, Formatter );
} else if( typeof purpose === 'string' ) {
this._registerDataValueFormatter( purpose, Formatter );
} else {
throw new Error( 'No sufficient purpose provided what to register the formatter '
+ 'for' );
}
},

/**
* Registers a formatter for a certain data type.
* @since 0.1
*
* @param {dataTypes.DataType} dataType
* @param {Function} Formatter
* @param {string} dataTypeId
*
* @throws {Error} if a formatter for the specified dataType object is registered already.
*/
_registerDataTypeFormatter: function( dataType, Formatter ) {
registerDataTypeFormatter: function( Formatter, dataTypeId ) {
assertIsValueFormatterConstructor( Formatter );

if( this._formattersForDataTypes[dataType.getId()] ) {
throw new Error( 'Formatter for DataType "' + dataType.getId() + '" is registered '
if( dataTypeId === undefined ) {
throw new Error( 'No proper data type id provided to register the formatter for' );
}

if( this._formattersForDataTypes[dataTypeId] ) {
throw new Error( 'Formatter for DataType "' + dataTypeId + '" is registered '
+ 'already' );
}

this._formattersForDataTypes[dataType.getId()] = Formatter;
this._formattersForDataTypes[dataTypeId] = Formatter;
},

/**
* Registers a formatter for a certain data value type.
* @since 0.1
*
* @param {string} dataValueType
* @param {Function} Formatter
* @param {string} dataValueType
*
* @throws {Error} if no data type id is specified.
* @throws {Error} if a formatter for the specified DataValue type is registered already.
*/
_registerDataValueFormatter: function( dataValueType, Formatter ) {
registerDataValueFormatter: function( Formatter, dataValueType ) {
assertIsValueFormatterConstructor( Formatter );

if( dataValueType === undefined ) {
throw new Error( 'No proper data value type provided to register the formatter '
+ 'for' );
}

if( this._formattersForDataValueTypes[dataValueType] ) {
throw new Error( 'Formatter for DataValue type "' + dataValueType + '" is '
+ 'registered already' );
Expand All @@ -107,32 +92,24 @@
* default formatter if no ValueFormatter is registered for that purpose.
* @since 0.1
*
* @param {dataTypes.DataType|string} purpose DataType object or DataValue type.
* @param {string} dataValueType
* @param {string} [dataTypeId]
* @return {Function|null}
*
* @throws {Error} if no data value type is specified.
* @throws {Error} if no proper purpose is provided to retrieve a formatter.
*/
getFormatter: function( purpose ) {
var dataValueType,
dataTypeId,
formatter;

if( purpose instanceof dataTypes.DataType ) {
dataValueType = purpose.getDataValueType();
dataTypeId = purpose.getId();
} else if( typeof purpose === 'string' ) {
dataValueType = purpose;
} else {
throw new Error( 'No sufficient purpose provided for choosing a formatter' );
}
getFormatter: function( dataValueType, dataTypeId ) {
var formatter;

if( dataTypeId ) {
if( typeof dataTypeId === 'string' ) {
formatter = this._formattersForDataTypes[dataTypeId];
}

if( !formatter ) {
// No formatter for specified data type or only DataValue provided.
if( !formatter && typeof dataValueType === 'string' ) {
formatter = this._formattersForDataValueTypes[dataValueType];
} else if( !formatter ) {
throw new Error( 'No sufficient purpose provided for choosing a formatter' );
}

return formatter || this._DefaultFormatter;
Expand All @@ -149,4 +126,4 @@
}
}

}( jQuery, valueFormatters, dataTypes ) );
}( jQuery, valueFormatters ) );
6 changes: 3 additions & 3 deletions js/src/ValueFormatters/mw.ext.valueFormatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

var valueFormatterProvider = new vf.ValueFormatterFactory( vf.NullFormatter );

valueFormatterProvider.registerFormatter(
dv.StringValue.TYPE,
vf.StringFormatter
valueFormatterProvider.registerDataValueFormatter(
vf.StringFormatter,
dv.StringValue.TYPE
);

/**
Expand Down
78 changes: 27 additions & 51 deletions js/src/ValueParsers/ValueParserFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @licence GNU GPL v2+
* @author H. Snater < mediawiki@snater.com >
*/
( function( $, vp, dt ) {
( function( $, vp ) {
'use strict';

/**
Expand Down Expand Up @@ -37,62 +37,47 @@
*/
_parsersForDataValueTypes: null,

/**
* Registers a parser.
* @since 0.1
*
* @param {dataTypes.DataType|string} purpose DataType object or DataValue type.
* @param {Function} Parser ValueParser constructor.
*
* @throws {Error} if the parser constructor is invalid.
* @throws {Error} no proper purpose is provided.
*/
registerParser: function( purpose, Parser ) {
if( !$.isFunction( Parser ) ) {
throw new Error( 'Invalid ValueParser constructor' );
}

if( purpose instanceof dt.DataType ) {
this._registerDataTypeParser( purpose, Parser );
} else if( typeof purpose === 'string' ) {
this._registerDataValueParser( purpose, Parser );
} else {
throw new Error( 'No sufficient purpose provided what to register the parser for' );
}
},

/**
* Registers a parser for a certain data type.
* @since 0.1
*
* @param {dataTypes.DataType} dataType
* @param {Function} Parser
* @param {string} dataTypeId
*
* @throws {Error} if no data type id is specified.
* @throws {Error} if a parser for the specified dataType object is registered already.
*/
_registerDataTypeParser: function( dataType, Parser ) {
registerDataTypeParser: function( Parser, dataTypeId ) {
assertIsValueParserConstructor( Parser );

if( this._parsersForDataTypes[dataType.getId()] ) {
throw new Error( 'Parser for DataType "' + dataType.getId() + '" is registered '
+ 'already' );
if( dataTypeId === undefined ) {
throw new Error( 'No proper data type id provided to register the parser for' );
}

this._parsersForDataTypes[dataType.getId()] = Parser;
if( this._parsersForDataTypes[dataTypeId] ) {
throw new Error( 'Parser for DataType "' + dataTypeId + '" is registered already' );
}

this._parsersForDataTypes[dataTypeId] = Parser;
},

/**
* Registers a parser for a certain data value type.
* @since 0.1
*
* @param {string} dataValueType
* @param {Function} Parser
* @param {string} dataValueType
*
* @throws {Error} if no data value type is specified.
* @throws {Error} if a parser for the specified DataValue type is registered already.
*/
_registerDataValueParser: function( dataValueType, Parser ) {
registerDataValueParser: function( Parser, dataValueType ) {
assertIsValueParserConstructor( Parser );

if( dataValueType === undefined ) {
throw new Error( 'No proper data value type provided to register the parser for' );
}

if( this._parsersForDataValueTypes[dataValueType] ) {
throw new Error( 'Parser for DataValue type "' + dataValueType + '" is registered '
+ 'already' );
Expand All @@ -106,32 +91,23 @@
* parser if no ValueParser is registered for that purpose.
* @since 0.1
*
* @param {dataTypes.DataType|string} purpose DataType object or DataValue type.
* @param {string} dataValueType
* @param {string} [dataTypeId]
* @return {Function|null}
*
* @throws {Error} if no proper purpose is provided to retrieve a parser.
*/
getParser: function( purpose ) {
var dataValueType,
dataTypeId,
parser;

if( purpose instanceof dataTypes.DataType ) {
dataValueType = purpose.getDataValueType();
dataTypeId = purpose.getId();
} else if( typeof purpose === 'string' ) {
dataValueType = purpose;
} else {
throw new Error( 'No sufficient purpose provided for choosing a parser' );
}
getParser: function( dataValueType, dataTypeId ) {
var parser;

if( dataTypeId ) {
if( typeof dataTypeId === 'string' ) {
parser = this._parsersForDataTypes[dataTypeId];
}

if( !parser ) {
// No parser for specified data type or only DataValue provided.
if( !parser && typeof dataValueType === 'string' ) {
parser = this._parsersForDataValueTypes[dataValueType];
} else if( !parser ) {
throw new Error( 'No sufficient purpose provided for choosing a parser' );
}

return parser || this._DefaultParser;
Expand All @@ -148,4 +124,4 @@
}
}

}( jQuery, valueParsers, dataTypes ) );
}( jQuery, valueParsers ) );
24 changes: 12 additions & 12 deletions js/src/ValueParsers/mw.ext.valueParsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@

var valueParserProvider = new vp.ValueParserFactory( vp.NullParser );

valueParserProvider.registerParser(
dv.GlobeCoordinateValue.TYPE,
vp.GlobeCoordinateParser
valueParserProvider.registerDataValueParser(
vp.GlobeCoordinateParser,
dv.GlobeCoordinateValue.TYPE
);

valueParserProvider.registerParser(
dv.QuantityValue.TYPE,
vp.QuantityParser
valueParserProvider.registerDataValueParser(
vp.QuantityParser,
dv.QuantityValue.TYPE
);

valueParserProvider.registerParser(
dv.StringValue.TYPE,
vp.StringParser
valueParserProvider.registerDataValueParser(
vp.StringParser,
dv.StringValue.TYPE
);

valueParserProvider.registerParser(
dv.TimeValue.TYPE,
vp.TimeParser
valueParserProvider.registerDataValueParser(
vp.TimeParser,
dv.TimeValue.TYPE
);

/**
Expand Down
Loading