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

342 lines
8.0 KiB
JavaScript
Raw Normal View History

2013-03-21 16:22:58 +01:00
var module = angular.module('commafeed.directives', []);
2013-07-31 11:16:50 +02:00
module.directive('focus', ['$timeout', function($timeout) {
2013-05-17 20:08:10 +02:00
return {
restrict : 'A',
link : function(scope, element, attrs) {
scope.$watch(attrs.focus, function(value) {
if (!value)
return;
$timeout(function() {
$(element).focus();
});
});
}
};
2013-07-31 11:16:50 +02:00
}]);
2013-05-17 20:08:10 +02:00
/**
* Open a popup window pointing to the url in the href attribute
*/
2013-06-28 09:41:50 +02:00
module.directive('popup', function() {
var opts = 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=800';
return {
link : function(scope, elm, attrs) {
elm.bind('click', function(event) {
window.open(this.href, '', opts);
event.preventDefault();
});
}
};
});
2013-05-01 18:06:18 +02:00
/**
* Reusable favicon component
*/
2013-06-28 09:41:50 +02:00
module.directive('favicon', function() {
var tpl = '<img ng-src="{{url}}" class="favicon"></img>';
return {
restrict : 'E',
scope : {
url : '='
},
replace : true,
template : tpl
};
});
2013-04-09 11:33:15 +02:00
/**
* Support for the blur event
*/
2013-04-09 11:33:15 +02:00
module.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
});
/**
* Prevent mousewheel scrolling from propagating to the parent when scrollbar
* reaches top or bottom
*/
module.directive('mousewheelScrolling', function() {
return {
restrict : 'A',
link : function(scope, elem, attr) {
elem.bind('mousewheel', function(e, d) {
var t = $(this);
if (d > 0 && t.scrollTop() === 0) {
e.preventDefault();
} else {
2013-07-31 11:16:50 +02:00
if (d < 0 && (t.scrollTop() == t.get(0).scrollHeight - t.innerHeight())) {
e.preventDefault();
}
}
});
}
};
});
/**
* Needed to use recursive directives. Wrap a recursive element with a
* <recursive> tag
*/
2013-07-31 11:16:50 +02:00
module.directive('recursive', ['$compile', function($compile) {
return {
restrict : 'E',
priority : 100000,
compile : function(tElement, tAttr) {
var contents = tElement.contents().remove();
2013-07-11 09:42:54 +02:00
var compiledContents = null;
return function(scope, iElement, iAttr) {
if (!compiledContents) {
compiledContents = $compile(contents);
}
iElement.append(compiledContents(scope, function(clone) {
return clone;
}));
};
}
};
2013-07-31 11:16:50 +02:00
}]);
/**
* Reusable category component
*/
2013-07-31 11:16:50 +02:00
module.directive('category', [function() {
2013-03-21 16:22:58 +01:00
return {
2013-03-27 12:13:56 +01:00
scope : {
node : '=',
level : '=',
2013-03-27 12:13:56 +01:00
selectedType : '=',
selectedId : '=',
2013-04-30 11:29:02 +02:00
showLabel : '=',
showChildren : '=',
2013-04-24 13:16:53 +02:00
unreadCount : '&'
2013-03-21 16:22:58 +01:00
},
restrict : 'E',
2013-03-27 12:13:56 +01:00
replace : true,
templateUrl : 'templates/_category.html',
2013-07-31 11:16:50 +02:00
controller : ['$scope', '$state', '$dialog', 'FeedService', 'CategoryService', 'SettingsService', 'MobileService',
function($scope, $state, $dialog, FeedService, CategoryService, SettingsService, MobileService) {
$scope.settingsService = SettingsService;
2013-07-31 11:16:50 +02:00
$scope.getClass = function(level) {
if ($scope.showLabel) {
return 'indent' + level;
}
};
2013-07-31 11:16:50 +02:00
$scope.categoryLabel = function(category) {
return $scope.showLabel !== true ? $scope.showLabel : category.name;
};
2013-07-31 11:16:50 +02:00
$scope.categoryCountLabel = function(category) {
var count = $scope.unreadCount({
category : category
});
var label = '';
if (count > 0) {
label = '(' + count + ')';
}
return label;
};
2013-07-31 11:16:50 +02:00
$scope.feedCountLabel = function(feed) {
var label = '';
if (feed.unread > 0) {
label = '(' + feed.unread + ')';
}
return label;
};
$scope.feedClicked = function(id, event) {
// Could be called by a middle click
if (!event || (!event.ctrlKey && event.which == 1)) {
MobileService.toggleLeftMenu();
if ($scope.selectedType == 'feed' && id == $scope.selectedId) {
$scope.$emit('emitReload');
} else {
$state.transitionTo('feeds.view', {
_type : 'feed',
_id : id
});
}
if (event) {
event.preventDefault();
event.stopPropagation();
}
}
};
$scope.categoryClicked = function(id) {
MobileService.toggleLeftMenu();
2013-07-31 11:16:50 +02:00
if ($scope.selectedType == 'category' && id == $scope.selectedId) {
$scope.$emit('emitReload');
} else {
$state.transitionTo('feeds.view', {
2013-07-31 11:16:50 +02:00
_type : 'category',
_id : id
});
}
2013-07-31 11:16:50 +02:00
};
2013-07-31 11:16:50 +02:00
$scope.showFeedDetails = function(feed) {
$state.transitionTo('feeds.feed_details', {
_id : feed.id
});
2013-07-31 11:16:50 +02:00
};
2013-07-31 11:16:50 +02:00
$scope.showCategoryDetails = function(category) {
$state.transitionTo('feeds.category_details', {
_id : category.id
});
};
$scope.toggleCategory = function(category, event) {
event.preventDefault();
event.stopPropagation();
category.expanded = !category.expanded;
if (category.id == 'all') {
return;
}
CategoryService.collapse({
id : category.id,
collapse : !category.expanded
});
};
}]
2013-03-21 16:22:58 +01:00
};
2013-07-31 11:16:50 +02:00
}]);
2013-03-23 23:14:14 +01:00
/**
* Reusable spinner component
*/
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
};
});
module.directive('draggable', function() {
return {
restrict : 'A',
link : function(scope, element, attrs) {
element.draggable({
2013-07-31 11:16:50 +02:00
revert : 'invalid',
helper : 'clone',
distance : 10,
axis : 'y'
}).data('source', scope.$eval(attrs.draggable));
}
};
});
2013-07-31 11:16:50 +02:00
module.directive('droppable', ['CategoryService', 'FeedService', function(CategoryService, FeedService) {
return {
restrict : 'A',
link : function(scope, element, attrs) {
element.droppable({
tolerance : 'pointer',
over : function(event, ui) {
2013-06-27 14:27:00 +02:00
2013-07-31 11:16:50 +02:00
},
drop : function(event, ui) {
var draggable = angular.element(ui.draggable);
2013-06-27 14:27:00 +02:00
2013-07-31 11:16:50 +02:00
var source = draggable.data('source');
var target = scope.$eval(attrs.droppable);
2013-06-27 14:27:00 +02:00
2013-07-31 11:16:50 +02:00
if (angular.equals(source, target)) {
return;
}
2013-06-27 14:27:00 +02:00
2013-07-31 11:16:50 +02:00
var data = {
id : source.id,
name : source.name
};
2013-06-27 14:27:00 +02:00
2013-07-31 11:16:50 +02:00
if (source.children) {
// source is a category
2013-06-27 14:27:00 +02:00
2013-07-31 11:16:50 +02:00
} else {
// source is a feed
if (target.children) {
// target is a category
data.categoryId = target.id;
data.position = 0;
} else {
// target is a feed
data.categoryId = target.categoryId;
data.position = target.position;
}
2013-07-31 11:02:39 +02:00
2013-07-31 11:16:50 +02:00
FeedService.modify(data, function() {
CategoryService.init();
});
}
scope.$apply();
}
});
}
};
}]);
2013-07-31 11:02:39 +02:00
2013-08-23 12:46:35 +02:00
module.directive('metricMeter', function() {
return {
scope : {
metric : '=',
label : '='
},
restrict : 'E',
templateUrl : 'templates/_metrics.meter.html'
2013-07-31 11:02:39 +02:00
};
2013-08-23 14:12:13 +02:00
});
module.directive('metricGauge', function() {
return {
scope : {
metric : '=',
label : '='
},
restrict : 'E',
templateUrl : 'templates/_metrics.gauge.html'
};
2013-07-31 11:02:39 +02:00
});