File size: 3,377 Bytes
1e4c25f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
CKEDITOR.plugins.add('OCUpload', {
  init: function (editor) {
    if (!Drupal.settings.ocupload || !Drupal.settings.ocupload.allowedExt) {
      return;
    }

    // Add Button
    editor.ui.addButton('OCUpload', {
      label: Drupal.t('Upload file'),
      command: 'OCUpload',
      icon: this.path + '../img/icon-ckeditor.png'
    });

    // Add Command
    editor.addCommand('OCUpload', {
      exec: function (editor) {
        if (!Drupal.ocupload.ckeditorPlugin.flow.support) {
          alert(Drupal.t('Your browser not support HTML5 File API'));
        }
      }
    });

    // Lazy create and configure Flow.js object
    if (!Drupal.ocupload.ckeditorPlugin.flow) {
      Drupal.ocupload.ckeditorPlugin.createFlow();
    }

    // Process upload button
    if (Drupal.ocupload.ckeditorPlugin.flow.support) {
      editor.on('dataReady', function () {
        jQuery('.cke_button__ocupload').once('ocupload', function () {
          var $button = jQuery(this);
          Drupal.ocupload.ckeditorPlugin.flow.assignBrowse($button[0]);
          Drupal.ocupload.ckeditorPlugin.flow.assignDrop($button[0]);

          // Stop hidden input click propagation
          $button.find('input').click(function (event) {
            event.stopPropagation();
          });
        });
      });
    }
  }
});

(function ($) {
  Drupal.ocupload = Drupal.ocupload || {};
  Drupal.ocupload.ckeditorPlugin = Drupal.ocupload.ckeditorPlugin || {};

  /**
   * Create and configure Flow.js object.
   */
  Drupal.ocupload.ckeditorPlugin.createFlow = function () {
    Drupal.ocupload.ckeditorPlugin.flow = Drupal.ocupload.createFlow();

    if (!Drupal.ocupload.ckeditorPlugin.flow.support) {
      return false;
    }

    Drupal.ocupload.ckeditorPlugin.flow.on('filesSubmitted', Drupal.ocupload.ckeditorPlugin.onFilesSubmitted);
    Drupal.ocupload.ckeditorPlugin.flow.on('fileSuccess', Drupal.ocupload.ckeditorPlugin.onFileSuccess);

    return true;
  };

  /**
   * Files selected handler.
   */
  Drupal.ocupload.ckeditorPlugin.onFilesSubmitted = function (files, event) {
    var $textarea = $(event.target).closest('.cke').prev('textarea');
    var editorInstance = CKEDITOR.instances[$textarea.attr('id')];
    var selection = editorInstance.getSelection();

    Drupal.ocupload.ckeditorPlugin.flow.opts.query.selectedText = selection ? selection.getNative().toString() : '';
    Drupal.ocupload.ckeditorPlugin.flow.upload();

    Drupal.ocupload.ckeditorPlugin.activeTextareaId = $textarea.attr('id');
  };

  /**
   * File uploaded handler.
   */
  Drupal.ocupload.ckeditorPlugin.onFileSuccess = function (file, response, chunk) {
    if (!Drupal.ocupload.checkResponse(response)) {
      return;
    }

    response = $.parseJSON(response);

    if (response.status) {
      var editorInstance = CKEDITOR.instances[Drupal.ocupload.ckeditorPlugin.activeTextareaId];
      var selection = editorInstance.getSelection();
      var selectedText = selection ? selection.getNative().toString() : '';
      var insertedHtml = selectedText ? response.data : response.data + ' ';

      editorInstance.insertHtml(insertedHtml);

      // Hack for Auto-Grow plugin
      if (CKEDITOR.plugins.get('autogrow')) {
        editorInstance.forceNextSelectionCheck();
        editorInstance.selectionChange();
      }
    }
    else {
      alert(response.data);
    }
  };
})(jQuery);