Permalink
Browse files

Connected with the API, added the routing, fixned the synchronous par…

…sing of JSON.
  • Loading branch information...
sasanikolic90 committed Jun 17, 2016
1 parent e31c213 commit 75bc39943132341aab74fc0bb98cfac64774184e
@@ -7,7 +7,12 @@
'use strict';
var tag = 'tmgmt-segment';
- var dummyTranslation = 'Dummy translated segment';
+ var xmlhttp;
+
+ if (window.XMLHttpRequest) {
+ // code for IE7+, Firefox, Chrome, Opera, Safari
+ xmlhttp = new XMLHttpRequest();
+ }
var commandDefinition = {
readOnly: 1,
@@ -170,14 +175,17 @@
if (selectedContent) {
var selectedSegment = selectedContent.split(';')[0];
var selectedWord = selectedContent.split(';')[1];
- var selectedSegmentId = selectedContent.split(';')[2];
// Display the segment as active.
displayContent(selectedSegment, selectedWord);
- suggestTranslation(selectedSegmentId, segmentsDiv);
- document.getElementById('btn-use-suggestion').addEventListener('click', function () {
- addSuggestion(selectedSegment);
- });
+ xmlhttp.onreadystatechange = function() {
+ if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
+ var jsonData = JSON.parse(xmlhttp.responseText);
+ suggestTranslation(jsonData.translatedSegment, selectedSegment, segmentsDiv);
+ }
+ };
+ xmlhttp.open('GET', 'http://d8.dev/tmgmt_ckeditor/get.json?segment=' + selectedSegment, true);
+ xmlhttp.send();
}
// If something else is clicked, remove the previous displayed segment.
else {
@@ -280,14 +288,8 @@
}
// Makes a dummy suggestion for the selected segment translation.
- function suggestTranslation(selectedSegmentId, segmentsDiv) {
- var sugTranslationTitle = document.createTextNode('Suggested translation:');
- segmentsDiv.appendChild(sugTranslationTitle);
- var sugTrP = document.createElement('P');
- sugTrP.className = 'suggested-translation';
- var sugTranslation = document.createTextNode(dummyTranslation + ' ' + selectedSegmentId);
- sugTrP.appendChild(sugTranslation);
- segmentsDiv.appendChild(sugTrP);
+ function suggestTranslation(jsonData, selectedSegment, segmentsDiv) {
+ createNewParagraph('Suggested translation', jsonData, segmentsDiv, 'suggested-translation');
var btn = document.createElement('button');
var t = document.createTextNode('Use suggestion');
@@ -296,6 +298,10 @@
btn.setAttribute('type', 'button');
btn.id = 'btn-use-suggestion';
segmentsDiv.appendChild(btn);
+
+ document.getElementById('btn-use-suggestion').addEventListener('click', function () {
+ addSuggestion(selectedSegment);
+ });
}
// Resets the active segments in the editor, so that there is only 1 active.
@@ -307,12 +313,6 @@
activeSegments[j].className = activeSegments[j].className.replace(/ *\bactive-segment\b/g, '');
}
}
- /*for (var i in CKEDITOR.instances) {
- var activeSegments = CKEDITOR.instances[i].document.$.getElementsByClassName('active-segment');
- for (var j = 0; j < activeSegments.length; j++) {
- activeSegments[j].className = 'inactive-segment';
- }
- }*/
}
// Marks active segments in the editor.
@@ -0,0 +1,53 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\tmgmt_ckeditor\Controller\TMGMTCKEditorController.
+ */
+
+namespace Drupal\tmgmt_ckeditor\Controller;
+
+use Drupal\Core\Controller\ControllerBase;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use Symfony\Component\HttpFoundation\Request;
+
+class TMGMTCKEditorController extends ControllerBase {
+
+ /**
+ * Callback for `tmgmt_ckeditor/get.json` API method.
+ *
+ * @param \Symfony\Component\HttpFoundation\Request $request
+ * A configured text editor object
+ * @return JsonResponse
+ * Returns data in a json.
+ */
+ public function get(Request $request) {
+ $params = array();
+ $content = $request->query->get('segment');
+ if (!empty($content)) {
+ /**
+ * @var \Drupal\tmgmt_memory\MemoryManager $memory_manager
+ */
+ $memory_manager = \Drupal::service('tmgmt_memory.memory_manager');
+ $first_segment_text = 'This is the first segment.';
+ $second_segment_text = 'This is the second segment.';
+ $third_segment_text = 'This is the third segment.';
+
+ $first_segment_translated = 'This is the first translated segment';
+ $second_segment_translated = 'This is the second translated segment';
+ $third_segment_translated = 'This is the third translated segment';
+
+ $memory_manager->addSegment('en', $first_segment_text);
+ $memory_manager->addSegment('en', $second_segment_text);
+ $memory_manager->addSegment('en', $third_segment_text);
+
+ $memory_manager->addSegmentTranslation('en', $first_segment_text, 'de', $first_segment_translated);
+ $memory_manager->addSegmentTranslation('en', $second_segment_text, 'de', $second_segment_translated);
+ $memory_manager->addSegmentTranslation('en', $third_segment_text, 'de', $third_segment_translated);
+ $translated_segment = $memory_manager->getSegmentTranslation('en', $content, 'de');
+
+ $json_segment = array("translatedSegment" => $translated_segment->getTarget()->getStrippedData());
+ }
+ return new JsonResponse($json_segment);
+ }
+}
View
@@ -4,3 +4,6 @@ description: CKEditor plugins for the translation UI
core: 8.x
package: CKEditor
version: VERSION
+
+dependencies:
+ - tmgmt_memory
View
@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * @file
+ * Update function for the tmgmt_ckeditor module.
+ */
+
+use Drupal\node\Entity\Node;
+
+/**
+ * Create a node with segments.
+ */
+function tmgmt_ckeditor_install() {
+ $node = Node::create(array(
+ 'type' => 'translatable_node',
+ 'title' => t('Segments test'),
+ 'body' => [
+ 'value' => '<tmgmt-segment id="1">' . t('This is the first segment.') . '</tmgmt-segment>' .
+ '<tmgmt-segment id="2">' . t('This is the second segment.') . '</tmgmt-segment>' .
+ '<tmgmt-segment id="3">' . t('This is the third segment.') . '</tmgmt-segment>',
+ 'format' => 'basic_html',
+ ],
+ 'uid' => '0',
+ 'status' => 1,
+ ));
+ $node->save();
+
+ $memory_manager = \Drupal::service('tmgmt_memory.memory_manager');
+
+}
@@ -0,0 +1,6 @@
+tmgmt_ckeditor.get:
+ path: 'tmgmt_ckeditor/get.json'
+ defaults: { _controller: '\Drupal\tmgmt_ckeditor\Controller\TMGMTCKEditorController::get' }
+ methods: [GET]
+ requirements:
+ _access: 'TRUE'

0 comments on commit 75bc399

Please sign in to comment.