Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pablomaraya/assignment1 #4

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"angular-bootstrap": "~0.14.0",
"font-awesome": ">=4.1.0",
"lodash": "~2.4.1",
"angular-ui-router": "~0.2.15"
"angular-ui-router": "~0.2.15",
"angular-ui-date": "~0.0.8"
},
"devDependencies": {
"angular-mocks": ">=1.2.*",
Expand Down
49 changes: 45 additions & 4 deletions client/app/components/movies-connector/moviesConnector.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
$q,
$http
) {

var cachedMovies = [];

var service = {
cachedConfiguration: null,
topRatedMovies: topRatedMovies,
Expand All @@ -27,16 +30,54 @@
return $http.get('/api/movies/');
}

function search(query) {
return $http.get('/api/movies/search/' + query);
function search(query) {

var index = queryIsCached(query);

if(index == -1){
var httpPromise = $http.get('/api/movies/search/' + query);
httpPromise
.then(function (response) {
addResponseToCache(query,response);
});

return httpPromise;
}
else{
return $q.when(cachedMovies[index].value);
}
}

function addResponseToCache(query, response){

if(cachedMovies.length > 100){
cachedMovies.splice(0,cachedMovies.length);
}

cachedMovies.push({key:query, value: response});
}

function queryIsCached(query)
{
var index = -1;

for(var i = 0, len = cachedMovies.length; i < len; i++){

if( cachedMovies[i].key == query){
index = i;
break;
}
}

return index;
}

function configuration() {
if (service.cachedConfiguration) {
return $q.when(service.cachedConfiguration);
}
return $http.get('/api/movies/configuration').then(function(response) {
service.cachedConfiguration = response;
return $http.get('/api/movies/configuration').then(function sucessCallback(response) {
service.cachedConfiguration = response;
return response;
});
}
Expand Down
3 changes: 2 additions & 1 deletion client/app/pages/search/search-page.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@


function updatedSearch(query) {
moviesConnector.search(query).then(function (response) {
var searchPromise = moviesConnector.search(query);
searchPromise.then(function (response) {
vm.movies = response.data.results;
angular.forEach(vm.movies, function (each) {
if (vm.topMovieNames.indexOf(each) === -1){
Expand Down
115 changes: 115 additions & 0 deletions client/demo/expenses/expenses-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
(function() {
'use strict';

// Create module and controller
angular
.module('expensesApp',['ui.date'])
.controller('expensesController', expensesController);


expensesController.$inject = [
];

function expensesController() {

// Controller as viewModel
var vm = this;

// Initialization
vm.types = ["food","transportation","lodging","financial","other"];
vm.list = [];
vm.listTotal = [];

vm.currentAmount = 0;


// Controller methods
vm.add = add;
vm.remove = remove;
vm.edit = edit;
vm.save = save;
vm.cancel = cancel;

/* Adds an item to the todo list */
function add(descTxt,dateTxt,typeDdl,amountTxt) {
vm.list.push({desc:descTxt,date:dateTxt, typeExpense:typeDdl, amount:amountTxt, editing:false});
calculateTotals();
cleanForm();
}

/* Remove and item to the expenses list */
function remove(indexList) {
vm.list.splice(indexList,1);
calculateTotals();
}

/* Save changes when we edit a item*/
function save(indexList){
vm.list[indexList].editing = false;
calculateTotals();
}


/* Edit a item from expenses list */
function edit(indexList) {
vm.currentAmount = vm.list[indexList].amount;

disableAllEditingItem();
vm.list[indexList].editing = true;
}

function cancel(indexList) {
vm.list[indexList].editing = false;
vm.list[indexList].amount = vm.currentAmount;
calculateTotals();
}

function disableAllEditingItem(){
for(var i = 0, len = vm.list.length; i < len; i++){
vm.list[i].editing = false;
}
}

/* Clean form test */
function cleanForm()
{
vm.descTxt = '';
vm.amountTxt = '';
vm.dateTxt = '';
}


function calculateTotals()
{
vm.listTotal.splice(0,vm.listTotal.length);

for(var i = 0, len = vm.list.length; i < len; i++){

var type = vm.list[i].typeExpense;

var indextTotal = getIndexFromTotalListByType(type)

if(indextTotal == -1){
vm.listTotal.push({type:vm.list[i].typeExpense, total:vm.list[i].amount});
}
else{
vm.listTotal[indextTotal].total = parseInt(vm.listTotal[indextTotal].total) + parseInt(vm.list[i].amount);
}
}
}

/* Get the index from total list */
function getIndexFromTotalListByType(searchTerm)
{
var index = -1;

for(var i = 0, len = vm.listTotal.length; i < len; i++) {
if (vm.listTotal[i].type === searchTerm) {
index = i;
break;
}
}
return index;
}
}
})();
107 changes: 107 additions & 0 deletions client/demo/expenses/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<html >
<head>

<script src="/bower_components/jquery/dist/jquery.js"></script>
<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.js"></script>
<script src="/bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
<script type="text/javascript" src="/bower_components/jquery-ui/jquery-ui.js"></script>
<script type="text/javascript" src="/bower_components/angular/angular.js"></script>
<script type="text/javascript" src="/bower_components/angular-ui-date/src/date.js"></script>

<script src="/demo/expenses/expenses-controller.js"></script>

<link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.css">
<link rel="stylesheet" href="/demo/expenses/style.css">
<link rel="stylesheet" href="/bower_components/jquery-ui/themes/smoothness/jquery-ui.css"/>

</style>
</head>
<body ng-app="expensesApp">
<div class="container" ng-controller="expensesController as expVM">
<h2>Add Expense:</h2>
<div class="row">
<table class="table" class="col-xs-12">
<tr>
<td>
Desc
</td>
<td>
Date
</td>
<td>
Amount
</td>
<td>
Type
<td>
</tr>
<tr>
<td>
<input id="descTxt" type="text" ng-model="expVM.descTxt">
</td>
<td>
<input ui-date id="dateTxt" type="text" ng-model="expVM.dateTxt">
</td>
<td>
<input id="dateTxt" type="text" ng-model="expVM.amountTxt">
</td>
<td>
<select id="typeDdl" ng-model="expVM.typeDdl">
<option ng-repeat="item in expVM.types" value="{{item}}">{{item}}</option>
</select>
</td>
<td>
<input id="add-button" class="btn btn-default" type="button" value="Add"
ng-click="expVM.add(expVM.descTxt,expVM.dateTxt,expVM.typeDdl,expVM.amountTxt)">
</td>
</tr>
</table>
</div>
<div class="row">
<h2 ng-show="expVM.list.length > 0">Items (#{{expVM.list.length}}):</h2>
<table class="table" ng-repeat="item in expVM.list track by $index" class="col-xs-12">
<tr ng-class="{food: item.typeExpense == 'food',
transp: item.typeExpense == 'transportation',
lodging: item.typeExpense == 'lodging',
other: item.typeExpense == 'other',
financial: item.typeExpense == 'financial'}">
<td>
{{ item.desc }}
</td>
<td>
{{ item.date | date:'shortDate' }}
</td>
<td>
{{ item.typeExpense }}
</td>
<td>
<span ng-show="item.editing == false">{{ item.amount | currency }}</span>
<input ng-show="item.editing == true" id="dateTxt" type="text" ng-model="item.amount">
</td>
<td>
<div ng-show="item.editing == false">
<input class="btn btn-default btn-xs" type="button" value="Edit" ng-click="expVM.edit($index)">
<input class="btn btn-default btn-xs" type="button" value="Delete" ng-click="expVM.remove($index)">
</div>
<div ng-show="item.editing == true">
<input class="btn btn-default btn-xs" type="button" value="Save" ng-click="expVM.save($index)">
<input class="btn btn-default btn-xs" type="button" value="Cancel" ng-click="expVM.cancel($index)">
</div>
</td>
</tr>
</table>
</div>
<div class="row">
<h2 ng-show="expVM.list.length > 0">Totals:</h2>
<table ng-repeat="item in expVM.listTotal" class="col-xs-12">
<tr ng-show="item.total > 0">
<td>
{{item.type}} : {{item.total | currency}}
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
15 changes: 15 additions & 0 deletions client/demo/expenses/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.transp {
background-color: green;
}
.food {
background-color: gray;
}
.lodging {
background-color: red;
}
.financial {
background-color: blue;
}
.other {
background-color: yellow;
}
3 changes: 3 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/json3/lib/json3.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
<script src="bower_components/lodash/dist/lodash.compat.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<script src="bower_components/jquery-ui/jquery-ui.js"></script>
<script src="bower_components/angular-ui-date/src/date.js"></script>
<!-- endbower -->
<!-- endbuild -->

Expand Down