[js-client] Added javascript account client
This commit is contained in:
parent
8bd5c36267
commit
377c1567ab
13 changed files with 41128 additions and 0 deletions
13
LICENSE
13
LICENSE
|
@ -46,6 +46,19 @@ are from the python 3 sphinx documentation source code and are licensed under
|
||||||
the PSF. For the full license, see the file PSF-PY3.3-LICENSE.
|
the PSF. For the full license, see the file PSF-PY3.3-LICENSE.
|
||||||
|
|
||||||
|
|
||||||
|
Bootstrap, AngularJS and jQuery
|
||||||
|
-------------------------------
|
||||||
|
|
||||||
|
These JavaScript libraries are used by the 'Accounting Client' included in this
|
||||||
|
project. The libraries are located in
|
||||||
|
|
||||||
|
accounting/static/js/lib/*
|
||||||
|
accounting/static/css/lib/*
|
||||||
|
|
||||||
|
(asterisk '*' indicates wildcard substitution) and the first lines of each file
|
||||||
|
include the license information for each file.
|
||||||
|
|
||||||
|
|
||||||
Copyright Inventory
|
Copyright Inventory
|
||||||
===================
|
===================
|
||||||
|
|
||||||
|
|
7118
accounting/static/css/lib/bootstrap-3.0.3.css
vendored
Normal file
7118
accounting/static/css/lib/bootstrap-3.0.3.css
vendored
Normal file
File diff suppressed because it is too large
Load diff
152
accounting/static/js/accounting.js
Normal file
152
accounting/static/js/accounting.js
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
'use strict';
|
||||||
|
// Part of the accounting-api project:
|
||||||
|
// https://gitorious.org/conservancy/accounting-api
|
||||||
|
// License: AGPLv3-or-later
|
||||||
|
|
||||||
|
function s4() {
|
||||||
|
return Math.floor((1 + Math.random()) * 0x10000)
|
||||||
|
.toString(16)
|
||||||
|
.substring(1);
|
||||||
|
};
|
||||||
|
|
||||||
|
function guid() {
|
||||||
|
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
|
||||||
|
s4() + '-' + s4() + s4() + s4();
|
||||||
|
}
|
||||||
|
|
||||||
|
function Transaction(args) {
|
||||||
|
var self = this;
|
||||||
|
this.__type__ = 'Transaction';
|
||||||
|
this.id = null;
|
||||||
|
this.payee = null;
|
||||||
|
this.date = null;
|
||||||
|
this.postings = null;
|
||||||
|
this.metadata = {};
|
||||||
|
|
||||||
|
this.generateId = function () {
|
||||||
|
self.id = guid();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i in args) {
|
||||||
|
this[i] = args[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Posting(args) {
|
||||||
|
this.__type__ = 'Posting';
|
||||||
|
this.account = null;
|
||||||
|
this.amount = null;
|
||||||
|
|
||||||
|
for (var i in args) {
|
||||||
|
this[i] = args[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Amount(args) {
|
||||||
|
this.__type__ = 'Amount';
|
||||||
|
this.symbol = null;
|
||||||
|
this.amount = null;
|
||||||
|
|
||||||
|
for (var i in args) {
|
||||||
|
this[i] = args[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var models = {
|
||||||
|
'Amount': Amount,
|
||||||
|
'Posting': Posting,
|
||||||
|
'Transaction': Transaction
|
||||||
|
};
|
||||||
|
|
||||||
|
function accountingObjectHook(object) {
|
||||||
|
if ('__type__' in object) {
|
||||||
|
console.log('Found typed object: ', object.__type__, object);
|
||||||
|
var model = new models[object.__type__];
|
||||||
|
for (var i in object) {
|
||||||
|
model[i] = object[i];
|
||||||
|
}
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
function transformResponseFactory(object_hook) {
|
||||||
|
var self = this;
|
||||||
|
self.object_hook = object_hook;
|
||||||
|
|
||||||
|
function transformResponse(json, headers) {
|
||||||
|
var transformed;
|
||||||
|
console.log('json: ', json);
|
||||||
|
if (typeof json == 'undefined') {
|
||||||
|
return;
|
||||||
|
} else if ((typeof json == 'object') &&
|
||||||
|
Object.prototype.toString.call(json) == '[object Object]') {
|
||||||
|
console.log('json is Object, json: ', json);
|
||||||
|
for (var i in json) {
|
||||||
|
json[i] = transformResponse(json[i]);
|
||||||
|
}
|
||||||
|
transformed = self.object_hook(json)
|
||||||
|
} else if ((typeof json == 'object') &&
|
||||||
|
Object.prototype.toString.call(json) == '[object Array]') {
|
||||||
|
console.log('json is Array, json: ', json);
|
||||||
|
for (var i in json) {
|
||||||
|
json[i] = transformResponse(json[i]);
|
||||||
|
}
|
||||||
|
transformed = json;
|
||||||
|
} else {
|
||||||
|
transformed = json;
|
||||||
|
}
|
||||||
|
return transformed;
|
||||||
|
};
|
||||||
|
return transformResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
function transformRequestFactory(object_hook) {
|
||||||
|
function transformRequest(data, headerSetter) {
|
||||||
|
console.log('transformRequest', data, headerSetter)
|
||||||
|
headerSetter()['Content-Type'] = 'application/json';
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
return transformRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
angular.module('accountingApi', ['ngResource'])
|
||||||
|
.factory('Transaction', function () {
|
||||||
|
return Transaction;
|
||||||
|
})
|
||||||
|
.factory('Posting', function () {
|
||||||
|
return Posting;
|
||||||
|
})
|
||||||
|
.factory('Amount', function () {
|
||||||
|
return Amount;
|
||||||
|
})
|
||||||
|
.factory('AccountingApi', function($resource, $http) {
|
||||||
|
$http.defaults.useXDomain = true;
|
||||||
|
$http.defaults.transformResponse.push(new transformResponseFactory(
|
||||||
|
accountingObjectHook));
|
||||||
|
var AccountingApi = $resource(
|
||||||
|
'/transaction/:id',
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
get: {
|
||||||
|
method: 'GET',
|
||||||
|
transformResponse: Array.prototype.concat(
|
||||||
|
$http.defaults.transformResponse,
|
||||||
|
[function (data){
|
||||||
|
if (typeof data == 'object' &&
|
||||||
|
'transaction' in data) {
|
||||||
|
return data.transaction;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}]
|
||||||
|
)
|
||||||
|
},
|
||||||
|
save: {
|
||||||
|
method: 'POST',
|
||||||
|
transformRequest: [new transformRequestFactory()].concat(
|
||||||
|
$http.defaults.transformRequest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return AccountingApi;
|
||||||
|
});
|
67
accounting/static/js/client.js
Normal file
67
accounting/static/js/client.js
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
angular.module('accountingClient', ['ngRoute', 'accountingApi'])
|
||||||
|
.config(function($routeProvider) {
|
||||||
|
$routeProvider
|
||||||
|
.when('/', {
|
||||||
|
controller: 'ListTransactionsCtrl',
|
||||||
|
templateUrl: '/static/templates/list-transactions.html'
|
||||||
|
})
|
||||||
|
.when('/show/:transactionId', {
|
||||||
|
controller: 'TransactionDetailCtrl',
|
||||||
|
templateUrl: '/static/templates/detail.html'
|
||||||
|
})
|
||||||
|
.when('/edit/:transactionId', {
|
||||||
|
controller: 'EditTransactionCtrl',
|
||||||
|
templateUrl: '/static/templates/edit.html'
|
||||||
|
})
|
||||||
|
.when('/new', {
|
||||||
|
controller: 'CreateTransactionCtrl',
|
||||||
|
templateUrl: '/static/templates/edit.html'
|
||||||
|
})
|
||||||
|
.otherwise({
|
||||||
|
redirectTo: '/'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.controller('ListTransactionsCtrl', function ($scope, AccountingApi) {
|
||||||
|
var request = AccountingApi.get();
|
||||||
|
console.log('request: ', request)
|
||||||
|
$scope.request = request
|
||||||
|
console.log('$scope.request', $scope.request)
|
||||||
|
})
|
||||||
|
.controller('TransactionDetailCtrl', function ($scope, $routeParams,
|
||||||
|
AccountingApi) {
|
||||||
|
$scope.transaction = AccountingApi.get({
|
||||||
|
id: $routeParams.transactionId
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.controller('CreateTransactionCtrl', function ($scope, $location, $timeout,
|
||||||
|
AccountingApi, Transaction,
|
||||||
|
Posting, Amount) {
|
||||||
|
$scope.transaction = new Transaction({
|
||||||
|
postings: [
|
||||||
|
new Posting({
|
||||||
|
amount: new Amount({symbol: 'USD'})
|
||||||
|
}),
|
||||||
|
new Posting({
|
||||||
|
amount: new Amount({symbol: 'USD'})
|
||||||
|
})
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
$scope.addPosting = function(e) {
|
||||||
|
$scope.transaction.postings.push(
|
||||||
|
new Posting({
|
||||||
|
amount: new Amount({symbol: 'USD'})
|
||||||
|
})
|
||||||
|
);
|
||||||
|
e.stopPropagation();
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
$scope.save = function () {
|
||||||
|
console.log($scope.transaction);
|
||||||
|
AccountingApi.save($scope.transaction, function () {
|
||||||
|
$timeout(function () { $location.path('/'); });
|
||||||
|
});
|
||||||
|
};
|
||||||
|
})
|
20369
accounting/static/js/lib/angular-1.2.5.js
Normal file
20369
accounting/static/js/lib/angular-1.2.5.js
Normal file
File diff suppressed because it is too large
Load diff
565
accounting/static/js/lib/angular-resource-1.2.5.js
Normal file
565
accounting/static/js/lib/angular-resource-1.2.5.js
Normal file
|
@ -0,0 +1,565 @@
|
||||||
|
/**
|
||||||
|
* @license AngularJS v1.2.5
|
||||||
|
* (c) 2010-2014 Google, Inc. http://angularjs.org
|
||||||
|
* License: MIT
|
||||||
|
*/
|
||||||
|
(function(window, angular, undefined) {'use strict';
|
||||||
|
|
||||||
|
var $resourceMinErr = angular.$$minErr('$resource');
|
||||||
|
|
||||||
|
// Helper functions and regex to lookup a dotted path on an object
|
||||||
|
// stopping at undefined/null. The path must be composed of ASCII
|
||||||
|
// identifiers (just like $parse)
|
||||||
|
var MEMBER_NAME_REGEX = /^(\.[a-zA-Z_$][0-9a-zA-Z_$]*)+$/;
|
||||||
|
|
||||||
|
function isValidDottedPath(path) {
|
||||||
|
return (path != null && path !== '' && path !== 'hasOwnProperty' &&
|
||||||
|
MEMBER_NAME_REGEX.test('.' + path));
|
||||||
|
}
|
||||||
|
|
||||||
|
function lookupDottedPath(obj, path) {
|
||||||
|
if (!isValidDottedPath(path)) {
|
||||||
|
throw $resourceMinErr('badmember', 'Dotted member path "@{0}" is invalid.', path);
|
||||||
|
}
|
||||||
|
var keys = path.split('.');
|
||||||
|
for (var i = 0, ii = keys.length; i < ii && obj !== undefined; i++) {
|
||||||
|
var key = keys[i];
|
||||||
|
obj = (obj !== null) ? obj[key] : undefined;
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a shallow copy of an object and clear other fields from the destination
|
||||||
|
*/
|
||||||
|
function shallowClearAndCopy(src, dst) {
|
||||||
|
dst = dst || {};
|
||||||
|
|
||||||
|
angular.forEach(dst, function(value, key){
|
||||||
|
delete dst[key];
|
||||||
|
});
|
||||||
|
|
||||||
|
for (var key in src) {
|
||||||
|
if (src.hasOwnProperty(key) && key.substr(0, 2) !== '$$') {
|
||||||
|
dst[key] = src[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc overview
|
||||||
|
* @name ngResource
|
||||||
|
* @description
|
||||||
|
*
|
||||||
|
* # ngResource
|
||||||
|
*
|
||||||
|
* The `ngResource` module provides interaction support with RESTful services
|
||||||
|
* via the $resource service.
|
||||||
|
*
|
||||||
|
* {@installModule resource}
|
||||||
|
*
|
||||||
|
* <div doc-module-components="ngResource"></div>
|
||||||
|
*
|
||||||
|
* See {@link ngResource.$resource `$resource`} for usage.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc object
|
||||||
|
* @name ngResource.$resource
|
||||||
|
* @requires $http
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* A factory which creates a resource object that lets you interact with
|
||||||
|
* [RESTful](http://en.wikipedia.org/wiki/Representational_State_Transfer) server-side data sources.
|
||||||
|
*
|
||||||
|
* The returned resource object has action methods which provide high-level behaviors without
|
||||||
|
* the need to interact with the low level {@link ng.$http $http} service.
|
||||||
|
*
|
||||||
|
* Requires the {@link ngResource `ngResource`} module to be installed.
|
||||||
|
*
|
||||||
|
* @param {string} url A parametrized URL template with parameters prefixed by `:` as in
|
||||||
|
* `/user/:username`. If you are using a URL with a port number (e.g.
|
||||||
|
* `http://example.com:8080/api`), it will be respected.
|
||||||
|
*
|
||||||
|
* If you are using a url with a suffix, just add the suffix, like this:
|
||||||
|
* `$resource('http://example.com/resource.json')` or `$resource('http://example.com/:id.json')`
|
||||||
|
* or even `$resource('http://example.com/resource/:resource_id.:format')`
|
||||||
|
* If the parameter before the suffix is empty, :resource_id in this case, then the `/.` will be
|
||||||
|
* collapsed down to a single `.`. If you need this sequence to appear and not collapse then you
|
||||||
|
* can escape it with `/\.`.
|
||||||
|
*
|
||||||
|
* @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in
|
||||||
|
* `actions` methods. If any of the parameter value is a function, it will be executed every time
|
||||||
|
* when a param value needs to be obtained for a request (unless the param was overridden).
|
||||||
|
*
|
||||||
|
* Each key value in the parameter object is first bound to url template if present and then any
|
||||||
|
* excess keys are appended to the url search query after the `?`.
|
||||||
|
*
|
||||||
|
* Given a template `/path/:verb` and parameter `{verb:'greet', salutation:'Hello'}` results in
|
||||||
|
* URL `/path/greet?salutation=Hello`.
|
||||||
|
*
|
||||||
|
* If the parameter value is prefixed with `@` then the value of that parameter is extracted from
|
||||||
|
* the data object (useful for non-GET operations).
|
||||||
|
*
|
||||||
|
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
|
||||||
|
* default set of resource actions. The declaration should be created in the format of {@link
|
||||||
|
* ng.$http#usage_parameters $http.config}:
|
||||||
|
*
|
||||||
|
* {action1: {method:?, params:?, isArray:?, headers:?, ...},
|
||||||
|
* action2: {method:?, params:?, isArray:?, headers:?, ...},
|
||||||
|
* ...}
|
||||||
|
*
|
||||||
|
* Where:
|
||||||
|
*
|
||||||
|
* - **`action`** – {string} – The name of action. This name becomes the name of the method on
|
||||||
|
* your resource object.
|
||||||
|
* - **`method`** – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`,
|
||||||
|
* `DELETE`, and `JSONP`.
|
||||||
|
* - **`params`** – {Object=} – Optional set of pre-bound parameters for this action. If any of
|
||||||
|
* the parameter value is a function, it will be executed every time when a param value needs to
|
||||||
|
* be obtained for a request (unless the param was overridden).
|
||||||
|
* - **`url`** – {string} – action specific `url` override. The url templating is supported just
|
||||||
|
* like for the resource-level urls.
|
||||||
|
* - **`isArray`** – {boolean=} – If true then the returned object for this action is an array,
|
||||||
|
* see `returns` section.
|
||||||
|
* - **`transformRequest`** –
|
||||||
|
* `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
|
||||||
|
* transform function or an array of such functions. The transform function takes the http
|
||||||
|
* request body and headers and returns its transformed (typically serialized) version.
|
||||||
|
* - **`transformResponse`** –
|
||||||
|
* `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
|
||||||
|
* transform function or an array of such functions. The transform function takes the http
|
||||||
|
* response body and headers and returns its transformed (typically deserialized) version.
|
||||||
|
* - **`cache`** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the
|
||||||
|
* GET request, otherwise if a cache instance built with
|
||||||
|
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
|
||||||
|
* caching.
|
||||||
|
* - **`timeout`** – `{number|Promise}` – timeout in milliseconds, or {@link ng.$q promise} that
|
||||||
|
* should abort the request when resolved.
|
||||||
|
* - **`withCredentials`** - `{boolean}` - whether to set the `withCredentials` flag on the
|
||||||
|
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
|
||||||
|
* requests with credentials} for more information.
|
||||||
|
* - **`responseType`** - `{string}` - see {@link
|
||||||
|
* https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType requestType}.
|
||||||
|
* - **`interceptor`** - `{Object=}` - The interceptor object has two optional methods -
|
||||||
|
* `response` and `responseError`. Both `response` and `responseError` interceptors get called
|
||||||
|
* with `http response` object. See {@link ng.$http $http interceptors}.
|
||||||
|
*
|
||||||
|
* @returns {Object} A resource "class" object with methods for the default set of resource actions
|
||||||
|
* optionally extended with custom `actions`. The default set contains these actions:
|
||||||
|
*
|
||||||
|
* { 'get': {method:'GET'},
|
||||||
|
* 'save': {method:'POST'},
|
||||||
|
* 'query': {method:'GET', isArray:true},
|
||||||
|
* 'remove': {method:'DELETE'},
|
||||||
|
* 'delete': {method:'DELETE'} };
|
||||||
|
*
|
||||||
|
* Calling these methods invoke an {@link ng.$http} with the specified http method,
|
||||||
|
* destination and parameters. When the data is returned from the server then the object is an
|
||||||
|
* instance of the resource class. The actions `save`, `remove` and `delete` are available on it
|
||||||
|
* as methods with the `$` prefix. This allows you to easily perform CRUD operations (create,
|
||||||
|
* read, update, delete) on server-side data like this:
|
||||||
|
* <pre>
|
||||||
|
var User = $resource('/user/:userId', {userId:'@id'});
|
||||||
|
var user = User.get({userId:123}, function() {
|
||||||
|
user.abc = true;
|
||||||
|
user.$save();
|
||||||
|
});
|
||||||
|
</pre>
|
||||||
|
*
|
||||||
|
* It is important to realize that invoking a $resource object method immediately returns an
|
||||||
|
* empty reference (object or array depending on `isArray`). Once the data is returned from the
|
||||||
|
* server the existing reference is populated with the actual data. This is a useful trick since
|
||||||
|
* usually the resource is assigned to a model which is then rendered by the view. Having an empty
|
||||||
|
* object results in no rendering, once the data arrives from the server then the object is
|
||||||
|
* populated with the data and the view automatically re-renders itself showing the new data. This
|
||||||
|
* means that in most cases one never has to write a callback function for the action methods.
|
||||||
|
*
|
||||||
|
* The action methods on the class object or instance object can be invoked with the following
|
||||||
|
* parameters:
|
||||||
|
*
|
||||||
|
* - HTTP GET "class" actions: `Resource.action([parameters], [success], [error])`
|
||||||
|
* - non-GET "class" actions: `Resource.action([parameters], postData, [success], [error])`
|
||||||
|
* - non-GET instance actions: `instance.$action([parameters], [success], [error])`
|
||||||
|
*
|
||||||
|
* Success callback is called with (value, responseHeaders) arguments. Error callback is called
|
||||||
|
* with (httpResponse) argument.
|
||||||
|
*
|
||||||
|
* Class actions return empty instance (with additional properties below).
|
||||||
|
* Instance actions return promise of the action.
|
||||||
|
*
|
||||||
|
* The Resource instances and collection have these additional properties:
|
||||||
|
*
|
||||||
|
* - `$promise`: the {@link ng.$q promise} of the original server interaction that created this
|
||||||
|
* instance or collection.
|
||||||
|
*
|
||||||
|
* On success, the promise is resolved with the same resource instance or collection object,
|
||||||
|
* updated with data from server. This makes it easy to use in
|
||||||
|
* {@link ngRoute.$routeProvider resolve section of $routeProvider.when()} to defer view
|
||||||
|
* rendering until the resource(s) are loaded.
|
||||||
|
*
|
||||||
|
* On failure, the promise is resolved with the {@link ng.$http http response} object, without
|
||||||
|
* the `resource` property.
|
||||||
|
*
|
||||||
|
* - `$resolved`: `true` after first server interaction is completed (either with success or
|
||||||
|
* rejection), `false` before that. Knowing if the Resource has been resolved is useful in
|
||||||
|
* data-binding.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* # Credit card resource
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
// Define CreditCard class
|
||||||
|
var CreditCard = $resource('/user/:userId/card/:cardId',
|
||||||
|
{userId:123, cardId:'@id'}, {
|
||||||
|
charge: {method:'POST', params:{charge:true}}
|
||||||
|
});
|
||||||
|
|
||||||
|
// We can retrieve a collection from the server
|
||||||
|
var cards = CreditCard.query(function() {
|
||||||
|
// GET: /user/123/card
|
||||||
|
// server returns: [ {id:456, number:'1234', name:'Smith'} ];
|
||||||
|
|
||||||
|
var card = cards[0];
|
||||||
|
// each item is an instance of CreditCard
|
||||||
|
expect(card instanceof CreditCard).toEqual(true);
|
||||||
|
card.name = "J. Smith";
|
||||||
|
// non GET methods are mapped onto the instances
|
||||||
|
card.$save();
|
||||||
|
// POST: /user/123/card/456 {id:456, number:'1234', name:'J. Smith'}
|
||||||
|
// server returns: {id:456, number:'1234', name: 'J. Smith'};
|
||||||
|
|
||||||
|
// our custom method is mapped as well.
|
||||||
|
card.$charge({amount:9.99});
|
||||||
|
// POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'}
|
||||||
|
});
|
||||||
|
|
||||||
|
// we can create an instance as well
|
||||||
|
var newCard = new CreditCard({number:'0123'});
|
||||||
|
newCard.name = "Mike Smith";
|
||||||
|
newCard.$save();
|
||||||
|
// POST: /user/123/card {number:'0123', name:'Mike Smith'}
|
||||||
|
// server returns: {id:789, number:'01234', name: 'Mike Smith'};
|
||||||
|
expect(newCard.id).toEqual(789);
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* The object returned from this function execution is a resource "class" which has "static" method
|
||||||
|
* for each action in the definition.
|
||||||
|
*
|
||||||
|
* Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and
|
||||||
|
* `headers`.
|
||||||
|
* When the data is returned from the server then the object is an instance of the resource type and
|
||||||
|
* all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD
|
||||||
|
* operations (create, read, update, delete) on server-side data.
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
var User = $resource('/user/:userId', {userId:'@id'});
|
||||||
|
var user = User.get({userId:123}, function() {
|
||||||
|
user.abc = true;
|
||||||
|
user.$save();
|
||||||
|
});
|
||||||
|
</pre>
|
||||||
|
*
|
||||||
|
* It's worth noting that the success callback for `get`, `query` and other methods gets passed
|
||||||
|
* in the response that came from the server as well as $http header getter function, so one
|
||||||
|
* could rewrite the above example and get access to http headers as:
|
||||||
|
*
|
||||||
|
<pre>
|
||||||
|
var User = $resource('/user/:userId', {userId:'@id'});
|
||||||
|
User.get({userId:123}, function(u, getResponseHeaders){
|
||||||
|
u.abc = true;
|
||||||
|
u.$save(function(u, putResponseHeaders) {
|
||||||
|
//u => saved user object
|
||||||
|
//putResponseHeaders => $http header getter
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</pre>
|
||||||
|
*/
|
||||||
|
angular.module('ngResource', ['ng']).
|
||||||
|
factory('$resource', ['$http', '$q', function($http, $q) {
|
||||||
|
|
||||||
|
var DEFAULT_ACTIONS = {
|
||||||
|
'get': {method:'GET'},
|
||||||
|
'save': {method:'POST'},
|
||||||
|
'query': {method:'GET', isArray:true},
|
||||||
|
'remove': {method:'DELETE'},
|
||||||
|
'delete': {method:'DELETE'}
|
||||||
|
};
|
||||||
|
var noop = angular.noop,
|
||||||
|
forEach = angular.forEach,
|
||||||
|
extend = angular.extend,
|
||||||
|
copy = angular.copy,
|
||||||
|
isFunction = angular.isFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We need our custom method because encodeURIComponent is too aggressive and doesn't follow
|
||||||
|
* http://www.ietf.org/rfc/rfc3986.txt with regards to the character set (pchar) allowed in path
|
||||||
|
* segments:
|
||||||
|
* segment = *pchar
|
||||||
|
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
|
||||||
|
* pct-encoded = "%" HEXDIG HEXDIG
|
||||||
|
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
||||||
|
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
||||||
|
* / "*" / "+" / "," / ";" / "="
|
||||||
|
*/
|
||||||
|
function encodeUriSegment(val) {
|
||||||
|
return encodeUriQuery(val, true).
|
||||||
|
replace(/%26/gi, '&').
|
||||||
|
replace(/%3D/gi, '=').
|
||||||
|
replace(/%2B/gi, '+');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is intended for encoding *key* or *value* parts of query component. We need a
|
||||||
|
* custom method because encodeURIComponent is too aggressive and encodes stuff that doesn't
|
||||||
|
* have to be encoded per http://tools.ietf.org/html/rfc3986:
|
||||||
|
* query = *( pchar / "/" / "?" )
|
||||||
|
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
|
||||||
|
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
||||||
|
* pct-encoded = "%" HEXDIG HEXDIG
|
||||||
|
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
||||||
|
* / "*" / "+" / "," / ";" / "="
|
||||||
|
*/
|
||||||
|
function encodeUriQuery(val, pctEncodeSpaces) {
|
||||||
|
return encodeURIComponent(val).
|
||||||
|
replace(/%40/gi, '@').
|
||||||
|
replace(/%3A/gi, ':').
|
||||||
|
replace(/%24/g, '$').
|
||||||
|
replace(/%2C/gi, ',').
|
||||||
|
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function Route(template, defaults) {
|
||||||
|
this.template = template;
|
||||||
|
this.defaults = defaults || {};
|
||||||
|
this.urlParams = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Route.prototype = {
|
||||||
|
setUrlParams: function(config, params, actionUrl) {
|
||||||
|
var self = this,
|
||||||
|
url = actionUrl || self.template,
|
||||||
|
val,
|
||||||
|
encodedVal;
|
||||||
|
|
||||||
|
var urlParams = self.urlParams = {};
|
||||||
|
forEach(url.split(/\W/), function(param){
|
||||||
|
if (param === 'hasOwnProperty') {
|
||||||
|
throw $resourceMinErr('badname', "hasOwnProperty is not a valid parameter name.");
|
||||||
|
}
|
||||||
|
if (!(new RegExp("^\\d+$").test(param)) && param &&
|
||||||
|
(new RegExp("(^|[^\\\\]):" + param + "(\\W|$)").test(url))) {
|
||||||
|
urlParams[param] = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
url = url.replace(/\\:/g, ':');
|
||||||
|
|
||||||
|
params = params || {};
|
||||||
|
forEach(self.urlParams, function(_, urlParam){
|
||||||
|
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
|
||||||
|
if (angular.isDefined(val) && val !== null) {
|
||||||
|
encodedVal = encodeUriSegment(val);
|
||||||
|
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), encodedVal + "$1");
|
||||||
|
} else {
|
||||||
|
url = url.replace(new RegExp("(\/?):" + urlParam + "(\\W|$)", "g"), function(match,
|
||||||
|
leadingSlashes, tail) {
|
||||||
|
if (tail.charAt(0) == '/') {
|
||||||
|
return tail;
|
||||||
|
} else {
|
||||||
|
return leadingSlashes + tail;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// strip trailing slashes and set the url
|
||||||
|
url = url.replace(/\/+$/, '');
|
||||||
|
// then replace collapse `/.` if found in the last URL path segment before the query
|
||||||
|
// E.g. `http://url.com/id./format?q=x` becomes `http://url.com/id.format?q=x`
|
||||||
|
url = url.replace(/\/\.(?=\w+($|\?))/, '.');
|
||||||
|
// replace escaped `/\.` with `/.`
|
||||||
|
config.url = url.replace(/\/\\\./, '/.');
|
||||||
|
|
||||||
|
|
||||||
|
// set params - delegate param encoding to $http
|
||||||
|
forEach(params, function(value, key){
|
||||||
|
if (!self.urlParams[key]) {
|
||||||
|
config.params = config.params || {};
|
||||||
|
config.params[key] = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function resourceFactory(url, paramDefaults, actions) {
|
||||||
|
var route = new Route(url);
|
||||||
|
|
||||||
|
actions = extend({}, DEFAULT_ACTIONS, actions);
|
||||||
|
|
||||||
|
function extractParams(data, actionParams){
|
||||||
|
var ids = {};
|
||||||
|
actionParams = extend({}, paramDefaults, actionParams);
|
||||||
|
forEach(actionParams, function(value, key){
|
||||||
|
if (isFunction(value)) { value = value(); }
|
||||||
|
ids[key] = value && value.charAt && value.charAt(0) == '@' ?
|
||||||
|
lookupDottedPath(data, value.substr(1)) : value;
|
||||||
|
});
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
function defaultResponseInterceptor(response) {
|
||||||
|
return response.resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
function Resource(value){
|
||||||
|
shallowClearAndCopy(value || {}, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
forEach(actions, function(action, name) {
|
||||||
|
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method);
|
||||||
|
|
||||||
|
Resource[name] = function(a1, a2, a3, a4) {
|
||||||
|
var params = {}, data, success, error;
|
||||||
|
|
||||||
|
/* jshint -W086 */ /* (purposefully fall through case statements) */
|
||||||
|
switch(arguments.length) {
|
||||||
|
case 4:
|
||||||
|
error = a4;
|
||||||
|
success = a3;
|
||||||
|
//fallthrough
|
||||||
|
case 3:
|
||||||
|
case 2:
|
||||||
|
if (isFunction(a2)) {
|
||||||
|
if (isFunction(a1)) {
|
||||||
|
success = a1;
|
||||||
|
error = a2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
success = a2;
|
||||||
|
error = a3;
|
||||||
|
//fallthrough
|
||||||
|
} else {
|
||||||
|
params = a1;
|
||||||
|
data = a2;
|
||||||
|
success = a3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (isFunction(a1)) success = a1;
|
||||||
|
else if (hasBody) data = a1;
|
||||||
|
else params = a1;
|
||||||
|
break;
|
||||||
|
case 0: break;
|
||||||
|
default:
|
||||||
|
throw $resourceMinErr('badargs',
|
||||||
|
"Expected up to 4 arguments [params, data, success, error], got {0} arguments",
|
||||||
|
arguments.length);
|
||||||
|
}
|
||||||
|
/* jshint +W086 */ /* (purposefully fall through case statements) */
|
||||||
|
|
||||||
|
var isInstanceCall = this instanceof Resource;
|
||||||
|
var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data));
|
||||||
|
var httpConfig = {};
|
||||||
|
var responseInterceptor = action.interceptor && action.interceptor.response ||
|
||||||
|
defaultResponseInterceptor;
|
||||||
|
var responseErrorInterceptor = action.interceptor && action.interceptor.responseError ||
|
||||||
|
undefined;
|
||||||
|
|
||||||
|
forEach(action, function(value, key) {
|
||||||
|
if (key != 'params' && key != 'isArray' && key != 'interceptor') {
|
||||||
|
httpConfig[key] = copy(value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (hasBody) httpConfig.data = data;
|
||||||
|
route.setUrlParams(httpConfig,
|
||||||
|
extend({}, extractParams(data, action.params || {}), params),
|
||||||
|
action.url);
|
||||||
|
|
||||||
|
var promise = $http(httpConfig).then(function(response) {
|
||||||
|
var data = response.data,
|
||||||
|
promise = value.$promise;
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
// Need to convert action.isArray to boolean in case it is undefined
|
||||||
|
// jshint -W018
|
||||||
|
if (angular.isArray(data) !== (!!action.isArray)) {
|
||||||
|
throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' +
|
||||||
|
'response to contain an {0} but got an {1}',
|
||||||
|
action.isArray?'array':'object', angular.isArray(data)?'array':'object');
|
||||||
|
}
|
||||||
|
// jshint +W018
|
||||||
|
if (action.isArray) {
|
||||||
|
value.length = 0;
|
||||||
|
forEach(data, function(item) {
|
||||||
|
value.push(new Resource(item));
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
shallowClearAndCopy(data, value);
|
||||||
|
value.$promise = promise;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
value.$resolved = true;
|
||||||
|
|
||||||
|
response.resource = value;
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}, function(response) {
|
||||||
|
value.$resolved = true;
|
||||||
|
|
||||||
|
(error||noop)(response);
|
||||||
|
|
||||||
|
return $q.reject(response);
|
||||||
|
});
|
||||||
|
|
||||||
|
promise = promise.then(
|
||||||
|
function(response) {
|
||||||
|
var value = responseInterceptor(response);
|
||||||
|
(success||noop)(value, response.headers);
|
||||||
|
return value;
|
||||||
|
},
|
||||||
|
responseErrorInterceptor);
|
||||||
|
|
||||||
|
if (!isInstanceCall) {
|
||||||
|
// we are creating instance / collection
|
||||||
|
// - set the initial promise
|
||||||
|
// - return the instance / collection
|
||||||
|
value.$promise = promise;
|
||||||
|
value.$resolved = false;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// instance call
|
||||||
|
return promise;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Resource.prototype['$' + name] = function(params, success, error) {
|
||||||
|
if (isFunction(params)) {
|
||||||
|
error = success; success = params; params = {};
|
||||||
|
}
|
||||||
|
var result = Resource[name].call(this, params, this, success, error);
|
||||||
|
return result.$promise || result;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
Resource.bind = function(additionalParamDefaults){
|
||||||
|
return resourceFactory(url, extend({}, paramDefaults, additionalParamDefaults), actions);
|
||||||
|
};
|
||||||
|
|
||||||
|
return Resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
return resourceFactory;
|
||||||
|
}]);
|
||||||
|
|
||||||
|
|
||||||
|
})(window, window.angular);
|
911
accounting/static/js/lib/angular-route-1.2.5.js
Normal file
911
accounting/static/js/lib/angular-route-1.2.5.js
Normal file
|
@ -0,0 +1,911 @@
|
||||||
|
/**
|
||||||
|
* @license AngularJS v1.2.5
|
||||||
|
* (c) 2010-2014 Google, Inc. http://angularjs.org
|
||||||
|
* License: MIT
|
||||||
|
*/
|
||||||
|
(function(window, angular, undefined) {'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc overview
|
||||||
|
* @name ngRoute
|
||||||
|
* @description
|
||||||
|
*
|
||||||
|
* # ngRoute
|
||||||
|
*
|
||||||
|
* The `ngRoute` module provides routing and deeplinking services and directives for angular apps.
|
||||||
|
*
|
||||||
|
* ## Example
|
||||||
|
* See {@link ngRoute.$route#example $route} for an example of configuring and using `ngRoute`.
|
||||||
|
*
|
||||||
|
* {@installModule route}
|
||||||
|
*
|
||||||
|
* <div doc-module-components="ngRoute"></div>
|
||||||
|
*/
|
||||||
|
/* global -ngRouteModule */
|
||||||
|
var ngRouteModule = angular.module('ngRoute', ['ng']).
|
||||||
|
provider('$route', $RouteProvider);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc object
|
||||||
|
* @name ngRoute.$routeProvider
|
||||||
|
* @function
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
*
|
||||||
|
* Used for configuring routes.
|
||||||
|
*
|
||||||
|
* ## Example
|
||||||
|
* See {@link ngRoute.$route#example $route} for an example of configuring and using `ngRoute`.
|
||||||
|
*
|
||||||
|
* ## Dependencies
|
||||||
|
* Requires the {@link ngRoute `ngRoute`} module to be installed.
|
||||||
|
*/
|
||||||
|
function $RouteProvider(){
|
||||||
|
function inherit(parent, extra) {
|
||||||
|
return angular.extend(new (angular.extend(function() {}, {prototype:parent}))(), extra);
|
||||||
|
}
|
||||||
|
|
||||||
|
var routes = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc method
|
||||||
|
* @name ngRoute.$routeProvider#when
|
||||||
|
* @methodOf ngRoute.$routeProvider
|
||||||
|
*
|
||||||
|
* @param {string} path Route path (matched against `$location.path`). If `$location.path`
|
||||||
|
* contains redundant trailing slash or is missing one, the route will still match and the
|
||||||
|
* `$location.path` will be updated to add or drop the trailing slash to exactly match the
|
||||||
|
* route definition.
|
||||||
|
*
|
||||||
|
* * `path` can contain named groups starting with a colon: e.g. `:name`. All characters up
|
||||||
|
* to the next slash are matched and stored in `$routeParams` under the given `name`
|
||||||
|
* when the route matches.
|
||||||
|
* * `path` can contain named groups starting with a colon and ending with a star:
|
||||||
|
* e.g.`:name*`. All characters are eagerly stored in `$routeParams` under the given `name`
|
||||||
|
* when the route matches.
|
||||||
|
* * `path` can contain optional named groups with a question mark: e.g.`:name?`.
|
||||||
|
*
|
||||||
|
* For example, routes like `/color/:color/largecode/:largecode*\/edit` will match
|
||||||
|
* `/color/brown/largecode/code/with/slashs/edit` and extract:
|
||||||
|
*
|
||||||
|
* * `color: brown`
|
||||||
|
* * `largecode: code/with/slashs`.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param {Object} route Mapping information to be assigned to `$route.current` on route
|
||||||
|
* match.
|
||||||
|
*
|
||||||
|
* Object properties:
|
||||||
|
*
|
||||||
|
* - `controller` – `{(string|function()=}` – Controller fn that should be associated with
|
||||||
|
* newly created scope or the name of a {@link angular.Module#controller registered
|
||||||
|
* controller} if passed as a string.
|
||||||
|
* - `controllerAs` – `{string=}` – A controller alias name. If present the controller will be
|
||||||
|
* published to scope under the `controllerAs` name.
|
||||||
|
* - `template` – `{string=|function()=}` – html template as a string or a function that
|
||||||
|
* returns an html template as a string which should be used by {@link
|
||||||
|
* ngRoute.directive:ngView ngView} or {@link ng.directive:ngInclude ngInclude} directives.
|
||||||
|
* This property takes precedence over `templateUrl`.
|
||||||
|
*
|
||||||
|
* If `template` is a function, it will be called with the following parameters:
|
||||||
|
*
|
||||||
|
* - `{Array.<Object>}` - route parameters extracted from the current
|
||||||
|
* `$location.path()` by applying the current route
|
||||||
|
*
|
||||||
|
* - `templateUrl` – `{string=|function()=}` – path or function that returns a path to an html
|
||||||
|
* template that should be used by {@link ngRoute.directive:ngView ngView}.
|
||||||
|
*
|
||||||
|
* If `templateUrl` is a function, it will be called with the following parameters:
|
||||||
|
*
|
||||||
|
* - `{Array.<Object>}` - route parameters extracted from the current
|
||||||
|
* `$location.path()` by applying the current route
|
||||||
|
*
|
||||||
|
* - `resolve` - `{Object.<string, function>=}` - An optional map of dependencies which should
|
||||||
|
* be injected into the controller. If any of these dependencies are promises, the router
|
||||||
|
* will wait for them all to be resolved or one to be rejected before the controller is
|
||||||
|
* instantiated.
|
||||||
|
* If all the promises are resolved successfully, the values of the resolved promises are
|
||||||
|
* injected and {@link ngRoute.$route#$routeChangeSuccess $routeChangeSuccess} event is
|
||||||
|
* fired. If any of the promises are rejected the
|
||||||
|
* {@link ngRoute.$route#$routeChangeError $routeChangeError} event is fired. The map object
|
||||||
|
* is:
|
||||||
|
*
|
||||||
|
* - `key` – `{string}`: a name of a dependency to be injected into the controller.
|
||||||
|
* - `factory` - `{string|function}`: If `string` then it is an alias for a service.
|
||||||
|
* Otherwise if function, then it is {@link api/AUTO.$injector#invoke injected}
|
||||||
|
* and the return value is treated as the dependency. If the result is a promise, it is
|
||||||
|
* resolved before its value is injected into the controller. Be aware that
|
||||||
|
* `ngRoute.$routeParams` will still refer to the previous route within these resolve
|
||||||
|
* functions. Use `$route.current.params` to access the new route parameters, instead.
|
||||||
|
*
|
||||||
|
* - `redirectTo` – {(string|function())=} – value to update
|
||||||
|
* {@link ng.$location $location} path with and trigger route redirection.
|
||||||
|
*
|
||||||
|
* If `redirectTo` is a function, it will be called with the following parameters:
|
||||||
|
*
|
||||||
|
* - `{Object.<string>}` - route parameters extracted from the current
|
||||||
|
* `$location.path()` by applying the current route templateUrl.
|
||||||
|
* - `{string}` - current `$location.path()`
|
||||||
|
* - `{Object}` - current `$location.search()`
|
||||||
|
*
|
||||||
|
* The custom `redirectTo` function is expected to return a string which will be used
|
||||||
|
* to update `$location.path()` and `$location.search()`.
|
||||||
|
*
|
||||||
|
* - `[reloadOnSearch=true]` - {boolean=} - reload route when only `$location.search()`
|
||||||
|
* or `$location.hash()` changes.
|
||||||
|
*
|
||||||
|
* If the option is set to `false` and url in the browser changes, then
|
||||||
|
* `$routeUpdate` event is broadcasted on the root scope.
|
||||||
|
*
|
||||||
|
* - `[caseInsensitiveMatch=false]` - {boolean=} - match routes without being case sensitive
|
||||||
|
*
|
||||||
|
* If the option is set to `true`, then the particular route can be matched without being
|
||||||
|
* case sensitive
|
||||||
|
*
|
||||||
|
* @returns {Object} self
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* Adds a new route definition to the `$route` service.
|
||||||
|
*/
|
||||||
|
this.when = function(path, route) {
|
||||||
|
routes[path] = angular.extend(
|
||||||
|
{reloadOnSearch: true},
|
||||||
|
route,
|
||||||
|
path && pathRegExp(path, route)
|
||||||
|
);
|
||||||
|
|
||||||
|
// create redirection for trailing slashes
|
||||||
|
if (path) {
|
||||||
|
var redirectPath = (path[path.length-1] == '/')
|
||||||
|
? path.substr(0, path.length-1)
|
||||||
|
: path +'/';
|
||||||
|
|
||||||
|
routes[redirectPath] = angular.extend(
|
||||||
|
{redirectTo: path},
|
||||||
|
pathRegExp(redirectPath, route)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param path {string} path
|
||||||
|
* @param opts {Object} options
|
||||||
|
* @return {?Object}
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* Normalizes the given path, returning a regular expression
|
||||||
|
* and the original path.
|
||||||
|
*
|
||||||
|
* Inspired by pathRexp in visionmedia/express/lib/utils.js.
|
||||||
|
*/
|
||||||
|
function pathRegExp(path, opts) {
|
||||||
|
var insensitive = opts.caseInsensitiveMatch,
|
||||||
|
ret = {
|
||||||
|
originalPath: path,
|
||||||
|
regexp: path
|
||||||
|
},
|
||||||
|
keys = ret.keys = [];
|
||||||
|
|
||||||
|
path = path
|
||||||
|
.replace(/([().])/g, '\\$1')
|
||||||
|
.replace(/(\/)?:(\w+)([\?|\*])?/g, function(_, slash, key, option){
|
||||||
|
var optional = option === '?' ? option : null;
|
||||||
|
var star = option === '*' ? option : null;
|
||||||
|
keys.push({ name: key, optional: !!optional });
|
||||||
|
slash = slash || '';
|
||||||
|
return ''
|
||||||
|
+ (optional ? '' : slash)
|
||||||
|
+ '(?:'
|
||||||
|
+ (optional ? slash : '')
|
||||||
|
+ (star && '(.+?)' || '([^/]+)')
|
||||||
|
+ (optional || '')
|
||||||
|
+ ')'
|
||||||
|
+ (optional || '');
|
||||||
|
})
|
||||||
|
.replace(/([\/$\*])/g, '\\$1');
|
||||||
|
|
||||||
|
ret.regexp = new RegExp('^' + path + '$', insensitive ? 'i' : '');
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc method
|
||||||
|
* @name ngRoute.$routeProvider#otherwise
|
||||||
|
* @methodOf ngRoute.$routeProvider
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* Sets route definition that will be used on route change when no other route definition
|
||||||
|
* is matched.
|
||||||
|
*
|
||||||
|
* @param {Object} params Mapping information to be assigned to `$route.current`.
|
||||||
|
* @returns {Object} self
|
||||||
|
*/
|
||||||
|
this.otherwise = function(params) {
|
||||||
|
this.when(null, params);
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
this.$get = ['$rootScope',
|
||||||
|
'$location',
|
||||||
|
'$routeParams',
|
||||||
|
'$q',
|
||||||
|
'$injector',
|
||||||
|
'$http',
|
||||||
|
'$templateCache',
|
||||||
|
'$sce',
|
||||||
|
function($rootScope, $location, $routeParams, $q, $injector, $http, $templateCache, $sce) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc object
|
||||||
|
* @name ngRoute.$route
|
||||||
|
* @requires $location
|
||||||
|
* @requires $routeParams
|
||||||
|
*
|
||||||
|
* @property {Object} current Reference to the current route definition.
|
||||||
|
* The route definition contains:
|
||||||
|
*
|
||||||
|
* - `controller`: The controller constructor as define in route definition.
|
||||||
|
* - `locals`: A map of locals which is used by {@link ng.$controller $controller} service for
|
||||||
|
* controller instantiation. The `locals` contain
|
||||||
|
* the resolved values of the `resolve` map. Additionally the `locals` also contain:
|
||||||
|
*
|
||||||
|
* - `$scope` - The current route scope.
|
||||||
|
* - `$template` - The current route template HTML.
|
||||||
|
*
|
||||||
|
* @property {Array.<Object>} routes Array of all configured routes.
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* `$route` is used for deep-linking URLs to controllers and views (HTML partials).
|
||||||
|
* It watches `$location.url()` and tries to map the path to an existing route definition.
|
||||||
|
*
|
||||||
|
* Requires the {@link ngRoute `ngRoute`} module to be installed.
|
||||||
|
*
|
||||||
|
* You can define routes through {@link ngRoute.$routeProvider $routeProvider}'s API.
|
||||||
|
*
|
||||||
|
* The `$route` service is typically used in conjunction with the
|
||||||
|
* {@link ngRoute.directive:ngView `ngView`} directive and the
|
||||||
|
* {@link ngRoute.$routeParams `$routeParams`} service.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
This example shows how changing the URL hash causes the `$route` to match a route against the
|
||||||
|
URL, and the `ngView` pulls in the partial.
|
||||||
|
|
||||||
|
Note that this example is using {@link ng.directive:script inlined templates}
|
||||||
|
to get it working on jsfiddle as well.
|
||||||
|
|
||||||
|
<example module="ngViewExample" deps="angular-route.js">
|
||||||
|
<file name="index.html">
|
||||||
|
<div ng-controller="MainCntl">
|
||||||
|
Choose:
|
||||||
|
<a href="Book/Moby">Moby</a> |
|
||||||
|
<a href="Book/Moby/ch/1">Moby: Ch1</a> |
|
||||||
|
<a href="Book/Gatsby">Gatsby</a> |
|
||||||
|
<a href="Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a> |
|
||||||
|
<a href="Book/Scarlet">Scarlet Letter</a><br/>
|
||||||
|
|
||||||
|
<div ng-view></div>
|
||||||
|
<hr />
|
||||||
|
|
||||||
|
<pre>$location.path() = {{$location.path()}}</pre>
|
||||||
|
<pre>$route.current.templateUrl = {{$route.current.templateUrl}}</pre>
|
||||||
|
<pre>$route.current.params = {{$route.current.params}}</pre>
|
||||||
|
<pre>$route.current.scope.name = {{$route.current.scope.name}}</pre>
|
||||||
|
<pre>$routeParams = {{$routeParams}}</pre>
|
||||||
|
</div>
|
||||||
|
</file>
|
||||||
|
|
||||||
|
<file name="book.html">
|
||||||
|
controller: {{name}}<br />
|
||||||
|
Book Id: {{params.bookId}}<br />
|
||||||
|
</file>
|
||||||
|
|
||||||
|
<file name="chapter.html">
|
||||||
|
controller: {{name}}<br />
|
||||||
|
Book Id: {{params.bookId}}<br />
|
||||||
|
Chapter Id: {{params.chapterId}}
|
||||||
|
</file>
|
||||||
|
|
||||||
|
<file name="script.js">
|
||||||
|
angular.module('ngViewExample', ['ngRoute'])
|
||||||
|
|
||||||
|
.config(function($routeProvider, $locationProvider) {
|
||||||
|
$routeProvider.when('/Book/:bookId', {
|
||||||
|
templateUrl: 'book.html',
|
||||||
|
controller: BookCntl,
|
||||||
|
resolve: {
|
||||||
|
// I will cause a 1 second delay
|
||||||
|
delay: function($q, $timeout) {
|
||||||
|
var delay = $q.defer();
|
||||||
|
$timeout(delay.resolve, 1000);
|
||||||
|
return delay.promise;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
$routeProvider.when('/Book/:bookId/ch/:chapterId', {
|
||||||
|
templateUrl: 'chapter.html',
|
||||||
|
controller: ChapterCntl
|
||||||
|
});
|
||||||
|
|
||||||
|
// configure html5 to get links working on jsfiddle
|
||||||
|
$locationProvider.html5Mode(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
function MainCntl($scope, $route, $routeParams, $location) {
|
||||||
|
$scope.$route = $route;
|
||||||
|
$scope.$location = $location;
|
||||||
|
$scope.$routeParams = $routeParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
function BookCntl($scope, $routeParams) {
|
||||||
|
$scope.name = "BookCntl";
|
||||||
|
$scope.params = $routeParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ChapterCntl($scope, $routeParams) {
|
||||||
|
$scope.name = "ChapterCntl";
|
||||||
|
$scope.params = $routeParams;
|
||||||
|
}
|
||||||
|
</file>
|
||||||
|
|
||||||
|
<file name="scenario.js">
|
||||||
|
it('should load and compile correct template', function() {
|
||||||
|
element('a:contains("Moby: Ch1")').click();
|
||||||
|
var content = element('.doc-example-live [ng-view]').text();
|
||||||
|
expect(content).toMatch(/controller\: ChapterCntl/);
|
||||||
|
expect(content).toMatch(/Book Id\: Moby/);
|
||||||
|
expect(content).toMatch(/Chapter Id\: 1/);
|
||||||
|
|
||||||
|
element('a:contains("Scarlet")').click();
|
||||||
|
sleep(2); // promises are not part of scenario waiting
|
||||||
|
content = element('.doc-example-live [ng-view]').text();
|
||||||
|
expect(content).toMatch(/controller\: BookCntl/);
|
||||||
|
expect(content).toMatch(/Book Id\: Scarlet/);
|
||||||
|
});
|
||||||
|
</file>
|
||||||
|
</example>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc event
|
||||||
|
* @name ngRoute.$route#$routeChangeStart
|
||||||
|
* @eventOf ngRoute.$route
|
||||||
|
* @eventType broadcast on root scope
|
||||||
|
* @description
|
||||||
|
* Broadcasted before a route change. At this point the route services starts
|
||||||
|
* resolving all of the dependencies needed for the route change to occurs.
|
||||||
|
* Typically this involves fetching the view template as well as any dependencies
|
||||||
|
* defined in `resolve` route property. Once all of the dependencies are resolved
|
||||||
|
* `$routeChangeSuccess` is fired.
|
||||||
|
*
|
||||||
|
* @param {Object} angularEvent Synthetic event object.
|
||||||
|
* @param {Route} next Future route information.
|
||||||
|
* @param {Route} current Current route information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc event
|
||||||
|
* @name ngRoute.$route#$routeChangeSuccess
|
||||||
|
* @eventOf ngRoute.$route
|
||||||
|
* @eventType broadcast on root scope
|
||||||
|
* @description
|
||||||
|
* Broadcasted after a route dependencies are resolved.
|
||||||
|
* {@link ngRoute.directive:ngView ngView} listens for the directive
|
||||||
|
* to instantiate the controller and render the view.
|
||||||
|
*
|
||||||
|
* @param {Object} angularEvent Synthetic event object.
|
||||||
|
* @param {Route} current Current route information.
|
||||||
|
* @param {Route|Undefined} previous Previous route information, or undefined if current is
|
||||||
|
* first route entered.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc event
|
||||||
|
* @name ngRoute.$route#$routeChangeError
|
||||||
|
* @eventOf ngRoute.$route
|
||||||
|
* @eventType broadcast on root scope
|
||||||
|
* @description
|
||||||
|
* Broadcasted if any of the resolve promises are rejected.
|
||||||
|
*
|
||||||
|
* @param {Object} angularEvent Synthetic event object
|
||||||
|
* @param {Route} current Current route information.
|
||||||
|
* @param {Route} previous Previous route information.
|
||||||
|
* @param {Route} rejection Rejection of the promise. Usually the error of the failed promise.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc event
|
||||||
|
* @name ngRoute.$route#$routeUpdate
|
||||||
|
* @eventOf ngRoute.$route
|
||||||
|
* @eventType broadcast on root scope
|
||||||
|
* @description
|
||||||
|
*
|
||||||
|
* The `reloadOnSearch` property has been set to false, and we are reusing the same
|
||||||
|
* instance of the Controller.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var forceReload = false,
|
||||||
|
$route = {
|
||||||
|
routes: routes,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc method
|
||||||
|
* @name ngRoute.$route#reload
|
||||||
|
* @methodOf ngRoute.$route
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* Causes `$route` service to reload the current route even if
|
||||||
|
* {@link ng.$location $location} hasn't changed.
|
||||||
|
*
|
||||||
|
* As a result of that, {@link ngRoute.directive:ngView ngView}
|
||||||
|
* creates new scope, reinstantiates the controller.
|
||||||
|
*/
|
||||||
|
reload: function() {
|
||||||
|
forceReload = true;
|
||||||
|
$rootScope.$evalAsync(updateRoute);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$rootScope.$on('$locationChangeSuccess', updateRoute);
|
||||||
|
|
||||||
|
return $route;
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param on {string} current url
|
||||||
|
* @param route {Object} route regexp to match the url against
|
||||||
|
* @return {?Object}
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* Check if the route matches the current url.
|
||||||
|
*
|
||||||
|
* Inspired by match in
|
||||||
|
* visionmedia/express/lib/router/router.js.
|
||||||
|
*/
|
||||||
|
function switchRouteMatcher(on, route) {
|
||||||
|
var keys = route.keys,
|
||||||
|
params = {};
|
||||||
|
|
||||||
|
if (!route.regexp) return null;
|
||||||
|
|
||||||
|
var m = route.regexp.exec(on);
|
||||||
|
if (!m) return null;
|
||||||
|
|
||||||
|
for (var i = 1, len = m.length; i < len; ++i) {
|
||||||
|
var key = keys[i - 1];
|
||||||
|
|
||||||
|
var val = 'string' == typeof m[i]
|
||||||
|
? decodeURIComponent(m[i])
|
||||||
|
: m[i];
|
||||||
|
|
||||||
|
if (key && val) {
|
||||||
|
params[key.name] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateRoute() {
|
||||||
|
var next = parseRoute(),
|
||||||
|
last = $route.current;
|
||||||
|
|
||||||
|
if (next && last && next.$$route === last.$$route
|
||||||
|
&& angular.equals(next.pathParams, last.pathParams)
|
||||||
|
&& !next.reloadOnSearch && !forceReload) {
|
||||||
|
last.params = next.params;
|
||||||
|
angular.copy(last.params, $routeParams);
|
||||||
|
$rootScope.$broadcast('$routeUpdate', last);
|
||||||
|
} else if (next || last) {
|
||||||
|
forceReload = false;
|
||||||
|
$rootScope.$broadcast('$routeChangeStart', next, last);
|
||||||
|
$route.current = next;
|
||||||
|
if (next) {
|
||||||
|
if (next.redirectTo) {
|
||||||
|
if (angular.isString(next.redirectTo)) {
|
||||||
|
$location.path(interpolate(next.redirectTo, next.params)).search(next.params)
|
||||||
|
.replace();
|
||||||
|
} else {
|
||||||
|
$location.url(next.redirectTo(next.pathParams, $location.path(), $location.search()))
|
||||||
|
.replace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$q.when(next).
|
||||||
|
then(function() {
|
||||||
|
if (next) {
|
||||||
|
var locals = angular.extend({}, next.resolve),
|
||||||
|
template, templateUrl;
|
||||||
|
|
||||||
|
angular.forEach(locals, function(value, key) {
|
||||||
|
locals[key] = angular.isString(value) ?
|
||||||
|
$injector.get(value) : $injector.invoke(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (angular.isDefined(template = next.template)) {
|
||||||
|
if (angular.isFunction(template)) {
|
||||||
|
template = template(next.params);
|
||||||
|
}
|
||||||
|
} else if (angular.isDefined(templateUrl = next.templateUrl)) {
|
||||||
|
if (angular.isFunction(templateUrl)) {
|
||||||
|
templateUrl = templateUrl(next.params);
|
||||||
|
}
|
||||||
|
templateUrl = $sce.getTrustedResourceUrl(templateUrl);
|
||||||
|
if (angular.isDefined(templateUrl)) {
|
||||||
|
next.loadedTemplateUrl = templateUrl;
|
||||||
|
template = $http.get(templateUrl, {cache: $templateCache}).
|
||||||
|
then(function(response) { return response.data; });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (angular.isDefined(template)) {
|
||||||
|
locals['$template'] = template;
|
||||||
|
}
|
||||||
|
return $q.all(locals);
|
||||||
|
}
|
||||||
|
}).
|
||||||
|
// after route change
|
||||||
|
then(function(locals) {
|
||||||
|
if (next == $route.current) {
|
||||||
|
if (next) {
|
||||||
|
next.locals = locals;
|
||||||
|
angular.copy(next.params, $routeParams);
|
||||||
|
}
|
||||||
|
$rootScope.$broadcast('$routeChangeSuccess', next, last);
|
||||||
|
}
|
||||||
|
}, function(error) {
|
||||||
|
if (next == $route.current) {
|
||||||
|
$rootScope.$broadcast('$routeChangeError', next, last, error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns the current active route, by matching it against the URL
|
||||||
|
*/
|
||||||
|
function parseRoute() {
|
||||||
|
// Match a route
|
||||||
|
var params, match;
|
||||||
|
angular.forEach(routes, function(route, path) {
|
||||||
|
if (!match && (params = switchRouteMatcher($location.path(), route))) {
|
||||||
|
match = inherit(route, {
|
||||||
|
params: angular.extend({}, $location.search(), params),
|
||||||
|
pathParams: params});
|
||||||
|
match.$$route = route;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// No route matched; fallback to "otherwise" route
|
||||||
|
return match || routes[null] && inherit(routes[null], {params: {}, pathParams:{}});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns interpolation of the redirect path with the parameters
|
||||||
|
*/
|
||||||
|
function interpolate(string, params) {
|
||||||
|
var result = [];
|
||||||
|
angular.forEach((string||'').split(':'), function(segment, i) {
|
||||||
|
if (i === 0) {
|
||||||
|
result.push(segment);
|
||||||
|
} else {
|
||||||
|
var segmentMatch = segment.match(/(\w+)(.*)/);
|
||||||
|
var key = segmentMatch[1];
|
||||||
|
result.push(params[key]);
|
||||||
|
result.push(segmentMatch[2] || '');
|
||||||
|
delete params[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return result.join('');
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
ngRouteModule.provider('$routeParams', $RouteParamsProvider);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc object
|
||||||
|
* @name ngRoute.$routeParams
|
||||||
|
* @requires $route
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* The `$routeParams` service allows you to retrieve the current set of route parameters.
|
||||||
|
*
|
||||||
|
* Requires the {@link ngRoute `ngRoute`} module to be installed.
|
||||||
|
*
|
||||||
|
* The route parameters are a combination of {@link ng.$location `$location`}'s
|
||||||
|
* {@link ng.$location#methods_search `search()`} and {@link ng.$location#methods_path `path()`}.
|
||||||
|
* The `path` parameters are extracted when the {@link ngRoute.$route `$route`} path is matched.
|
||||||
|
*
|
||||||
|
* In case of parameter name collision, `path` params take precedence over `search` params.
|
||||||
|
*
|
||||||
|
* The service guarantees that the identity of the `$routeParams` object will remain unchanged
|
||||||
|
* (but its properties will likely change) even when a route change occurs.
|
||||||
|
*
|
||||||
|
* Note that the `$routeParams` are only updated *after* a route change completes successfully.
|
||||||
|
* This means that you cannot rely on `$routeParams` being correct in route resolve functions.
|
||||||
|
* Instead you can use `$route.current.params` to access the new route's parameters.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* <pre>
|
||||||
|
* // Given:
|
||||||
|
* // URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
|
||||||
|
* // Route: /Chapter/:chapterId/Section/:sectionId
|
||||||
|
* //
|
||||||
|
* // Then
|
||||||
|
* $routeParams ==> {chapterId:1, sectionId:2, search:'moby'}
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
function $RouteParamsProvider() {
|
||||||
|
this.$get = function() { return {}; };
|
||||||
|
}
|
||||||
|
|
||||||
|
ngRouteModule.directive('ngView', ngViewFactory);
|
||||||
|
ngRouteModule.directive('ngView', ngViewFillContentFactory);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc directive
|
||||||
|
* @name ngRoute.directive:ngView
|
||||||
|
* @restrict ECA
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* # Overview
|
||||||
|
* `ngView` is a directive that complements the {@link ngRoute.$route $route} service by
|
||||||
|
* including the rendered template of the current route into the main layout (`index.html`) file.
|
||||||
|
* Every time the current route changes, the included view changes with it according to the
|
||||||
|
* configuration of the `$route` service.
|
||||||
|
*
|
||||||
|
* Requires the {@link ngRoute `ngRoute`} module to be installed.
|
||||||
|
*
|
||||||
|
* @animations
|
||||||
|
* enter - animation is used to bring new content into the browser.
|
||||||
|
* leave - animation is used to animate existing content away.
|
||||||
|
*
|
||||||
|
* The enter and leave animation occur concurrently.
|
||||||
|
*
|
||||||
|
* @scope
|
||||||
|
* @priority 400
|
||||||
|
* @example
|
||||||
|
<example module="ngViewExample" deps="angular-route.js" animations="true">
|
||||||
|
<file name="index.html">
|
||||||
|
<div ng-controller="MainCntl as main">
|
||||||
|
Choose:
|
||||||
|
<a href="Book/Moby">Moby</a> |
|
||||||
|
<a href="Book/Moby/ch/1">Moby: Ch1</a> |
|
||||||
|
<a href="Book/Gatsby">Gatsby</a> |
|
||||||
|
<a href="Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a> |
|
||||||
|
<a href="Book/Scarlet">Scarlet Letter</a><br/>
|
||||||
|
|
||||||
|
<div class="view-animate-container">
|
||||||
|
<div ng-view class="view-animate"></div>
|
||||||
|
</div>
|
||||||
|
<hr />
|
||||||
|
|
||||||
|
<pre>$location.path() = {{main.$location.path()}}</pre>
|
||||||
|
<pre>$route.current.templateUrl = {{main.$route.current.templateUrl}}</pre>
|
||||||
|
<pre>$route.current.params = {{main.$route.current.params}}</pre>
|
||||||
|
<pre>$route.current.scope.name = {{main.$route.current.scope.name}}</pre>
|
||||||
|
<pre>$routeParams = {{main.$routeParams}}</pre>
|
||||||
|
</div>
|
||||||
|
</file>
|
||||||
|
|
||||||
|
<file name="book.html">
|
||||||
|
<div>
|
||||||
|
controller: {{book.name}}<br />
|
||||||
|
Book Id: {{book.params.bookId}}<br />
|
||||||
|
</div>
|
||||||
|
</file>
|
||||||
|
|
||||||
|
<file name="chapter.html">
|
||||||
|
<div>
|
||||||
|
controller: {{chapter.name}}<br />
|
||||||
|
Book Id: {{chapter.params.bookId}}<br />
|
||||||
|
Chapter Id: {{chapter.params.chapterId}}
|
||||||
|
</div>
|
||||||
|
</file>
|
||||||
|
|
||||||
|
<file name="animations.css">
|
||||||
|
.view-animate-container {
|
||||||
|
position:relative;
|
||||||
|
height:100px!important;
|
||||||
|
position:relative;
|
||||||
|
background:white;
|
||||||
|
border:1px solid black;
|
||||||
|
height:40px;
|
||||||
|
overflow:hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.view-animate {
|
||||||
|
padding:10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.view-animate.ng-enter, .view-animate.ng-leave {
|
||||||
|
-webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s;
|
||||||
|
transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s;
|
||||||
|
|
||||||
|
display:block;
|
||||||
|
width:100%;
|
||||||
|
border-left:1px solid black;
|
||||||
|
|
||||||
|
position:absolute;
|
||||||
|
top:0;
|
||||||
|
left:0;
|
||||||
|
right:0;
|
||||||
|
bottom:0;
|
||||||
|
padding:10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.view-animate.ng-enter {
|
||||||
|
left:100%;
|
||||||
|
}
|
||||||
|
.view-animate.ng-enter.ng-enter-active {
|
||||||
|
left:0;
|
||||||
|
}
|
||||||
|
.view-animate.ng-leave.ng-leave-active {
|
||||||
|
left:-100%;
|
||||||
|
}
|
||||||
|
</file>
|
||||||
|
|
||||||
|
<file name="script.js">
|
||||||
|
angular.module('ngViewExample', ['ngRoute', 'ngAnimate'],
|
||||||
|
function($routeProvider, $locationProvider) {
|
||||||
|
$routeProvider.when('/Book/:bookId', {
|
||||||
|
templateUrl: 'book.html',
|
||||||
|
controller: BookCntl,
|
||||||
|
controllerAs: 'book'
|
||||||
|
});
|
||||||
|
$routeProvider.when('/Book/:bookId/ch/:chapterId', {
|
||||||
|
templateUrl: 'chapter.html',
|
||||||
|
controller: ChapterCntl,
|
||||||
|
controllerAs: 'chapter'
|
||||||
|
});
|
||||||
|
|
||||||
|
// configure html5 to get links working on jsfiddle
|
||||||
|
$locationProvider.html5Mode(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
function MainCntl($route, $routeParams, $location) {
|
||||||
|
this.$route = $route;
|
||||||
|
this.$location = $location;
|
||||||
|
this.$routeParams = $routeParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
function BookCntl($routeParams) {
|
||||||
|
this.name = "BookCntl";
|
||||||
|
this.params = $routeParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ChapterCntl($routeParams) {
|
||||||
|
this.name = "ChapterCntl";
|
||||||
|
this.params = $routeParams;
|
||||||
|
}
|
||||||
|
</file>
|
||||||
|
|
||||||
|
<file name="scenario.js">
|
||||||
|
it('should load and compile correct template', function() {
|
||||||
|
element('a:contains("Moby: Ch1")').click();
|
||||||
|
var content = element('.doc-example-live [ng-view]').text();
|
||||||
|
expect(content).toMatch(/controller\: ChapterCntl/);
|
||||||
|
expect(content).toMatch(/Book Id\: Moby/);
|
||||||
|
expect(content).toMatch(/Chapter Id\: 1/);
|
||||||
|
|
||||||
|
element('a:contains("Scarlet")').click();
|
||||||
|
content = element('.doc-example-live [ng-view]').text();
|
||||||
|
expect(content).toMatch(/controller\: BookCntl/);
|
||||||
|
expect(content).toMatch(/Book Id\: Scarlet/);
|
||||||
|
});
|
||||||
|
</file>
|
||||||
|
</example>
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc event
|
||||||
|
* @name ngRoute.directive:ngView#$viewContentLoaded
|
||||||
|
* @eventOf ngRoute.directive:ngView
|
||||||
|
* @eventType emit on the current ngView scope
|
||||||
|
* @description
|
||||||
|
* Emitted every time the ngView content is reloaded.
|
||||||
|
*/
|
||||||
|
ngViewFactory.$inject = ['$route', '$anchorScroll', '$animate'];
|
||||||
|
function ngViewFactory( $route, $anchorScroll, $animate) {
|
||||||
|
return {
|
||||||
|
restrict: 'ECA',
|
||||||
|
terminal: true,
|
||||||
|
priority: 400,
|
||||||
|
transclude: 'element',
|
||||||
|
link: function(scope, $element, attr, ctrl, $transclude) {
|
||||||
|
var currentScope,
|
||||||
|
currentElement,
|
||||||
|
autoScrollExp = attr.autoscroll,
|
||||||
|
onloadExp = attr.onload || '';
|
||||||
|
|
||||||
|
scope.$on('$routeChangeSuccess', update);
|
||||||
|
update();
|
||||||
|
|
||||||
|
function cleanupLastView() {
|
||||||
|
if (currentScope) {
|
||||||
|
currentScope.$destroy();
|
||||||
|
currentScope = null;
|
||||||
|
}
|
||||||
|
if(currentElement) {
|
||||||
|
$animate.leave(currentElement);
|
||||||
|
currentElement = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function update() {
|
||||||
|
var locals = $route.current && $route.current.locals,
|
||||||
|
template = locals && locals.$template;
|
||||||
|
|
||||||
|
if (template) {
|
||||||
|
var newScope = scope.$new();
|
||||||
|
var current = $route.current;
|
||||||
|
|
||||||
|
// Note: This will also link all children of ng-view that were contained in the original
|
||||||
|
// html. If that content contains controllers, ... they could pollute/change the scope.
|
||||||
|
// However, using ng-view on an element with additional content does not make sense...
|
||||||
|
// Note: We can't remove them in the cloneAttchFn of $transclude as that
|
||||||
|
// function is called before linking the content, which would apply child
|
||||||
|
// directives to non existing elements.
|
||||||
|
var clone = $transclude(newScope, function(clone) {
|
||||||
|
$animate.enter(clone, null, currentElement || $element, function onNgViewEnter () {
|
||||||
|
if (angular.isDefined(autoScrollExp)
|
||||||
|
&& (!autoScrollExp || scope.$eval(autoScrollExp))) {
|
||||||
|
$anchorScroll();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
cleanupLastView();
|
||||||
|
});
|
||||||
|
|
||||||
|
currentElement = clone;
|
||||||
|
currentScope = current.scope = newScope;
|
||||||
|
currentScope.$emit('$viewContentLoaded');
|
||||||
|
currentScope.$eval(onloadExp);
|
||||||
|
} else {
|
||||||
|
cleanupLastView();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// This directive is called during the $transclude call of the first `ngView` directive.
|
||||||
|
// It will replace and compile the content of the element with the loaded template.
|
||||||
|
// We need this directive so that the element content is already filled when
|
||||||
|
// the link function of another directive on the same element as ngView
|
||||||
|
// is called.
|
||||||
|
ngViewFillContentFactory.$inject = ['$compile', '$controller', '$route'];
|
||||||
|
function ngViewFillContentFactory($compile, $controller, $route) {
|
||||||
|
return {
|
||||||
|
restrict: 'ECA',
|
||||||
|
priority: -400,
|
||||||
|
link: function(scope, $element) {
|
||||||
|
var current = $route.current,
|
||||||
|
locals = current.locals;
|
||||||
|
|
||||||
|
$element.html(locals.$template);
|
||||||
|
|
||||||
|
var link = $compile($element.contents());
|
||||||
|
|
||||||
|
if (current.controller) {
|
||||||
|
locals.$scope = scope;
|
||||||
|
var controller = $controller(current.controller, locals);
|
||||||
|
if (current.controllerAs) {
|
||||||
|
scope[current.controllerAs] = controller;
|
||||||
|
}
|
||||||
|
$element.data('$ngControllerController', controller);
|
||||||
|
$element.children().data('$ngControllerController', controller);
|
||||||
|
}
|
||||||
|
|
||||||
|
link(scope);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
})(window, window.angular);
|
2006
accounting/static/js/lib/bootstrap-3.0.3.js
vendored
Normal file
2006
accounting/static/js/lib/bootstrap-3.0.3.js
vendored
Normal file
File diff suppressed because it is too large
Load diff
9789
accounting/static/js/lib/jquery-1.10.2.js
vendored
Normal file
9789
accounting/static/js/lib/jquery-1.10.2.js
vendored
Normal file
File diff suppressed because it is too large
Load diff
26
accounting/static/templates/detail.html
Normal file
26
accounting/static/templates/detail.html
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<div class="row">
|
||||||
|
<div class="span12">
|
||||||
|
<h1>Transaction {{ transaction.id }}</h1>
|
||||||
|
<h2>{{ transaction.payee }} - {{ transaction.date }}</h2>
|
||||||
|
<h3>Postings</h3>
|
||||||
|
<table class="table striped">
|
||||||
|
<tr>
|
||||||
|
<th>Account</th>
|
||||||
|
<th>Amount</th>
|
||||||
|
</tr>
|
||||||
|
<tr ng-repeat="posting in transaction.postings">
|
||||||
|
<td>{{ posting.account }}</td>
|
||||||
|
<td>{{ posting.amount.amount }} {{ posting.amount.symbol }}</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<div ng-hide="transaction.metadata == {}">
|
||||||
|
<h3>Metadata</h3>
|
||||||
|
<dl class="dl-horizontal">
|
||||||
|
<dt ng-repeat-start="(key, value) in transaction.metadata">
|
||||||
|
{{ key }}
|
||||||
|
</dt>
|
||||||
|
<dd ng-repeat-end>{{ value }}</dd>
|
||||||
|
</dl>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
59
accounting/static/templates/edit.html
Normal file
59
accounting/static/templates/edit.html
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
<div class="row">
|
||||||
|
<div class="span12">
|
||||||
|
<h1>Transaction</h1>
|
||||||
|
<form role="form" class="form-horizontal" name="transactionForm">
|
||||||
|
<fieldset>
|
||||||
|
<legend>Add transaction</legend>
|
||||||
|
<div class="form-group"
|
||||||
|
ng-class="{error: trasactionForm.date.$invalid}">
|
||||||
|
<label class="col-sm-2 control-label" for="date">Date</label>
|
||||||
|
<div class="col-sm-10">
|
||||||
|
<input class="form-control" placeholder="Date"
|
||||||
|
type="date" name="date" ng-model="transaction.date"
|
||||||
|
required />
|
||||||
|
<span ng-show="transactionForm.date.$error.required"
|
||||||
|
class="help-block">
|
||||||
|
Required
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group"
|
||||||
|
ng-class="{error: trasactionForm.payee.$invalid}">
|
||||||
|
<label class="col-sm-2 control-label" for="payee">Payee</label>
|
||||||
|
<div class="col-sm-10">
|
||||||
|
<input class="form-control" placeholder="Payee"
|
||||||
|
type="text" name="payee" ng-model="transaction.payee" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<h3>Postings</h3>
|
||||||
|
<div ng-repeat="posting in transaction.postings">
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="col-sm-2 control-label" for="account">Account</label>
|
||||||
|
<div class="col-sm-10">
|
||||||
|
<input class="form-control" type="text"
|
||||||
|
placeholder="Account"
|
||||||
|
name="account" ng-model="posting.account" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="col-sm-2 control-label" for="amount">Amount</label>
|
||||||
|
<div class="col-sm-10">
|
||||||
|
<input class="form-control" type="text"
|
||||||
|
placeholder="Amount"
|
||||||
|
name="amount" ng-model="posting.amount.amount" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<hr />
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<div class="col-sm-offset-2 col-sm-10">
|
||||||
|
<button class="btn" ng-click="addPosting($event)">
|
||||||
|
Add posting</button>
|
||||||
|
<button ng-click="save()" ng-disabled="transactionForm.$invalid"
|
||||||
|
class="btn btn-primary">Save</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
24
accounting/static/templates/list-transactions.html
Normal file
24
accounting/static/templates/list-transactions.html
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<div class="row">
|
||||||
|
<div class="span12">
|
||||||
|
<h1>Transactions</h1>
|
||||||
|
<table class="table striped">
|
||||||
|
<tr>
|
||||||
|
<th>Date</th>
|
||||||
|
<th>Payee</th>
|
||||||
|
<th>Postings</th>
|
||||||
|
</tr>
|
||||||
|
<tr ng-repeat="transaction in request.transactions">
|
||||||
|
<td>{{ transaction.date }}</td>
|
||||||
|
<td><a href="#/show/{{ transaction.id }}">
|
||||||
|
{{ transaction.payee }}</a></td>
|
||||||
|
<td>
|
||||||
|
<li ng-repeat="posting in transaction.postings">
|
||||||
|
{{ posting.account }} {{ posting.amount.amount }}
|
||||||
|
{{ posting.amount.symbol }}
|
||||||
|
</li>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<a class="btn btn-primary" href="#/new">Add transaction</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
29
accounting/templates/client.html
Normal file
29
accounting/templates/client.html
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html ng-app="accountingClient">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
|
||||||
|
<script src="/static/js/lib/angular-1.2.5.js"></script>
|
||||||
|
<script src="/static/js/lib/angular-route-1.2.5.js"></script>
|
||||||
|
<script src="/static/js/lib/angular-resource-1.2.5.js"></script>
|
||||||
|
<script src="/static/js/lib/jquery-1.10.2.js"></script>
|
||||||
|
<script src="/static/js/lib/bootstrap-3.0.3.js"></script>
|
||||||
|
<script src="/static/js/accounting.js"></script>
|
||||||
|
<script src="/static/js/client.js"></script>
|
||||||
|
|
||||||
|
<link rel="stylesheet" href="/static/css/lib/bootstrap-3.0.3.css" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container" ng-view>
|
||||||
|
</div>
|
||||||
|
<hr />
|
||||||
|
<footer class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="span12">
|
||||||
|
Accounting Client from the
|
||||||
|
<a href="https://gitorious.org/conservancy/accounting-api">
|
||||||
|
Accounting API</a> project.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in a new issue