Skip to content

Commit

Permalink
Merge pull request #3 from OZ-United/canary
Browse files Browse the repository at this point in the history
0.1.0-alpha.3
  • Loading branch information
janantala committed Aug 31, 2013
2 parents b318971 + 7526731 commit efaf2a9
Show file tree
Hide file tree
Showing 32 changed files with 868 additions and 7,167 deletions.
1 change: 1 addition & 0 deletions admin/app/img
4 changes: 4 additions & 0 deletions admin/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->

<!-- build:css({.tmp,app}) styles/main.css -->
<link rel="stylesheet" href="bower_components/bootstrap-sass/bootstrap-2.3.2.min.css">
<link rel="stylesheet" href="styles/main.css">
<link rel="stylesheet" href="styles/ddupload.css">
<!-- endbuild -->
</head>
<body ng-app="adminApp">
Expand Down Expand Up @@ -47,6 +49,8 @@
<script src="bower_components/jquery/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>

<link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css">

<!-- build:js scripts/plugins.js -->
<script src="bower_components/bootstrap-sass/js/bootstrap-affix.js"></script>
<script src="bower_components/bootstrap-sass/js/bootstrap-alert.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion admin/app/scripts/controllers/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ angular.module('adminApp')
Books.create($scope.book, function(book){
$scope.books.push(book);
$scope.newBook = false;
$scope.book = {};
});
$scope.book = {};
}
};

Expand Down
24 changes: 22 additions & 2 deletions admin/app/scripts/controllers/books/bookId.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ angular.module('adminApp')
.controller('BooksBookidCtrl', function ($scope, Books, Users, Rents, $routeParams, $location) {
$scope.book = Books.get({'bookId': $routeParams.bookId});

$scope.open = function (bookCopy) {
$scope.openRent = function (bookCopy) {
$scope.rentBook = true;
$scope.users = Users.query();
$scope.rentCopy = {
Expand All @@ -13,7 +13,7 @@ angular.module('adminApp')
};
};

$scope.close = function () {
$scope.closeRent = function () {
$scope.rentBook = false;
};

Expand All @@ -31,4 +31,24 @@ angular.module('adminApp')
$location.path('/rents');
});
};

$scope.openEdit = function (book) {
$scope.editBook = true;
$scope.edit = angular.copy(book);
delete $scope.edit.copies;
console.log($scope.edit);
};

$scope.closeEdit = function () {
$scope.editBook = false;
};

$scope.saveEdit = function(edit) {
console.log(edit);
edit.$update(function(book){
$scope.book = book;
$scope.closeEdit();
});

};
});
6 changes: 6 additions & 0 deletions admin/app/scripts/controllers/rents.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ angular.module('adminApp')
rent.rent.returnDate = res.rent.returnDate;
});
};

$scope.getStyle = function(rent){
if (new Date(rent.rent.endDate) < new Date() && !rent.rent.returnDate) {
return 'warning';
}
};
});
9 changes: 9 additions & 0 deletions admin/app/scripts/controllers/users/userId.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ angular.module('adminApp')
}
};

$scope.setPassword = function(password){
if ($scope.setPasswordForm.$valid) {
Users.update({'password': password, 'userId': $routeParams.userId}, function(password){
$location.path('/users');
});
$scope.password = {};
}
};

$scope.removeUser = function(user){
Users.remove({'userId': user.userId}, function(){
$location.path('/users');
Expand Down
149 changes: 73 additions & 76 deletions admin/app/scripts/directives/draganddrop.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
'use strict';

angular.module('adminApp')
.directive('draganddrop', function ($q) {
.directive('draganddrop', function ($q, $timeout) {
return {
template:
'<section class="ddupload">' +
'<input type="file" ng-model="logo" name="logo" accept="image/*" id="logo" onchange="angular.element(this).scope().setFiles(this)" ng-cloak/>' +
'<div id="dropbox" class="dropbox" ng-class="dropClass">' +
' <span class="dropbox-text">{{dropText}}</span>' +
' <div>' +
' <img ng-src="{{cover}}" alt="cover" ng-show="cover"/>' +
' <span class="icon-remove dropbox-remove" ng-show="cover" ng-click="removeCover()"></span>' +
' <span class="icon-picture dropbox-add" ng-hide="cover"></span>' +
' <div class="dropbox-image" ng-show="cover">' +
' <img ng-src="{{cover}}" alt="cover"/>' +
' </div>' +
' <div class="dropbox-progress" ng-style="progressStyle" ng-hide="cover"></div>' +
'</div>' +
'</section>',
restrict: 'E',
Expand All @@ -20,79 +22,84 @@ angular.module('adminApp')
controller: function($scope, $element) {

$scope.image = {};

var dropTextOK = 'Select an image';
var dropTextFAIL = 'Only images are allowed!';
$scope.dropText = dropTextOK;
var MAX_WIDTH = 200;
var MAX_HEIGHT = 200;
$scope.progressStyle = {'width': 0};

var dropbox = $element.find('#dropbox')[0];
dropbox.addEventListener('click', function() {
$element.find('input[type=file]')[0].click();
}, false);

var readAsDataURL = function(file){
var deferred = $q.defer();

var reader = new FileReader();
reader.onload = function(e) {
var fileDataUrl = e.target.result;

var tempImg = new Image();
tempImg.onload = function(){
var tempW = tempImg.width;
var tempH = tempImg.height;
console.log(tempW, tempH);
if (tempW > tempH) {
if (tempW > MAX_WIDTH) {
tempH *= MAX_WIDTH / tempW;
tempW = MAX_WIDTH;
console.log(tempW, tempH);
}
}
else {
if (tempH > MAX_HEIGHT) {
tempW *= MAX_HEIGHT / tempH;
tempH = MAX_HEIGHT;
}
}

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = tempW;
canvas.height = tempH;
ctx.drawImage(tempImg, 0, 0, tempW, tempH);

var dataURL = canvas.toDataURL('image/png');
deferred.resolve(dataURL);
$scope.removeCover = function() {
$scope.cover = undefined;
};

var showError = function(){
$scope.$apply(function(){
$scope.cover = undefined;
$scope.dropClass = 'dropbox-not-available';
$scope.progressStyle.width = 0;

$timeout(function(){
$scope.dropClass = '';
}, 500);
});
};

var upload = function(file) {

$scope.cover = undefined;
$scope.progressStyle.width = 0;
$scope.$apply();
var fd = new FormData();
fd.append('image', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload');

xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
var percentage = (e.loaded / e.total) * 100;
console.log(percentage + '%');
$scope.progressStyle.width = percentage + '%';
$scope.$apply();
};
tempImg.src = fileDataUrl;
}
};
reader.readAsDataURL(file);

return deferred.promise;
xhr.onerror = function() {
console.error('An error occurred while submitting the form.', this.statusText);
showError();
};

xhr.onload = function(e) {

if (this.status === 200) {
var data = JSON.parse(e.target.responseText);
console.log(data);


$scope.$apply(function(){
$scope.progressStyle.width = '100%';
$scope.cover = data.image;
});
}
else {
xhr.onerror();
}

};

xhr.send(fd);
};

$scope.setFiles = function(element) {
$scope.dropText = dropTextOK;
$scope.inputEl = element;
$scope.dropClass = '';
$scope.progressStyle.width = 0;
$scope.$apply();

if (element.files.length) {
$scope.files = [];
$scope.dataUrl = '';
var file = element.files[0];
upload(file);
}

var file = element.files[0];
readAsDataURL(file).then(
function(dataUrl){
console.log(dataUrl);
$scope.cover = dataUrl;
}
);

$scope.$apply();
};


Expand All @@ -101,7 +108,6 @@ angular.module('adminApp')
evt.preventDefault();

$scope.$apply(function(){
$scope.dropText = dropTextOK;
$scope.dropClass = '';
});
};
Expand All @@ -113,7 +119,6 @@ angular.module('adminApp')
evt.preventDefault();
var ok = evt.dataTransfer && evt.dataTransfer.types && evt.dataTransfer.types.indexOf('Files') >= 0;
$scope.$apply(function(){
$scope.dropText = ok ? dropTextOK : dropTextFAIL;
$scope.dropClass = ok ? 'dropbox-over' : 'dropbox-not-available';
});
}, false);
Expand All @@ -122,32 +127,24 @@ angular.module('adminApp')
evt.stopPropagation();
evt.preventDefault();
$scope.$apply(function(){
$scope.dropText = dropTextOK;
$scope.removeCover();
$scope.dropClass = '';
});

var files = evt.dataTransfer.files;

if (files.length) {
$scope.files = [];
$scope.dataUrl = '';
$scope.cover = undefined;
}

var file = files[0];

if (file.type.match(/image/g)) {
readAsDataURL(file).then(
function(dataUrl){
console.log(dataUrl);
$scope.cover = dataUrl;
}
);
upload(file);
}
else {
$scope.dropText = dropTextFAIL;
$scope.dropClass = 'dropbox-not-available';
showError();
}

$scope.$apply();
}, false);
}
};
Expand Down
1 change: 1 addition & 0 deletions admin/app/scripts/services/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ angular.module('adminApp')
'create' : { method: 'POST', params: { } },
'query' : { method: 'GET', params: { }, isArray: true },
'update' : { method: 'PUT', params: { } },
'setPassword' : { method: 'PUT', params: { } },
'remove' : { method: 'DELETE', params: { } },
'delete' : { method: 'DELETE', params: { } }
});
Expand Down
Loading

0 comments on commit efaf2a9

Please sign in to comment.