From 2605239244ad5ffa01339dc213a495c85f3aef62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Ho=CC=88=C3=9Fl?= Date: Wed, 7 Sep 2016 21:42:53 +0200 Subject: [PATCH] Move inline editing methods into a separate service --- openslides/motions/apps.py | 2 +- .../static/js/motions/motion-services.js | 121 ++++++++++++++++++ openslides/motions/static/js/motions/site.js | 117 ++--------------- .../templates/motions/motion-detail.html | 17 +-- 4 files changed, 139 insertions(+), 118 deletions(-) create mode 100644 openslides/motions/static/js/motions/motion-services.js diff --git a/openslides/motions/apps.py b/openslides/motions/apps.py index 0f08c6445..a988a7022 100644 --- a/openslides/motions/apps.py +++ b/openslides/motions/apps.py @@ -8,7 +8,7 @@ class MotionsAppConfig(AppConfig): angular_site_module = True angular_projector_module = True js_files = ['js/motions/base.js', 'js/motions/site.js', 'js/motions/projector.js', - 'js/motions/linenumbering.js', 'js/motions/diff.js'] + 'js/motions/linenumbering.js', 'js/motions/diff.js', 'js/motions/motion-services.js'] def ready(self): # Load projector elements. diff --git a/openslides/motions/static/js/motions/motion-services.js b/openslides/motions/static/js/motions/motion-services.js new file mode 100644 index 000000000..585fc635f --- /dev/null +++ b/openslides/motions/static/js/motions/motion-services.js @@ -0,0 +1,121 @@ +(function () { + +"use strict"; + +angular.module('OpenSlidesApp.motions.motionservices', ['OpenSlidesApp.motions', 'OpenSlidesApp.motions.lineNumbering']) + +.factory('MotionInlineEditing', [ + 'Editor', + 'Motion', + 'Config', + '$timeout', + function (Editor, Motion, Config, $timeout) { + var obj = { + active: false, + changed: false, + trivialChange: false, + editor: null, + lineBrokenText: null, + originalHtml: null + }; + + var $scope, motion; + + obj.init = function (_scope, _motion) { + $scope = _scope; + motion = _motion; + obj.lineBrokenText = motion.getTextWithLineBreaks($scope.version); + obj.originalHtml = obj.lineBrokenText; + + if (motion.state.versioning && Config.get('motions_allow_disable_versioning').value) { + obj.trivialChange = true; + } + }; + + + obj.tinymceOptions = Editor.getOptions(null, true); + obj.tinymceOptions.readonly = 1; + obj.tinymceOptions.setup = function (editor) { + obj.editor = editor; + editor.on("init", function () { + obj.lineBrokenText = motion.getTextWithLineBreaks($scope.version); + obj.editor.setContent(obj.lineBrokenText); + obj.originalHtml = obj.editor.getContent(); + obj.changed = false; + }); + editor.on("change", function () { + obj.changed = (editor.getContent() != obj.originalHtml); + }); + editor.on("undo", function () { + obj.changed = (editor.getContent() != obj.originalHtml); + }); + }; + + obj.setVersion = function (_motion, versionId) { + motion = _motion; // If this is not updated, + console.log(versionId, motion.getTextWithLineBreaks(versionId)); + obj.lineBrokenText = motion.getTextWithLineBreaks(versionId); + obj.changed = false; + obj.active = false; + if (obj.editor) { + obj.editor.setContent(obj.lineBrokenText); + obj.editor.setMode("readonly"); + obj.originalHtml = obj.editor.getContent(); + } else { + obj.originalHtml = obj.lineBrokenText; + } + }; + + obj.enable = function () { + obj.editor.setMode("design"); + obj.active = true; + obj.changed = false; + + obj.lineBrokenText = motion.getTextWithLineBreaks($scope.version); + obj.editor.setContent(obj.lineBrokenText); + obj.originalHtml = obj.editor.getContent(); + $timeout(function () { + obj.editor.focus(); + }, 100); + }; + + obj.disable = function () { + obj.editor.setMode("readonly"); + obj.active = false; + obj.changed = false; + obj.lineBrokenText = obj.originalHtml; + obj.editor.setContent(obj.originalHtml); + }; + + obj.save = function () { + if (!motion.isAllowed('update')) { + throw "No permission to update motion"; + } + + motion.setTextStrippingLineBreaks(motion.active_version, obj.editor.getContent()); + motion.disable_versioning = (obj.trivialChange && Config.get('motions_allow_disable_versioning').value); + + Motion.inject(motion); + // save change motion object on server + Motion.save(motion, {method: 'PATCH'}).then( + function (success) { + $scope.showVersion(motion.getVersion(-1)); + }, + function (error) { + // save error: revert all changes by restore + // (refresh) original motion object from server + Motion.refresh(motion); + var message = ''; + for (var e in error.data) { + message += e + ': ' + error.data[e] + ' '; + } + $scope.alert = {type: 'danger', msg: message, show: true}; + } + ); + }; + + return obj; + } +]); + +}()); diff --git a/openslides/motions/static/js/motions/site.js b/openslides/motions/static/js/motions/site.js index 027b0bf63..65d3b9546 100644 --- a/openslides/motions/static/js/motions/site.js +++ b/openslides/motions/static/js/motions/site.js @@ -2,7 +2,7 @@ 'use strict'; -angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlidesApp.motions.diff']) +angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlidesApp.motions.diff', 'OpenSlidesApp.motions.motionservices']) .factory('MotionContentProvider', ['gettextCatalog', function(gettextCatalog) { /** @@ -1022,7 +1022,6 @@ angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlid .controller('MotionDetailCtrl', [ '$scope', '$http', - '$timeout', 'ngDialog', 'MotionComment', 'MotionForm', @@ -1032,7 +1031,6 @@ angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlid 'Tag', 'User', 'Workflow', - 'Editor', 'Config', 'motion', 'SingleMotionContentProvider', @@ -1040,11 +1038,11 @@ angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlid 'PollContentProvider', 'PdfMakeConverter', 'PdfMakeDocumentProvider', + 'MotionInlineEditing', 'gettextCatalog', - 'diffService', - function($scope, $http, $timeout, ngDialog, MotionComment, MotionForm, Motion, Category, Mediafile, Tag, - User, Workflow, Editor, Config, motion, SingleMotionContentProvider, MotionContentProvider, - PollContentProvider, PdfMakeConverter, PdfMakeDocumentProvider, gettextCatalog, diffService) { + function($scope, $http, ngDialog, MotionComment, MotionForm, Motion, Category, Mediafile, Tag, + User, Workflow, Config, motion, SingleMotionContentProvider, MotionContentProvider, + PollContentProvider, PdfMakeConverter, PdfMakeDocumentProvider, MotionInlineEditing, gettextCatalog) { Motion.bindOne(motion.id, $scope, 'motion'); Category.bindAll({}, $scope, 'categories'); Mediafile.bindAll({}, $scope, 'mediafiles'); @@ -1056,7 +1054,6 @@ angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlid $scope.isCollapsed = true; $scope.commentsFields = MotionComment.getFields(); $scope.lineNumberMode = Config.get('motions_default_line_numbering').value; - $scope.lineBrokenText = motion.getTextWithLineBreaks($scope.version); if (motion.parent_id) { Motion.bindOne(motion.parent_id, $scope, 'parent'); } @@ -1167,17 +1164,7 @@ angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlid // show specific version $scope.showVersion = function (version) { $scope.version = version.id; - $scope.lineBrokenText = motion.getTextWithLineBreaks($scope.version); - $scope.inlineEditing.allowed = (motion.isAllowed('update') && $scope.version == motion.getVersion(-1).id); - $scope.inlineEditing.changed = false; - $scope.inlineEditing.active = false; - if ($scope.inlineEditing.editor) { - $scope.inlineEditing.editor.setContent($scope.lineBrokenText); - $scope.inlineEditing.editor.setMode("readonly"); - $scope.inlineEditing.originalHtml = $scope.inlineEditing.editor.getContent(); - } else { - $scope.inlineEditing.originalHtml = $scope.lineBrokenText; - } + $scope.inlineEditing.setVersion(motion, version.id); }; // permit specific version $scope.permitVersion = function (version) { @@ -1198,96 +1185,8 @@ angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlid }; // Inline editing functions - $scope.inlineEditing = { - allowed: (motion.isAllowed('update') && $scope.version == motion.getVersion(-1).id), - active: false, - changed: false, - trivialChange: false, - trivialChangeAllowed: false, - editor: null, - originalHtml: $scope.lineBrokenText, - }; - - if (motion.state.versioning && Config.get('motions_allow_disable_versioning').value) { - $scope.inlineEditing.trivialChange = true; - $scope.inlineEditing.trivialChangeAllowed = true; - } - - $scope.$watch( - function () { - return Motion.lastModified(); - }, - function () { - $scope.inlineEditing.trivialChangeAllowed = - (motion.state.versioning && Config.get('motions_allow_disable_versioning').value); - } - ); - - $scope.tinymceOptions = Editor.getOptions(null, true); - $scope.tinymceOptions.readonly = 1; - $scope.tinymceOptions.setup = function (editor) { - $scope.inlineEditing.editor = editor; - editor.on("init", function () { - $scope.lineBrokenText = motion.getTextWithLineBreaks($scope.version); - $scope.inlineEditing.editor.setContent($scope.lineBrokenText); - $scope.inlineEditing.originalHtml = $scope.inlineEditing.editor.getContent(); - $scope.inlineEditing.changed = false; - }); - editor.on("change", function () { - $scope.inlineEditing.changed = (editor.getContent() != $scope.inlineEditing.originalHtml); - }); - editor.on("undo", function() { - $scope.inlineEditing.changed = (editor.getContent() != $scope.inlineEditing.originalHtml); - }); - }; - - $scope.enableInlineEditing = function() { - $scope.inlineEditing.editor.setMode("design"); - $scope.inlineEditing.active = true; - $scope.inlineEditing.changed = false; - - $scope.lineBrokenText = motion.getTextWithLineBreaks($scope.version); - $scope.inlineEditing.editor.setContent($scope.lineBrokenText); - $scope.inlineEditing.originalHtml = $scope.inlineEditing.editor.getContent(); - $timeout(function() { - $scope.inlineEditing.editor.focus(); - }, 100); - }; - - $scope.disableInlineEditing = function() { - $scope.inlineEditing.editor.setMode("readonly"); - $scope.inlineEditing.active = false; - $scope.inlineEditing.changed = false; - $scope.lineBrokenText = $scope.inlineEditing.originalHtml; - $scope.inlineEditing.editor.setContent($scope.inlineEditing.originalHtml); - }; - - $scope.motionInlineSave = function () { - if (!$scope.inlineEditing.allowed) { - throw "No permission to update motion"; - } - - motion.setTextStrippingLineBreaks(motion.active_version, $scope.inlineEditing.editor.getContent()); - motion.disable_versioning = $scope.inlineEditing.trivialChange; - - Motion.inject(motion); - // save change motion object on server - Motion.save(motion, { method: 'PATCH' }).then( - function(success) { - $scope.showVersion(motion.getVersion(-1)); - }, - function (error) { - // save error: revert all changes by restore - // (refresh) original motion object from server - Motion.refresh(motion); - var message = ''; - for (var e in error.data) { - message += e + ': ' + error.data[e] + ' '; - } - $scope.alert = {type: 'danger', msg: message, show: true}; - } - ); - }; + $scope.inlineEditing = MotionInlineEditing; + $scope.inlineEditing.init($scope, motion); } ]) diff --git a/openslides/motions/static/templates/motions/motion-detail.html b/openslides/motions/static/templates/motions/motion-detail.html index a3140cd31..f27c4f5c4 100644 --- a/openslides/motions/static/templates/motions/motion-detail.html +++ b/openslides/motions/static/templates/motions/motion-detail.html @@ -273,12 +273,13 @@
-
- - @@ -312,9 +313,9 @@
-
+
-
The text has been changed.
- -
-
+