瀏覽代碼

Fix product locale CKEditor reinit across tabs

root 1 天之前
父節點
當前提交
68bacd0a36
共有 1 個文件被更改,包括 60 次插入0 次删除
  1. 60 0
      app/assets/javascripts/rails_admin/custom/ui.js

+ 60 - 0
app/assets/javascripts/rails_admin/custom/ui.js

@@ -1,6 +1,57 @@
 //= require rails_admin/custom/ckeditor_ajax
 
 (function($) {
+  function replaceCkeditor(textarea) {
+    if (!window.CKEDITOR || !textarea || !textarea.id) {
+      return;
+    }
+
+    try {
+      if (window.CKEDITOR.instances[textarea.id]) {
+        window.CKEDITOR.instances[textarea.id].updateElement();
+        window.CKEDITOR.instances[textarea.id].destroy(true);
+      }
+    } catch (error) {}
+
+    var $textarea = $(textarea);
+    $textarea.removeClass('ckeditored');
+    window.CKEDITOR.replace(textarea, $textarea.data('options') || {});
+    $textarea.addClass('ckeditored');
+  }
+
+  function refreshPaneEditors($scope) {
+    $scope.find('[data-richtext=ckeditor]').each(function() {
+      replaceCkeditor(this);
+    });
+  }
+
+  function ensurePaneEditors($scope) {
+    var $editors = $scope.find('[data-richtext=ckeditor]');
+    if ($editors.length === 0) {
+      return;
+    }
+
+    if (window.CKEDITOR) {
+      setTimeout(function() {
+        refreshPaneEditors($scope);
+      }, 0);
+      return;
+    }
+
+    var options = $editors.first().data('options') || {};
+    if (options.base_location) {
+      window.CKEDITOR_BASEPATH = options.base_location;
+    }
+
+    if (options.jspath) {
+      $.getScript(options.jspath, function() {
+        setTimeout(function() {
+          refreshPaneEditors($scope);
+        }, 0);
+      });
+    }
+  }
+
   function nearestField($input) {
     return $input.closest('.form-group, .control-group');
   }
@@ -82,6 +133,15 @@
 
     $tabs.append($header).append($nav).append($content);
     $mount.replaceWith($tabs);
+
+    ensurePaneEditors($content.find('.product-locale-pane.active'));
+    $nav.on('shown.bs.tab', 'a[data-toggle="tab"]', function() {
+      var selector = $(this).attr('href');
+      if (!selector) {
+        return;
+      }
+      ensurePaneEditors($content.find(selector));
+    });
   }
 
   $(document).on('ready page:load turbolinks:load', buildProductLocaleTabs);