Files
commafeed/src/main/webapp/js/directives.js

273 lines
6.8 KiB
JavaScript
Raw Normal View History

2013-03-21 16:22:58 +01:00
var module = angular.module('commafeed.directives', []);
2013-03-30 20:51:51 +01:00
app.directive('ngBlur', function() {
2013-04-01 14:30:40 +02:00
return {
restrict : 'A',
link : function(scope, elm, attrs) {
elm.bind('blur', function() {
scope.$apply(attrs.ngBlur);
});
}
};
2013-03-30 20:51:51 +01:00
});
2013-03-29 09:11:10 +01:00
module.directive('scrollTo', function() {
return {
restrict : 'A',
controller : function($scope, $element, $attrs) {
},
link : function(scope, element, attrs) {
scope.$watch(attrs.scrollTo, function(value) {
if (value) {
2013-03-29 10:44:41 +01:00
var offset = parseInt(attrs.scrollToOffset, 10);
var scrollTop = $(element).offset().top + offset;
2013-03-29 09:11:10 +01:00
$('html, body').animate({
2013-03-29 10:44:41 +01:00
scrollTop : scrollTop
2013-03-29 09:38:35 +01:00
}, 0);
2013-03-29 09:11:10 +01:00
}
});
}
};
});
2013-03-25 12:24:00 +01:00
module.directive('subscribe', function(SubscriptionService) {
2013-03-24 13:11:05 +01:00
return {
scope : {},
restrict : 'E',
replace : true,
templateUrl : 'directives/subscribe.html',
controller : function($scope, SubscriptionService) {
$scope.opts = {
backdropFade : true,
dialogFade : true
};
2013-03-27 12:13:56 +01:00
$scope.isOpen = false;
$scope.isOpenImport = false;
$scope.sub = {};
2013-03-24 13:11:05 +01:00
$scope.SubscriptionService = SubscriptionService;
$scope.open = function() {
$scope.sub = {};
$scope.isOpen = true;
};
$scope.close = function() {
$scope.isOpen = false;
};
2013-03-30 20:51:51 +01:00
$scope.urlChanged = function() {
if ($scope.sub.url && !$scope.sub.title) {
2013-03-31 08:56:36 +02:00
$scope.sub.title = 'Loading...';
2013-03-30 20:51:51 +01:00
SubscriptionService.fetch({
url : $scope.sub.url
}, function(data) {
$scope.sub.title = data.title;
});
}
};
2013-03-27 12:13:56 +01:00
2013-03-24 13:11:05 +01:00
$scope.save = function() {
2013-03-27 12:13:56 +01:00
SubscriptionService.subscribe($scope.sub);
$scope.close();
2013-03-24 13:11:05 +01:00
};
2013-03-27 12:13:56 +01:00
2013-03-25 12:24:00 +01:00
$scope.openImport = function() {
$scope.isOpenImport = true;
};
$scope.closeImport = function() {
$scope.isOpenImport = false;
};
2013-03-27 12:13:56 +01:00
2013-03-25 12:24:00 +01:00
$scope.uploadComplete = function(contents, completed) {
SubscriptionService.init();
$scope.closeImport();
};
2013-04-01 14:30:40 +02:00
2013-03-31 08:17:28 +02:00
$scope.cat = {};
2013-04-01 14:30:40 +02:00
$scope.openCategory = function() {
2013-03-31 08:17:28 +02:00
$scope.isOpenCategory = true;
$scope.cat = {};
};
2013-04-01 14:30:40 +02:00
$scope.closeCategory = function() {
2013-03-31 08:17:28 +02:00
$scope.isOpenCategory = false;
};
2013-04-01 14:30:40 +02:00
2013-03-31 08:17:28 +02:00
$scope.saveCategory = function() {
SubscriptionService.addCategory($scope.cat);
$scope.closeCategory();
};
2013-03-24 13:11:05 +01:00
}
};
});
2013-03-21 16:22:58 +01:00
module.directive('category', function($compile) {
return {
2013-03-27 12:13:56 +01:00
scope : {
node : '=',
selectedType : '=',
selectedId : '=',
feedClick : '&',
categoryClick : '&',
formatCategoryName : '&',
formatFeedName : '&'
2013-03-21 16:22:58 +01:00
},
restrict : 'E',
2013-03-27 12:13:56 +01:00
replace : true,
templateUrl : 'directives/category.html',
link : function(scope, element) {
var ul = element.find('ul');
2013-03-31 11:30:52 +02:00
var html = '<category ng-repeat="child in node.children" node="child" feed-click="feedClick({id:id})" ';
html = html + 'category-click="categoryClick({id:id})" selected-type="selectedType" selected-id="selectedId" ';
html = html + 'format-category-name="formatCategoryName({category:category})" format-feed-name="formatFeedName({feed:feed})"> ';
html = html + '</category>';
ul.prepend(html);
2013-03-27 12:13:56 +01:00
$compile(ul.contents())(scope);
},
controller : function($scope, $dialog, SubscriptionService) {
$scope.unsubscribe = function(subscription) {
2013-03-24 16:39:45 +01:00
var title = 'Unsubscribe';
2013-03-27 12:13:56 +01:00
var msg = 'Unsubscribe from ' + subscription.name + ' ?';
var btns = [ {
result : 'cancel',
label : 'Cancel'
}, {
result : 'ok',
label : 'OK',
cssClass : 'btn-primary'
} ];
$dialog.messageBox(title, msg, btns).open().then(
function(result) {
if (result == 'ok') {
SubscriptionService
.unsubscribe(subscription.id);
}
});
2013-03-28 10:39:03 +01:00
};
2013-04-01 14:30:40 +02:00
$scope.rename = function(feed) {
2013-04-01 15:28:25 +02:00
var name = window.prompt('Rename feed : ', feed.name);
2013-04-01 14:30:40 +02:00
if (name && name != feed.name) {
feed.name = name;
SubscriptionService.rename({
type : 'feed',
id : feed.id,
name : name
});
}
2013-04-01 15:28:25 +02:00
};
2013-04-01 14:30:40 +02:00
2013-03-28 10:39:03 +01:00
$scope.toggleCategory = function(category) {
SubscriptionService.collapse({
id : category.id,
collapse : !category.expanded
});
};
2013-03-27 12:13:56 +01:00
}
2013-03-21 16:22:58 +01:00
};
2013-03-23 23:14:14 +01:00
});
2013-04-03 13:33:33 +02:00
module.directive('toolbar', function($state, $stateParams, $route, $location,
2013-03-31 08:17:28 +02:00
SettingsService, EntryService, SubscriptionService, SessionService) {
2013-03-23 23:14:14 +01:00
return {
scope : {},
restrict : 'E',
replace : true,
templateUrl : 'directives/toolbar.html',
2013-03-27 13:59:57 +01:00
controller : function($scope, $route, $http, SettingsService) {
2013-03-27 13:59:57 +01:00
function totalActiveAjaxRequests() {
return ($http.pendingRequests.length + $.active);
}
2013-04-01 14:30:40 +02:00
2013-03-31 08:17:28 +02:00
$scope.session = SessionService.get();
2013-03-27 13:59:57 +01:00
$scope.loading = true;
$scope.$watch(totalActiveAjaxRequests, function() {
2013-03-31 11:30:52 +02:00
$scope.loading = (totalActiveAjaxRequests() !== 0);
});
$scope.settingsService = SettingsService;
2013-03-24 17:52:43 +01:00
$scope.refresh = function() {
2013-03-29 15:09:50 +01:00
$scope.$emit('emitReload');
2013-03-27 12:13:56 +01:00
};
2013-03-25 21:01:29 +01:00
$scope.markAllAsRead = function() {
EntryService.mark({
2013-03-29 15:09:50 +01:00
type : $stateParams._type,
id : $stateParams._id,
2013-03-31 11:30:52 +02:00
read : true
2013-03-25 21:01:29 +01:00
}, function() {
SubscriptionService.init(function() {
2013-03-29 15:09:50 +01:00
$scope.$emit('emitReload');
2013-03-25 21:01:29 +01:00
});
});
2013-03-29 15:09:50 +01:00
};
2013-04-03 13:33:33 +02:00
$scope.keywords = $stateParams._keywords;
$scope.search = function() {
if ($scope.keywords == $stateParams._keywords) {
$scope.refresh();
} else {
$state.transitionTo('feeds.search', {
_keywords : $scope.keywords
});
}
};
$scope.showButtons = function() {
return !$stateParams._keywords;
}
2013-03-29 15:09:50 +01:00
$scope.toAdmin = function() {
$location.path('admin');
};
2013-03-23 23:14:14 +01:00
},
link : function($scope, element) {
2013-03-24 17:52:43 +01:00
element.find('.read-mode button').bind('click', function() {
2013-03-23 23:14:14 +01:00
SettingsService.save();
});
}
};
2013-03-27 13:59:57 +01:00
});
module.directive('spinner', function() {
return {
scope : {
2013-03-31 11:30:52 +02:00
shown : '='
2013-03-27 13:59:57 +01:00
},
restrict : 'A',
link : function($scope, element) {
element.addClass('spinner');
var opts = {
lines : 11, // The number of lines to draw
length : 5, // The length of each line
width : 3, // The line thickness
radius : 8, // The radius of the inner circle
corners : 1, // Corner roundness (0..1)
rotate : 0, // The rotation offset
color : '#000', // #rgb or #rrggbb
speed : 1.3, // Rounds per second
trail : 60, // Afterglow percentage
shadow : false, // Whether to render a shadow
hwaccel : true, // Whether to use hardware acceleration
zIndex : 2e9, // The z-index (defaults to 2000000000)
2013-03-29 10:44:41 +01:00
top : 'auto', // Top position relative to parent in px
2013-03-27 13:59:57 +01:00
left : 'auto' // Left position relative to parent in px
};
var spinner = new Spinner(opts);
$scope.$watch('shown', function(shown) {
if (shown) {
spinner.spin();
element.append(spinner.el);
} else {
spinner.stop();
}
});
}
2013-03-31 11:30:52 +02:00
};
2013-03-21 16:22:58 +01:00
});