OpenProject is the leading open source project management software.
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.
openproject/frontend/app/ui_components/inplace-editor-dispatcher.js

146 lines
4.5 KiB

10 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.
//++
module.exports = function($sce, AutoCompleteHelper, TextileService) {
function enableAutoCompletion(element) {
var textarea = element.find('.ined-input-wrapper input, .ined-input-wrapper textarea');
AutoCompleteHelper.enableTextareaAutoCompletion(textarea);
}
function disablePreview($scope) {
$scope.isPreview = false;
}
function getAttribute($scope) {
if ($scope.embedded) {
return $scope.attribute.split('.')[0];
} else {
return $scope.attribute;
}
}
function getAttributeValue($scope) {
if ($scope.embedded) {
var path = $scope.attribute.split('.');
return $scope.entity.embedded[path[0]].props[path[1]];
} else {
return $scope.entity.props[getAttribute($scope)];
}
}
10 years ago
function setOptions($scope) {
$scope.options = $scope
.entity.form.embedded.schema
.props[getAttribute($scope)]._links.allowedValues;
10 years ago
if (!$scope.options.length) {
$scope.isEditable = false;
}
}
var hooks = {
_fallback: {
submit: function($scope, data) {
data[getAttribute($scope)] = $scope.dataObject.value;
10 years ago
},
setWriteValue: function($scope) {
$scope.dataObject = {
value: getAttributeValue($scope)
10 years ago
};
},
setReadValue: function($scope) {
$scope.readValue = getAttributeValue($scope);
10 years ago
}
},
text: {
link: function(scope, element) {
enableAutoCompletion(element);
}
},
'wiki_textarea': {
link: function(scope, element) {
enableAutoCompletion(element);
var textarea = element.find('.ined-input-wrapper textarea'),
lines = textarea.val().split('\n');
textarea.attr('rows', lines.length + 1);
},
startEditing: disablePreview,
activate: function($scope) {
disablePreview($scope);
$scope.togglePreview = function() {
$scope.isPreview = !$scope.isPreview;
$scope.error = null;
if (!$scope.isPreview) {
return;
}
$scope.isBusy = true;
TextileService
.renderWithWorkPackageContext($scope.entity.props.id, $scope.dataObject.value)
.then(function(r) {
$scope.onFinally();
$scope.previewHtml = $sce.trustAsHtml(r.data);
}, function(e) {
$scope.onFinally();
$scope.onFail(e);
});
};
},
onFail: disablePreview,
setReadValue: function($scope) {
if (getAttribute($scope) == 'rawDescription') {
10 years ago
$scope.readValue = $sce.trustAsHtml($scope.entity.props.description);
} else {
$scope.readValue = $scope.entity.props[getAttribute($scope)];
10 years ago
}
}
},
select: {
activate: setOptions,
startEditing: setOptions,
submit: function($scope, data) {
data._links = { };
data._links[getAttribute($scope)] = { href: $scope.dataObject.value };
10 years ago
},
setWriteValue: function($scope) {
$scope.dataObject = {
value: $scope.entity.form.embedded.payload.links[getAttribute($scope)].href
10 years ago
};
}
}
};
this.dispatchHook = function($scope, action, data) {
var actionFunction = hooks[$scope.type][action] || hooks._fallback[action] || angular.noop;
return actionFunction($scope, data);
};
};