Skip to content

Commit

Permalink
display revert popup when adding article from preview
Browse files Browse the repository at this point in the history
  • Loading branch information
takeit committed Mar 23, 2015
1 parent 41937ab commit f9079dd
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 108 deletions.
Expand Up @@ -8,11 +8,9 @@
angular.module('playlistsApp').controller('FeaturedController', [
'$scope',
'Playlist',
'$timeout',
function (
$scope,
Playlist,
$timeout
Playlist
) {

$scope.sortableConfig = {
Expand All @@ -35,7 +33,6 @@ angular.module('playlistsApp').controller('FeaturedController', [

if (occurences > 1) {
$scope.$parent.featuredArticles.splice(evt.newIndex, 1);
Playlist.removeItemFromLogList(number, 'link');
flashMessage(Translator.trans('Item already exists in the list', {}, 'articles'), 'error');

return true;
Expand All @@ -47,12 +44,12 @@ angular.module('playlistsApp').controller('FeaturedController', [
// article that shouldn't be removed, its needed to determine on
// which position it's placed so we can remove the last one elment
// from the list or the one before last - see removeLastArticle function
$scope.articleNotToRemove = item;
$scope.articleOverLimitIndex = evt.newIndex;
$scope.articleOverLimitNumber = number;
$scope.showLimitAlert = true;
$scope.countDown = 6;
$scope.startCountDown();
$scope.$parent.articleNotToRemove = item;
$scope.$parent.articleOverLimitIndex = evt.newIndex;
$scope.$parent.articleOverLimitNumber = number;
$scope.$parent.showLimitAlert = true;
$scope.$parent.countDown = 6;
$scope.$parent.startCountDown();

return true;
}
Expand Down Expand Up @@ -88,94 +85,18 @@ angular.module('playlistsApp').controller('FeaturedController', [
article._order = evt.newIndex + 1;
article._method = "link";
Playlist.addItemToLogList(article);

}
};

// stops, starts counter
$scope.isCounting = false;
$scope.showLimitAlert = false;

$scope.startCountDown = function () {
countDown();
}

/**
* This function count seconds after which revert popup will be closed
* and removes last article from the playlist if the limit is reached,
* inserts new article
*/
var countDown = function(){
if($scope.isCounting) {
return;
}

$scope.isCounting = true;
(function countEvery() {
if ($scope.isCounting) {
$scope.countDown--;
$timeout(countEvery, 1000);
if ($scope.countDown === 0) {
removeLastArticle();
}
}
}());
}

/**
* It removes last article from the playlist if the limit is reached,
* inserts new article. It is called from FeaturedController.
*/
$scope.removeLastInsertNew = function () {
removeLastArticle();
}

/**
* It removes last article from the playlist if the limit is reached,
* inserts new article
*/
var removeLastArticle = function () {
// remove one before last article from the featured articles list
// when we drag-drop to the list as a last element, thats why we need to remove
// one before last article else remove last one (-1)
var articleToRemove = $scope.featuredArticles[$scope.featuredArticles.length - 1];
if ($scope.articleNotToRemove._order == $scope.featuredArticles.length) {
articleToRemove = $scope.featuredArticles[$scope.featuredArticles.length - 2];
}

articleToRemove._method = "unlink";
_.remove(
$scope.featuredArticles,
{number: articleToRemove.number}
);

Playlist.addItemToLogList(articleToRemove);
var logList = Playlist.getLogList();

// we have to now replace last element with one before last in log list
// so it can be save in API in a proper order, actually we first add a
// new article to the featured articles list and then we unlink the last one.
// We need to do it in a reverse way, so we first unlink, and then add a new one.
var lastElement = logList[logList.length - 1];
var beforeLast = logList[logList.length - 2];

logList[logList.length - 1] = beforeLast;
logList[logList.length - 2] = lastElement;

Playlist.setLogList(logList);
$scope.showLimitAlert = false;
$scope.isCounting = false;
}

/**
* It reverts new article insertion over the playlist's limit
*/
$scope.revertAction = function () {
if ($scope.articleOverLimitIndex !== undefined) {
$scope.$parent.featuredArticles.splice($scope.articleOverLimitIndex, 1);
$scope.showLimitAlert = false;
$scope.isCounting = false;
Playlist.removeItemFromLogList($scope.articleOverLimitNumber, 'link');
if ($scope.$parent.articleOverLimitIndex !== undefined) {
$scope.$parent.featuredArticles.splice($scope.$parent.articleOverLimitIndex, 1);
$scope.$parent.showLimitAlert = false;
$scope.$parent.isCounting = false;
Playlist.removeItemFromLogList($scope.$parent.articleOverLimitNumber, 'link');
}
}

Expand Down
Expand Up @@ -11,13 +11,15 @@ angular.module('playlistsApp').controller('PlaylistsController', [
'ngTableParams',
'modalFactory',
'$q',
'$timeout',
'$activityIndicator',
function (
$scope,
Playlist,
ngTableParams,
modalFactory,
$q,
$timeout,
$activityIndicator
) {

Expand All @@ -32,6 +34,7 @@ angular.module('playlistsApp').controller('PlaylistsController', [
// limit var, which is set to false by FeaturedController
// when provided limit is invalid
$scope.playlistLimit = true;
$scope.showLimitAlert = false;

// array of the articles,
// that will be removed or added to the list
Expand Down Expand Up @@ -60,6 +63,80 @@ angular.module('playlistsApp').controller('PlaylistsController', [
}
});

// stops, starts counter
$scope.isCounting = false;

$scope.startCountDown = function () {
countDown();
}

/**
* This function count seconds after which revert popup will be closed
* and removes last article from the playlist if the limit is reached,
* inserts new article
*/
var countDown = function(){
if($scope.isCounting) {
return;
}

$scope.isCounting = true;
(function countEvery() {
if ($scope.isCounting) {
$scope.countDown--;
$timeout(countEvery, 1000);
if ($scope.countDown === 0) {
removeLastArticle();
}
}
}());
}

/**
* It removes last article from the playlist if the limit is reached,
* inserts new article. It is called from FeaturedController.
*/
$scope.removeLastInsertNew = function () {
removeLastArticle();
}

/**
* It removes last article from the playlist if the limit is reached,
* inserts new article
*/
var removeLastArticle = function () {
// remove one before last article from the featured articles list
// when we drag-drop to the list as a last element, thats why we need to remove
// one before last article else remove last one (-1)
var articleToRemove = $scope.featuredArticles[$scope.featuredArticles.length - 1];
if ($scope.articleNotToRemove._order == $scope.featuredArticles.length) {
articleToRemove = $scope.featuredArticles[$scope.featuredArticles.length - 2];
}

articleToRemove._method = "unlink";
_.remove(
$scope.featuredArticles,
{number: articleToRemove.number}
);

Playlist.addItemToLogList(articleToRemove);
var logList = Playlist.getLogList();

// we have to now replace last element with one before last in log list
// so it can be save in API in a proper order, actually we first add a
// new article to the featured articles list and then we unlink the last one.
// We need to do it in a reverse way, so we first unlink, and then add a new one.
var lastElement = logList[logList.length - 1];
var beforeLast = logList[logList.length - 2];

logList[logList.length - 1] = beforeLast;
logList[logList.length - 2] = lastElement;

Playlist.setLogList(logList);
$scope.showLimitAlert = false;
$scope.isCounting = false;
}

/**
* Checks if list name max length is not exceeded,
* if it is, then it will display flash message with error.
Expand Down Expand Up @@ -107,14 +184,6 @@ angular.module('playlistsApp').controller('PlaylistsController', [
{number: $scope.articlePreview.number}
);

if (isLimitReached()) {
flashMessage(Translator.trans(
'List limit reached! Remove some articles from the list before adding new ones.', {}, 'articles'
), 'error');

return true;
}

if (!exists) {
var isInLogList = _.some(
Playlist.getLogList(),
Expand All @@ -126,6 +195,17 @@ angular.module('playlistsApp').controller('PlaylistsController', [
Playlist.addItemToLogList($scope.articlePreview);
$scope.featuredArticles.unshift($scope.articlePreview);
$scope.isViewing = false;

if (isLimitReached()) {
$scope.articleNotToRemove = $scope.articlePreview;
$scope.articleOverLimitIndex = 0;
$scope.articleOverLimitNumber = $scope.articlePreview.number;
$scope.showLimitAlert = true;
$scope.countDown = 6;
$scope.startCountDown();

return true;
}
} else {
flashMessage(Translator.trans('Item already exists in the list', {}, 'articles'), 'error');
}
Expand All @@ -140,14 +220,6 @@ angular.module('playlistsApp').controller('PlaylistsController', [
{number: number}
);

if (isLimitReached()) {
flashMessage(Translator.trans(
'List limit reached! Remove some articles from the list before adding new ones.', {}, 'articles'
), 'error');

return true;
}

if (!exists) {
var article = undefined,
isInLogList;
Expand All @@ -164,6 +236,17 @@ angular.module('playlistsApp').controller('PlaylistsController', [
Playlist.addItemToLogList(article);
$scope.featuredArticles.unshift(article);
$scope.processing = false;

if (isLimitReached()) {
$scope.articleNotToRemove = article;
$scope.articleOverLimitIndex = 0;
$scope.articleOverLimitNumber = article.number;
$scope.showLimitAlert = true;
$scope.countDown = 6;
$scope.startCountDown();

return true;
}
}, function() {
flashMessage(Translator.trans('Error List', {}, 'articles'), 'error');
});
Expand All @@ -181,7 +264,10 @@ angular.module('playlistsApp').controller('PlaylistsController', [
* @return {Boolean}
*/
var isLimitReached = function () {
var limit = $scope.playlist.selected.maxItems;
var limit = 0;
if ($scope.playlist.selected !== undefined) {
limit = $scope.playlist.selected.maxItems;
}

return (limit && limit != 0 && $scope.featuredArticles.length >= limit);
}
Expand Down

0 comments on commit f9079dd

Please sign in to comment.