public
Last active

  • Download Gist
yui_rte_with_media_button.js
JavaScript
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
 
(function () {
var Dom = YAHOO.util.Dom;
 
var myConfig = {
height: '250px',
width: '628px',
animate: true,
dompath: false,
focusAtStart: false,
extracss: ' img {display:block;margin:0 auto;}html{width:100%;overflow:auto;}.yui-media {display:block; height: 100px; width: 100px; border: 1px solid black; background-color: #f2f2f2; cursor:pointer !important; background-image: url( ${resource(dir:'css', file:'media_placeholder.gif')} ); background-position: 50% 50%; background-repeat: no-repeat; }.yui-media *{display:none;height:0px; text-indent:-4999px; width:0px;overflow:hidden; /*height width and overflow for safari*/}',
toolbar: {
 
collapse: true,
titlebar: 'The(G)net-Editor',
draggable: false,
buttons: [{
group: 'textstyle',
label: 'Font Style',
buttons: [{
type: 'push',
label: 'Bold CTRL + SHIFT + B',
value: 'bold'
},
{
type: 'push',
label: 'Italic CTRL + SHIFT + I',
value: 'italic'
},
{
type: 'push',
label: 'Underline CTRL + SHIFT + U',
value: 'underline'
},
{
type: 'separator'
},
{
type: 'color',
label: 'Font Color',
value: 'forecolor',
disabled: true
},
{
type: 'color',
label: 'Background Color',
value: 'backcolor',
disabled: true
}]
},
{
type: 'separator'
},
{
group: 'alignment',
label: 'Alignment',
buttons: [{
type: 'push',
label: 'Align Left CTRL + SHIFT + [',
value: 'justifyleft'
},
{
type: 'push',
label: 'Align Center CTRL + SHIFT + |',
value: 'justifycenter'
},
{
type: 'push',
label: 'Align Right CTRL + SHIFT + ]',
value: 'justifyright'
},
{
type: 'push',
label: 'Justify',
value: 'justifyfull'
}]
},
{
type: 'separator'
},
{
group: 'parastyle',
label: 'Paragraph Style',
buttons: [{
type: 'select',
label: 'Normal',
value: 'heading',
disabled: true,
menu: [{
text: 'Normal',
value: 'none',
checked: true
},
{
text: 'Header 1',
value: 'h1'
},
{
text: 'Header 2',
value: 'h2'
},
{
text: 'Header 3',
value: 'h3'
},
{
text: 'Header 4',
value: 'h4'
},
{
text: 'Header 5',
value: 'h5'
},
{
text: 'Header 6',
value: 'h6'
}]
}]
},
 
{
type: 'separator'
},
{
group: 'indentlist',
label: 'Lists',
buttons: [{
type: 'push',
label: 'Create an Unordered List',
value: 'insertunorderedlist'
},
{
type: 'push',
label: 'Create an Ordered List',
value: 'insertorderedlist'
}]
},
{
type: 'separator'
},
{
group: 'insertitem',
label: 'Insert Item',
buttons: [{
type: 'push',
label: 'HTML Link CTRL + SHIFT + L',
value: 'createlink',
disabled: true
},
{
type: 'push',
label: 'Insert Image',
value: 'insertimage'
},
{
type: 'push',
label: 'Media Options',
value: 'insertmedia'
}]
}]
 
}
};
 
var myEditor = new YAHOO.widget.Editor('text', myConfig);
 
/* after the window is closed */
var _handleWindowClose = function () {
var el = this.currentElement[0];
//set the attribute on the element so we can parse it out afterward.
var textarea = document.getElementById('embedplugin');
var val = textarea.value;
 
if (-1 != val.indexOf("<embed")) {
el.setAttribute('embeddedcode', textarea.value);
} else {
el.parentNode.removeChild(el);
}
 
textarea.value = "";
 
this.nodeChange();
};
 
/* the very call */
var _handleMediaWindow = function () {
var el = this.currentElement[0];
 
var win = new YAHOO.widget.EditorWindow('insertmedia', {
width: '425px'
});
var bFlag = false;
 
win.setHeader('Edit Media');
this.openWindow(win);
 
this.on('afterOpenWindow', function () {
this.get('panel').syncIframe();
},
this, true);
 
if (null != el.getAttribute("embeddedcode")) {
var textarea = document.getElementById('embedplugin');
 
if (null != textarea) {
textarea.value = el.getAttribute("embeddedcode");
} else {
bFlag = true;
}
}
 
};
 
/* if a embeded is selected */
myEditor.on('toolbarLoaded', function () {
this.toolbar.on('insertmediaClick', function () {
var el = this._getSelectedElement();
 
if (Dom.hasClass(el, 'yui-media')) {
this.currentElement = [el];
handleInsertIcon.call(this);
return false;
}
},
this, true);
},
myEditor, true);
 
/* press on the toolbar button */
myEditor.cmd_insertmedia = function () {
/* prevent to overwrite a valid media placeholder !!! */
if (!Dom.hasClass(this._getSelectedElement(), 'yui-media')) {
/* dummy image */
this.execCommand('insertimage', 'none ');
/* replace that image with a span or div */
var el = this._swapEl(this.currentElement[0], 'span', function (el) {
el.className = 'yui-media';
Dom.setStyle(el, 'fontSize', '100px');
});
/* set the current element */
this.currentElement = [el];
}
/* handle */
_handleMediaWindow.call(this);
/* no further event handling */
return[false];
};
 
/* double click event */
myEditor.on('editorDoubleClick', function () {
var el = this._getSelectedElement();
if (Dom.hasClass(el, 'yui-media')) {
this.currentElement = [el];
_handleMediaWindow.call(this);
return false;
}
},
myEditor, true);
 
/* selects the button */
myEditor.on('afterNodeChange', function () {
if (this._hasSelection()) {
this.toolbar.disableButton('insertmedia');
} else {
this.toolbar.enableButton('insertmedia');
var el = this._getSelectedElement();
if (Dom.hasClass(el, 'yui-media')) {
this.toolbar.selectButton('insertmedia');
} else {
this.toolbar.deselectButton('insertmedia');
}
}
},
myEditor, true);
 
/* on close handler */
myEditor.on('windowinsertmediaClose', function () {
_handleWindowClose.call(this);
},
myEditor, true);
 
/* one timers */
myEditor.on('windowRender', function () {
var body = document.createElement('div');
 
// a nice styled - simple (and just for <object><param>..<embed/></object>) text entry window
body.innerHTML = '<p style="margin: 5px">Paste your embed string here:</p><textarea id="embedplugin" style="width: 95%; margin: 5px" rows="10"></textarea>';
 
myEditor._windows.insertmedia = {
body: body
};
 
});
 
myEditor.on('editorContentLoaded', function () {
var b = this._getDoc().body;
var val = b.innerHTML;
var regex = /(<object .+?>.+?<\/object>)/gi;
 
var valReplaceInitial = val.replace(/"/g, "'");
var valReplace = valReplaceInitial.replace(regex, '<span class="yui-media" embeddedcode="$1" style="font-size:100px"></span>');
 
this.setEditorHTML(valReplace);
},
myEditor, true);
 
myEditor.render();
 
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.