Skip to content

Instantly share code, notes, and snippets.

@lslinnet
Created November 23, 2009 10:04
Show Gist options
  • Save lslinnet/240993 to your computer and use it in GitHub Desktop.
Save lslinnet/240993 to your computer and use it in GitHub Desktop.
function customcode() {
Drupal.editor.editor.on('toolbarLoaded',function() {
var customcodeButton = {type: 'push',label: 'Insert Custom code', value: 'insertcustomcode'};
this.toolbar.addButtonToGroup(customcodeButton,'specialbuttons');
var Dom = YAHOO.util.Dom,
Event = YAHOO.util.Event,
Lang = YAHOO.lang;
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('embedcustomcode');
var val = textarea.value;
el.setAttribute('embeddedcode',encodeURIComponent(textarea.value));
textarea.value = '';
this.nodeChange();
};
var _handleMediaWindow = function() {
var el = this.currentElement[0];
var win = new YAHOO.widget.EditorWindow('insertcustomcode',{width: '625px'});
var bFlag = false;
win.setHeader(Drupal.t('Edit Custom code'));
this.openWindow(win);
this.on('afterOpenWindow',function() {
this.get('panel').syncIframe();
}, this, true);
if (null != el.getAttribute('embeddedcode')) {
var textarea = document.getElementById('embedcustomcode');
if(null != textarea) {
textarea.value = decodeURIComponent(el.getAttribute('embeddedcode'));
} else {
bFlag = true;
}
}
};
this.toolbar.on('insertcustomcodeClick',function() {
var el = this._getSelectedElement();
if(Dom.hasClass(el, 'yui-customcode')) {
this.currentElement = [el];
handleInsertIcon.call(this);
return false;
}
},this, true);
this.cmd_insertcustomcode = function() {
if (!Dom.hasClass(this._getSelectedElement(), 'yui-customcode')) {
this.execCommand('insertimage', 'none');
var el = this._swapEl(this.currentElement[0], 'div', function(el) {
el.className = 'yui-customcode';
el.innerHTML = ' ';
});
this.currentElement = [el];
}
_handleMediaWindow.call(this);
return [false];
}
this.on('editorDoubleClick',function () {
var el = this._getSelectedElement();
if (Dom.hasClass(el, 'yui-customcode')) {
this.currentElement = [el];
_handleMediaWindow.call(this);
return false;
}
},
this,true);
this.on('afterNodeChange',function() {
if (this._hasSelection()) {
this.toolbar.disableButton('insertcustomcode');
} else {
this.toolbar.enableButton('insertcustomcode');
var el = this._getSelectedElement();
if (Dom.hasClass(el, 'yui-customcode')) {
this.toolbar.selectButton('insertcustomcode');
} else {
this.toolbar.deselectButton('insertcustomcode');
}
}
},
this,true);
this.on('windowinsertcustomcodeClose', function() {
_handleWindowClose.call(this);
},
this, true);
this.on('windowRender', function() {
var body = document.createElement('div');
body.innerHTML = '<div style="margin-left: 5px">' + Drupal.t('Paste your custom code here:') +
'</div><textarea id="embedcustomcode" style="width: 95%; margin-left: 5px" rows="10"></textarea>';
this._windows.insertcustomcode = {
body: body
};
});
},
Drupal.editor.editor, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment