-
Notifications
You must be signed in to change notification settings - Fork 3
/
segmentArea.js
247 lines (218 loc) · 9.32 KB
/
segmentArea.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// the segment area is the area of the UI representing a single translation unit
// this is a source + target pair
angular.module('controllers')
.controller('SegmentAreaCtrl', [
'$rootScope', '$scope', 'Wikipedia', 'Glossary', '$log', 'ruleMap', 'copyPunctuation', 'Document', 'editSession',
'Logger', 'Projects',
function($rootScope, $scope, Wikipedia, Glossary, $log, ruleMap, copyPunctuation, Document, Session, Logger,
Projects) {
$scope.outputLog = function () {
$log.log('SEGMENT AREA OUTPUT LOG');
Logger.exportJSON();
};
$scope.project = Session;
// this gets reset in the template
$scope.id = {};
// TODO: set this only when this is the active scope
$scope.isActive = { active:true };
// TODO: segment state should be directly linked to the XLIFF document
// working: only one field on this object (not currentState AND completed)
$scope.segmentState = { currentState: 'untranslated', completed: false };
// this is the interface to segment state -- always change via this interface
// states: [ 'pending', 'editing', 'complete' ]
$scope.changeSegmentState = function changeSegmentState (stateName) {
$scope.segmentState = stateName;
$scope.$broadcast('change-segment-state', { 'newState': stateName })
}
$scope.$on('activate-segment', function(event, index) {
if ($scope.index === index) {
// tell the scope to create the editor element
$scope.$broadcast('activate');
}
});
// $scope.language = Document.targetLang;
//
// // for the concordancer - default to English
// $scope.queryLang = 'en';
//
//// currently the model names on the child controllers are different
// $scope.setSource = function(sourceSentence) {
// $log.log("setSource");
// $log.log("source is: " + sourceSentence);
// $scope.segment.source = sourceSentence;
// };
// $scope.setTarget = function(targetSentence) {
// $scope.segment.target = targetSentence;
// };
//
// // Text currently selected in the child editor
$scope.setTextSelection = function(text, range) {
$scope.selectedToken = text;
$scope.selectedRange = range;
$log.log('setTextSelection fired, selectedToken: '+$scope.selectedToken+' selectedRange: ' + $scope.selectedRange);
};
$scope.clearSelection = function() {
$scope.selectedToken = '';
$scope.selectedRange = '';
};
// $scope.copySourcePunctuation = function(segment) {
// $log.log('copy source called from segment ' + segment);
// var source = $scope.segment.source;
// var target = $scope.segment.target;
// $scope.segment.target = copyPunctuation.copySourcePunctuation(source, target);
//
// Session.updateStat('copyPunctuation', $scope.$index);
// // Only adds the action to the edit history if it actually did something.
// if ($scope.segment.target !== target) {
// $scope.editHistory.push(
// ruleMap.newRule('copy-source-punctuation', '', '', 'Copy punctuation from source segment', segment));
// }
// };
//
// $scope.findAndReplace = function(original, change, segment) {
// var regexp = original;
// console.log($scope.segment.target);
// var newTarget = $scope.segment.target.replace(new RegExp(regexp), change);
// console.log(newTarget);
// if (newTarget !== $scope.segment.target) {
// $scope.segment.target = newTarget;
// var msg = 'Find and Replace: ' + original + " → " + change;
// $scope.editHistory.push(
// ruleMap.newRule('find-and-replace', original, change, msg, segment));
// Session.updateStat('pearl-find-and-replace', $scope.$index, msg);
// }
// };
//
// $scope.findAndReplaceTokens = function(original, change, segment) {
// var regexp = '\\b' + original + '\\b';
// console.log($scope.segment.target);
// var newTarget = $scope.segment.target.replace(new RegExp(regexp), change);
// console.log(newTarget);
// if (newTarget !== $scope.segment.target) {
// $scope.segment.target = newTarget;
// var msg = 'Find and Replace tokens: ' + original + " → " + change;
// $scope.editHistory.push(
// ruleMap.newRule('find-and-replace-tokens', original, change, msg, segment));
// Session.updateStat('pearl-find-and-replace-tokens', $scope.$index, msg);
// }
// };
//
// // sets the current target token
// $scope.setCurrentToken = function(token) {
// $scope.currentToken = token;
// };
//
// // special chars toolbar showing
// $scope.showSpecialChars = true;
//
// // testing the special chars directive
// TODO - let the user set this up
// Let the user define the macros they want to use
// $scope.germanChars = ['ä','ö','ü','Ä','Ö','Ü','ß'];
// $scope.insertChar = function(char) {
// $log.log("char to insert: " + char);
// $scope.insertText(char);
// };
//
//
//// TODO: collapse (remove from DOM) when this segment goes out of focus
// $scope.isCollapsed = {collapsed: true, clicked: false};
// $scope.toggleToolbar = function(bool) {
// if (arguments.length > 0) {
// $scope.isCollapsed = { collapsed: bool };
//// TODO: there is a broken corner-case here
// } else {
// $scope.isCollapsed = { collapsed: !$scope.isCollapsed.collapsed };
// }
// $log.log("isCollapsed: the value of isCollapsed is: " + $scope.isCollapsed.collapsed);
// };
$scope.clearEditor = function() {
$log.log('clear editor fired on the segment control');
$scope.$broadcast('clear-editor');
};
// Informs other segments that they should make a change.
// The event argument is a unique object created by the ruleMap service.
// The event will probably be received by the broadcaster as well so the action handlers
// should check first if the edit should be applied or not.
$scope.propagateEdit = function(index) {
$rootScope.$broadcast('propagate-action', $scope.editHistory[index]);
Session.updateStat('propagateAction', $scope.$index, $scope.editHistory[index].operation);
};
// Trigger propagated edits
// $scope.$on('propagate-action', function(event, edit) {
// if ($scope.segmentState.completed)
// return; // do not modify completed segments
//
// var from = edit.segment;
//
// $log.log(edit);
// if (edit.operation == 'copy-source-punctuation') {
// $scope.copySourcePunctuation(from);
// } else if (edit.operation == 'find-and-replace-tokens') {
// $scope.findAndReplaceTokens(edit.original, edit.change, from);
// } else if (edit.operation == 'find-and-replace') {
// $scope.findAndReplace(edit.original, edit.change, from);
// } else if (edit.operation == 'change-token-number') {
// $scope.findAndReplaceTokens(edit.context, edit.change, from);
// } else if (edit.operation == 'change-token-gender') {
// $scope.findAndReplaceTokens(edit.context, edit.change, from);
// } else if (edit.operation == 'change-token-case') {
// $scope.findAndReplaceTokens(edit.context, edit.change, from);
// }
// // Add more action handlers here if needed.
// });
// $scope.addToEditHistory = function(edit) {
// $scope.editHistory.push(edit);
// };
// List of edit actions performed on this segment
$scope.editHistory = [];
// TODO: project should listen for this event, we shouldn't have a separate function to update the project
$scope.segmentFinished = function(segId) {
$log.log("segId is: " + segId);
$scope.segmentState.completed = true;
$scope.isActive.active = false;
Session.updateStat('segmentFinished', segId);
Document.completedSegments[segId] = true;
Session.focusNextSegment();
// TODO: the segment state should be directly tied to the XLIFF document, there should be only a single place where this state data is stored
// TODO: this is a quick hack
$log.log('LOGGING FINAL VALUES');
Logger.Log.segments[segId].final = $scope.segment.target;
$log.log(Logger.Log.segments[segId].final);
// Update the current segment
// Note: the application critically relies on the targetDOM being a link into the DOM object of the XLIFF
$scope.segment.targetDOM.textContent = $scope.segment.target;
Document.revision++;
// Update the project on the server
// TODO: actually put the project object on the $scope, and update directly
var projectResource = $scope.projectResource;
$log.log('PROJECT');
$log.log(projectResource);
projectResource.content = Document.getDOMString();
projectResource.$update(function() {
// $location.path('blogs/' + blog._id);
$log.log('Project updated');
});
};
// Re-opens a finished segment. Undoes what segmentFinished() did
// TODO: this should be handled within the element itself -- there should be a single interface to segmentState
$scope.reopen = function(idx) {
$scope.segmentState.completed = false;
Session.setSegment(idx);
$scope.isActive.active = true;
};
// when the changeSegment event fires, each AceCtrl scope responds
// TODO: this event overlaps with change-segment-state
// the change segment event is fired from changeSegment
$scope.$on('changeSegment', function(e,data) {
if (data.currentSegment === $scope.index) {
// tell the staticTarget directive to create the editor element
$scope.$broadcast('activate');
// smooth scroll
var top = document.getElementById('segment-' + $scope.index).offsetTop;
// scroll and add space for the navbar
var navBarHeight = 100;
$("body").animate({scrollTop: top - navBarHeight}, "slow");
}
});
}]);