-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplugin.js
More file actions
163 lines (145 loc) · 4.49 KB
/
Copy pathplugin.js
File metadata and controls
163 lines (145 loc) · 4.49 KB
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
/**
* TinyVision plugin for TinyMCE. Primarily handles launching the window with
* the iframe, which is then where most of the action happens.
*
* @param {Window} window Current window object.
* @param {Document} document Current document object.
* @param {tinymce} tinymce TinyMCE dependency.
* @param {undefined} undefined Undefined value for convenience.
*/
(function (window, document, tinymce, undefined) {
'use strict';
// Register the plugin with TinyMCE.
tinymce.PluginManager.add('tinyvision', function (editor, pluginUrl) {
/**
* Object to encapsulate everything.
*
* @type {Object}
*/
var self = {};
/**
* TinyVision window.
*
* @type {tinymce.ui.Window}
* @see {@link http://www.tinymce.com/wiki.php/api4:class.tinymce.ui.Window}
*/
self.win = null;
/**
* Height of the TinyVision window.
*
* @constant {number}
* @default
*/
self.WINDOW_HEIGHT = 537;
/**
* URL to open in the TinyVision window's iframe.
*
* @constant {string}
* @default
*/
self.WINDOW_URL = pluginUrl + '/tinyvision.html';
/**
* Width of the TinyVision window.
*
* @constant {number}
* @default
*/
self.WINDOW_WIDTH = 702;
/**
* Get the URL of the TinyMCE editor skin. TinyMCE doesn't provide an
* accessor method, so the style sheets have to be manually parsed.
*
* @return {string} URL of the TinyMCE editor skin.
*/
self.skinUrl = function () {
var regex = /\/skins\/\w+\/skin(\.min)?\.css$/,
styleSheets = document.styleSheets,
styleSheetsLength = styleSheets.length,
styleSheetHref,
url;
for (var i = 0; i < styleSheetsLength; i += 1) {
styleSheetHref = styleSheets[i].href;
if (styleSheetHref && styleSheetHref.match(regex)) {
url = styleSheetHref;
break;
}
}
return url;
};
/**
* Get the iframe's window object.
*
* @return {Window} iframe's window object.
*/
self.iframeWindow = function () {
return self.win.getEl().getElementsByTagName('iframe')[0].contentWindow;
};
/**
* Get the currently selected item's data.
*
* @return {?Object} Item data.
*/
self.selected = function () {
return self.iframeWindow().selected();
};
/**
* Populate the field that TinyVision was launched from with the selected
* item's `value`. Empties the field value if nothing selected.
*
* @param {Element} field Field that TinyVision was launched from.
* @return {Object} self
*/
self.populateField = function (field) {
var selected = self.selected();
field.value = selected ? selected.value : '';
return self;
};
/**
* Open the TinyMCE window for TinyVision. The window consists primarily of
* an iframe with the toolbar and items list.
*
* @param {string} fieldId ID of field to populate with the selected value.
* @param {string} fieldValue Current value of the field to populate.
* @param {string} type The type of files to display.
* @param {tinymce.ui.Window} parentWin Window launching TinyVision.
* @return {Object} self
*/
self.openWindow = function (fieldId, fieldValue, type, parentWin) {
self.win = editor.windowManager.open({
title: 'Select ' + type,
url: self.WINDOW_URL,
buttons: [
{
text: 'Select',
subtype: 'primary',
onclick: function () {
self.populateField(parentWin.document.getElementById(fieldId));
self.win.close();
}
},
{
text: 'Cancel',
onclick: 'close'
}
],
width: self.WINDOW_WIDTH,
height: self.WINDOW_HEIGHT
}, {
// Values passed to the iframe.
fieldValue: fieldValue,
options: editor.settings.tinyvision,
skinUrl: self.skinUrl(),
type: type
});
return self;
};
// Add a TinyMCE command that other plugins can call to launch TinyVision.
editor.addCommand('tinyvision', function (options) {
self.openWindow(options.fieldId, options.fieldValue, options.type, options.win);
});
// Modify the editor's config to register TinyVision as the file browser.
/* jshint -W106 */
editor.settings.file_browser_callback = self.openWindow;
/* jshint +W106 */
});
})(window, document, tinymce);