angular.js/src/formatters.js

215 lines
6.4 KiB
JavaScript
Raw Normal View History

function formatter(format, parse) {return {'format':format, 'parse':parse || format};}
function toString(obj) {
return (isDefined(obj) && obj !== _null) ? "" + obj : obj;
}
var NUMBER = /^\s*[-+]?\d*(\.\d*)?\s*$/;
2010-08-11 19:04:02 +00:00
angularFormatter.noop = formatter(identity, identity);
2010-11-09 04:34:18 +00:00
/**
* @workInProgress
2010-11-09 04:34:18 +00:00
* @ngdoc formatter
* @name angular.formatter.json
*
* @description
* Formats the user input as JSON text.
*
* @returns {?string} A JSON string representation of the model.
2010-11-09 04:34:18 +00:00
*
* @example
* <div ng:init="data={name:'misko', project:'angular'}">
* <input type="text" size='50' name="data" ng:format="json"/>
* <pre>data={{data}}</pre>
* </div>
*
* @scenario
* it('should format json', function(){
* expect(binding('data')).toEqual('data={\n \"name\":\"misko\",\n \"project\":\"angular\"}');
* input('data').enter('{}');
* expect(binding('data')).toEqual('data={\n }');
* });
*/
angularFormatter.json = formatter(toJson, function(value){
return fromJson(value || 'null');
});
2010-11-09 04:34:18 +00:00
/**
* @workInProgress
2010-11-09 04:34:18 +00:00
* @ngdoc formatter
* @name angular.formatter.boolean
*
* @description
* Use boolean formatter if you wish to store the data as boolean.
*
2010-11-17 20:13:04 +00:00
* @returns {boolean} Converts to `true` unless user enters (blank), `f`, `false`, `0`, `no`, `[]`.
2010-11-09 04:34:18 +00:00
*
* @example
* Enter truthy text:
* <input type="text" name="value" ng:format="boolean" value="no"/>
* <input type="checkbox" name="value"/>
* <pre>value={{value}}</pre>
*
* @scenario
* it('should format boolean', function(){
* expect(binding('value')).toEqual('value=false');
* input('value').enter('truthy');
* expect(binding('value')).toEqual('value=true');
* });
*/
2010-08-11 19:04:02 +00:00
angularFormatter['boolean'] = formatter(toString, toBoolean);
2010-11-09 04:34:18 +00:00
/**
* @workInProgress
2010-11-09 04:34:18 +00:00
* @ngdoc formatter
* @name angular.formatter.number
*
* @description
* Use number formatter if you wish to convert the user entered string to a number.
*
2010-11-17 20:13:04 +00:00
* @returns {number} Number from the parsed string.
2010-11-09 04:34:18 +00:00
*
* @example
* Enter valid number:
* <input type="text" name="value" ng:format="number" value="1234"/>
* <pre>value={{value}}</pre>
*
* @scenario
* it('should format numbers', function(){
* expect(binding('value')).toEqual('value=1234');
* input('value').enter('5678');
* expect(binding('value')).toEqual('value=5678');
* });
*/
2010-08-11 19:04:02 +00:00
angularFormatter.number = formatter(toString, function(obj){
if (obj == _null || NUMBER.exec(obj)) {
return obj===_null || obj === '' ? _null : 1*obj;
} else {
throw "Not a number";
2010-08-11 19:04:02 +00:00
}
});
2010-01-29 06:10:49 +00:00
2010-11-09 04:34:18 +00:00
/**
* @workInProgress
2010-11-09 04:34:18 +00:00
* @ngdoc formatter
* @name angular.formatter.list
*
* @description
2010-11-17 20:13:04 +00:00
* Use list formatter if you wish to convert the user entered string to an array.
2010-11-09 04:34:18 +00:00
*
2010-11-17 20:13:04 +00:00
* @returns {Array} Array parsed from the entered string.
2010-11-09 04:34:18 +00:00
*
* @example
* Enter a list of items:
* <input type="text" name="value" ng:format="list" value=" chair ,, table"/>
* <input type="text" name="value" ng:format="list"/>
* <pre>value={{value}}</pre>
*
* @scenario
* it('should format lists', function(){
* expect(binding('value')).toEqual('value=["chair","table"]');
* this.addFutureAction('change to XYZ', function($window, $document, done){
* $document.elements('.doc-example :input:last').val(',,a,b,').trigger('change');
2010-11-09 04:34:18 +00:00
* done();
* });
* expect(binding('value')).toEqual('value=["a","b"]');
* });
*/
2010-08-11 19:04:02 +00:00
angularFormatter.list = formatter(
function(obj) { return obj ? obj.join(", ") : obj; },
function(value) {
var list = [];
forEach((value || '').split(','), function(item){
2010-08-11 19:04:02 +00:00
item = trim(item);
if (item) list.push(item);
});
return list;
}
);
2010-11-09 04:34:18 +00:00
/**
* @workInProgress
2010-11-09 04:34:18 +00:00
* @ngdoc formatter
* @name angular.formatter.trim
*
* @description
* Use trim formatter if you wish to trim extra spaces in user text.
*
* @returns {String} Trim excess leading and trailing space.
*
* @example
* Enter text with leading/trailing spaces:
* <input type="text" name="value" ng:format="trim" value=" book "/>
* <input type="text" name="value" ng:format="trim"/>
* <pre>value={{value|json}}</pre>
*
* @scenario
* it('should format trim', function(){
* expect(binding('value')).toEqual('value="book"');
* this.addFutureAction('change to XYZ', function($window, $document, done){
* $document.elements('.doc-example :input:last').val(' text ').trigger('change');
2010-11-09 04:34:18 +00:00
* done();
* });
* expect(binding('value')).toEqual('value="text"');
* });
*/
2010-08-11 19:04:02 +00:00
angularFormatter.trim = formatter(
function(obj) { return obj ? trim("" + obj) : ""; }
);
/**
* @workInProgress
* @ngdoc formatter
* @name angular.formatter.index
* @description
* Index formatter is meant to be used with `select` input widget. It is useful when one needs
* to select from a set of objects. To create pull-down one can iterate over the array of object
* to build the UI. However the value of the pull-down must be a string. This means that when on
* object is selected form the pull-down, the pull-down value is a string which needs to be
* converted back to an object. This conversion from string to on object is not possible, at best
* the converted object is a copy of the original object. To solve this issue we create a pull-down
* where the value strings are an index of the object in the array. When pull-down is selected the
* index can be used to look up the original user object.
*
* @inputType select
* @param {array} array to be used for selecting an object.
* @returns {object} object which is located at the selected position.
*
* @example
* <script>
* function DemoCntl(){
* this.users = [
* {name:'guest', password:'guest'},
* {name:'user', password:'123'},
* {name:'admin', password:'abc'}
* ];
* }
* </script>
* <div ng:controller="DemoCntl">
* User:
* <select name="currentUser" ng:format="index:users">
* <option ng:repeat="user in users" value="{{$index}}">{{user.name}}</option>
* </select>
* <select name="currentUser" ng:format="index:users">
* <option ng:repeat="user in users" value="{{$index}}">{{user.name}}</option>
* </select>
* user={{currentUser.name}}<br/>
* password={{currentUser.password}}<br/>
* </div>
*
* @scenario
* it('should retrieve object by index', function(){
* expect(binding('currentUser.password')).toEqual('guest');
* select('currentUser').option('2');
* expect(binding('currentUser.password')).toEqual('abc');
* });
*/
angularFormatter.index = formatter(
function(object, array){
return '' + indexOf(array || [], object);
},
function(index, array){
return (array||[])[index];
}
);