kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
238 lines
7.7 KiB
238 lines
7.7 KiB
11 years ago
|
//-- copyright
|
||
|
// OpenProject is a project management system.
|
||
|
// Copyright (C) 2012-2014 the OpenProject Foundation (OPF)
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License version 3.
|
||
|
//
|
||
|
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
// Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
|
// Copyright (C) 2010-2013 the ChiliProject Team
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License
|
||
|
// as published by the Free Software Foundation; either version 2
|
||
|
// of the License, or (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
//
|
||
|
// See doc/COPYRIGHT.rdoc for more details.
|
||
|
//++
|
||
|
|
||
|
// TODO move to UI components
|
||
10 years ago
|
module.exports = function($sce, LABEL_MAX_CHARS, KEY_CODES) {
|
||
11 years ago
|
return {
|
||
|
restrict: 'E',
|
||
11 years ago
|
replace: true,
|
||
11 years ago
|
scope: {
|
||
|
selectedTitle: '=',
|
||
11 years ago
|
groups: '=',
|
||
|
transitionMethod: '='
|
||
11 years ago
|
},
|
||
11 years ago
|
templateUrl: '/templates/components/selectable_title.html',
|
||
|
link: function(scope) {
|
||
11 years ago
|
scope.$watch('groups', refreshFilteredGroups);
|
||
11 years ago
|
scope.$watch('selectedId', selectTitle);
|
||
11 years ago
|
|
||
|
function refreshFilteredGroups() {
|
||
11 years ago
|
if(scope.groups){
|
||
|
initFilteredModels();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function selectTitle() {
|
||
|
angular.forEach(scope.filteredGroups, function(group) {
|
||
|
if(group.models.length) {
|
||
|
angular.forEach(group.models, function(model){
|
||
|
model.highlighted = model.id == scope.selectedId;
|
||
|
});
|
||
|
}
|
||
10 years ago
|
});
|
||
11 years ago
|
}
|
||
|
|
||
|
function initFilteredModels() {
|
||
11 years ago
|
scope.filteredGroups = angular.copy(scope.groups);
|
||
11 years ago
|
angular.forEach(scope.filteredGroups, function(group) {
|
||
|
group.models = group.models.map(function(model){
|
||
|
return {
|
||
|
label: model[0],
|
||
11 years ago
|
labelHtml: $sce.trustAsHtml(truncate(model[0], LABEL_MAX_CHARS)),
|
||
11 years ago
|
id: model[1],
|
||
|
highlighted: false
|
||
10 years ago
|
};
|
||
11 years ago
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
11 years ago
|
function labelHtml(label, filterBy) {
|
||
11 years ago
|
filterBy = filterBy.toLowerCase();
|
||
11 years ago
|
label = truncate(label, LABEL_MAX_CHARS);
|
||
|
if(label.toLowerCase().indexOf(filterBy) >= 0) {
|
||
|
var labelHtml = label.substr(0, label.toLowerCase().indexOf(filterBy))
|
||
|
+ "<span class='filter-selection'>" + label.substr(label.toLowerCase().indexOf(filterBy), filterBy.length) + "</span>"
|
||
|
+ label.substr(label.toLowerCase().indexOf(filterBy) + filterBy.length);
|
||
|
} else {
|
||
|
var labelHtml = label;
|
||
|
}
|
||
|
return $sce.trustAsHtml(labelHtml);
|
||
|
}
|
||
|
|
||
|
function truncate(text, chars) {
|
||
|
if (text.length > chars) {
|
||
|
return text.substr(0, chars) + "...";
|
||
|
}
|
||
|
return text;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
function modelIndex(models) {
|
||
|
return models.map(function(model){
|
||
|
return model.id;
|
||
|
}).indexOf(scope.selectedId);
|
||
|
}
|
||
|
|
||
11 years ago
|
function performSelect() {
|
||
|
scope.transitionMethod(scope.selectedId);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
function nextNonEmptyGroup(groups, currentGroupIndex) {
|
||
|
currentGroupIndex = (currentGroupIndex == undefined) ? -1 : currentGroupIndex;
|
||
|
while(currentGroupIndex < groups.length - 1) {
|
||
|
if(groups[currentGroupIndex + 1].models.length) {
|
||
|
return groups[currentGroupIndex + 1];
|
||
|
}
|
||
|
currentGroupIndex = currentGroupIndex + 1;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
function previousNonEmptyGroup(groups, currentGroupIndex) {
|
||
|
while(currentGroupIndex > 0) {
|
||
|
if(groups[currentGroupIndex - 1].models.length) {
|
||
|
return groups[currentGroupIndex - 1];
|
||
|
}
|
||
|
currentGroupIndex = currentGroupIndex - 1;
|
||
|
}
|
||
|
return null;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
function getModelPosition(groups, selectedId) {
|
||
|
for(var group_index = 0; group_index < groups.length; group_index++) {
|
||
|
var models = groups[group_index].models;
|
||
|
var model_index = modelIndex(models);
|
||
|
if(model_index >= 0) {
|
||
|
return {
|
||
|
group: group_index,
|
||
|
model: model_index
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
11 years ago
|
function selectNext() {
|
||
11 years ago
|
var groups = scope.filteredGroups;
|
||
|
if(!scope.selectedId) {
|
||
|
var nextGroup = nextNonEmptyGroup(groups);
|
||
|
scope.selectedId = nextGroup ? nextGroup.models[0].id : 0;
|
||
11 years ago
|
} else {
|
||
11 years ago
|
var position = getModelPosition(groups, scope.selectedId);
|
||
|
if (!position) return;
|
||
|
var models = groups[position.group].models;
|
||
|
|
||
|
if(position.model == models.length - 1){ // It is the last in the group
|
||
|
var nextGroup = nextNonEmptyGroup(groups, position.group);
|
||
|
if(nextGroup) {
|
||
|
scope.selectedId = nextGroup.models[0].id;
|
||
11 years ago
|
}
|
||
11 years ago
|
} else {
|
||
|
scope.selectedId = models[position.model + 1].id;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function selectPrevious() {
|
||
11 years ago
|
var groups = scope.filteredGroups;
|
||
11 years ago
|
if(scope.selectedId) {
|
||
11 years ago
|
var position = getModelPosition(groups, scope.selectedId);
|
||
|
if (!position) return;
|
||
|
var models = groups[position.group].models;
|
||
|
|
||
|
if(position.model == 0){ // It is the last in the group
|
||
|
var previousGroup = previousNonEmptyGroup(groups, position.group);
|
||
|
if(previousGroup) {
|
||
|
scope.selectedId = previousGroup.models[previousGroup.models.length - 1].id;
|
||
11 years ago
|
}
|
||
11 years ago
|
} else {
|
||
|
scope.selectedId = models[position.model - 1].id;
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
function preventDefault(event) {
|
||
11 years ago
|
event.preventDefault();
|
||
|
event.stopPropagation();
|
||
11 years ago
|
}
|
||
|
|
||
|
angular.element('#title-filter').bind('click', function(event) {
|
||
|
preventDefault(event);
|
||
11 years ago
|
});
|
||
10 years ago
|
|
||
11 years ago
|
scope.handleSelection = function(event) {
|
||
|
switch(event.which) {
|
||
|
case KEY_CODES.enter:
|
||
|
performSelect();
|
||
11 years ago
|
preventDefault(event);
|
||
11 years ago
|
break;
|
||
|
case KEY_CODES.down:
|
||
|
selectNext();
|
||
11 years ago
|
preventDefault(event);
|
||
11 years ago
|
break;
|
||
|
case KEY_CODES.up:
|
||
|
selectPrevious();
|
||
11 years ago
|
preventDefault(event);
|
||
11 years ago
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
};
|
||
11 years ago
|
|
||
11 years ago
|
scope.reload = function(modelId, newTitle) {
|
||
11 years ago
|
scope.selectedTitle = newTitle;
|
||
|
scope.reloadMethod(modelId);
|
||
11 years ago
|
scope.$emit('hideAllDropdowns');
|
||
11 years ago
|
};
|
||
11 years ago
|
|
||
|
scope.filterModels = function(filterBy) {
|
||
11 years ago
|
initFilteredModels();
|
||
11 years ago
|
|
||
11 years ago
|
scope.selectedId = 0;
|
||
11 years ago
|
angular.forEach(scope.filteredGroups, function(group) {
|
||
11 years ago
|
if(filterBy.length) {
|
||
|
group.filterBy = filterBy;
|
||
|
group.models = group.models.filter(function(model){
|
||
|
return model.label.toLowerCase().indexOf(filterBy.toLowerCase()) >= 0;
|
||
|
});
|
||
|
|
||
|
if(group.models.length) {
|
||
|
angular.forEach(group.models, function(model){
|
||
|
model['labelHtml'] = labelHtml(model.label, filterBy);
|
||
|
});
|
||
|
if(!scope.selectedId) {
|
||
|
group.models[0].highlighted = true;
|
||
|
scope.selectedId = group.models[0].id;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
});
|
||
11 years ago
|
};
|
||
11 years ago
|
}
|
||
|
};
|
||
10 years ago
|
};
|