1 // <script type="text/javascript">
5 * Copyright(c) 2006-2007, Ext JS, LLC.
12 * @class Roo.form.HtmlEditor.ToolbarContext
17 new Roo.form.HtmlEditor({
20 { xtype: 'ToolbarStandard', styles : {} }
21 { xtype: 'ToolbarContext', disable : {} }
27 * @config : {Object} disable List of elements to disable.. (not done yet.)
28 * @config : {Object} styles Map of styles available.
32 Roo.form.HtmlEditor.ToolbarContext = function(config)
35 Roo.apply(this, config);
36 //Roo.form.HtmlEditorToolbar1.superclass.constructor.call(this, editor.wrap.dom.firstChild, [], config);
37 // dont call parent... till later.
38 this.styles = this.styles || {};
41 Roo.form.HtmlEditor.ToolbarContext.fontFamilies = [
46 Roo.form.HtmlEditor.ToolbarContext.types = {
58 opts : [ [""],[ "left"],[ "right"],[ "center"],[ "top"]],
120 opts : [[""],[ "left"],[ "center"],[ "right"],[ "justify"],[ "char"]],
125 opts : [[""],[ "top"],[ "middle"],[ "bottom"],[ "baseline"]],
179 // should we really allow this??
180 // should this just be
193 Roo.form.HtmlEditor.ToolbarContext[ 'font-family'] = {
200 [ 'Times New Roman'],
207 Roo.each([ 'SPAN', 'DIV' , 'TD' , 'P' ], function(tg) {
208 if (typeof(Roo.form.HtmlEditor.ToolbarContext.types[tg]) == 'undefined') {
209 Roo.form.HtmlEditor.ToolbarContext.types[tg] = {};
212 Roo.form.HtmlEditor.ToolbarContext.types[tg]['font-family'] =
216 Roo.form.HtmlEditor.ToolbarContext.types
219 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
227 * @cfg {Object} disable List of toolbar elements to disable
232 * @cfg {Object} styles List of styles
233 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
235 * These must be defined in the page, so they get rendered correctly..
246 init : function(editor)
248 this.editor = editor;
251 var fid = editor.frameId;
253 function btn(id, toggle, handler){
254 var xid = fid + '-'+ id ;
258 cls : 'x-btn-icon x-edit-'+id,
259 enableToggle:toggle !== false,
260 scope: editor, // was editor...
261 handler:handler||editor.relayBtnCmd,
262 clickEvent:'mousedown',
263 tooltip: etb.buttonTips[id] || undefined, ///tips ???
267 // create a new element.
268 var wdiv = editor.wrap.createChild({
270 }, editor.wrap.dom.firstChild.nextSibling, true);
272 // can we do this more than once??
277 // disable everything...
278 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
283 this.toolbars[i] = this.buildToolbar(ty[i],i);
285 this.tb = this.toolbars.BODY;
289 editor.on('hide', function( ) { this.footer.hide() }, this);
290 editor.on('show', function( ) { this.footer.show() }, this);
293 this.rendered = true;
296 editor.on('editorevent', this.updateToolbar, this);
297 // other toolbars need to implement this..
298 //editor.on('editmodechange', this.updateToolbar, this);
304 * Protected method that will not generally be called directly. It triggers
305 * a toolbar update by reading the markup state of the current selection in the editor.
307 updateToolbar: function(editor,ev,sel){
310 // capture mouse up - this is handy for selecting images..
311 // perhaps should go somewhere else...
312 if(!this.editor.activated){
313 this.editor.onFirstFocus();
317 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
318 // selectNode - might want to handle IE?
320 (ev.type == 'mouseup' || ev.type == 'click' ) &&
321 ev.target && ev.target.tagName == 'IMG') {
322 // they have click on an image...
323 // let's see if we can change the selection...
326 var nodeRange = sel.ownerDocument.createRange();
328 nodeRange.selectNode(sel);
330 nodeRange.selectNodeContents(sel);
332 //nodeRange.collapse(true);
333 var s = editor.win.getSelection();
335 s.addRange(nodeRange);
339 var updateFooter = sel ? false : true;
342 var ans = this.editor.getAllAncestors();
345 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
348 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
349 sel = sel ? sel : this.editor.doc.body;
350 sel = sel.tagName.length ? sel : this.editor.doc.body;
353 // pick a menu that exists..
354 var tn = sel.tagName.toUpperCase();
355 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
357 tn = sel.tagName.toUpperCase();
359 var lastSel = this.tb.selectedNode
361 this.tb.selectedNode = sel;
363 // if current menu does not match..
364 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
367 ///console.log("show: " + tn);
368 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
371 this.tb.items.first().el.innerHTML = tn + ': ';
375 if (this.tb.fields) {
376 this.tb.fields.each(function(e) {
377 e.setValue(sel.getAttribute(e.attrname));
381 var hasStyles = false;
382 for(var i in this.styles) {
389 var st = this.tb.fields.item(0);
391 st.store.removeAll();
394 var cn = sel.className.split(/\s+/);
397 if (this.styles['*']) {
399 Roo.each(this.styles['*'], function(v) {
400 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
403 if (this.styles[tn]) {
404 Roo.each(this.styles[tn], function(v) {
405 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
409 st.store.loadData(avs);
413 // flag our selected Node.
414 this.tb.selectedNode = sel;
417 Roo.menu.MenuMgr.hideAll();
422 //this.footDisp.dom.innerHTML = '';
429 this.footerEls = ans.reverse();
430 Roo.each(this.footerEls, function(a,i) {
432 html += html.length ? ' > ' : '';
434 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
439 var sz = this.footDisp.up('td').getSize();
440 this.footDisp.dom.style.width = (sz.width -10) + 'px';
441 this.footDisp.dom.style.marginLeft = '5px';
443 this.footDisp.dom.style.overflow = 'hidden';
445 this.footDisp.dom.innerHTML = html;
447 //this.editorsyncValue();
454 onDestroy : function(){
457 this.tb.items.each(function(item){
459 item.menu.removeAll();
461 item.menu.el.destroy();
469 onFirstFocus: function() {
470 // need to do this for all the toolbars..
471 this.tb.items.each(function(item){
475 buildToolbar: function(tlist, nm)
477 var editor = this.editor;
478 // create a new element.
479 var wdiv = editor.wrap.createChild({
481 }, editor.wrap.dom.firstChild.nextSibling, true);
484 var tb = new Roo.Toolbar(wdiv);
487 tb.add(nm+ ": ");
490 for(var i in this.styles) {
495 if (styles && styles.length) {
497 // this needs a multi-select checkbox...
498 tb.addField( new Roo.form.ComboBox({
499 store: new Roo.data.SimpleStore({
501 fields: ['val', 'selected'],
504 name : '-roo-edit-className',
505 attrname : 'className',
510 triggerAction: 'all',
511 emptyText:'Select Style',
515 'select': function(c, r, i) {
516 // initial support only for on class per el..
517 tb.selectedNode.className = r ? r.get('val') : '';
527 for (var i in tlist) {
530 tb.add(item.title + ": ");
536 // opts == pulldown..
537 tb.addField( new Roo.form.ComboBox({
538 store: new Roo.data.SimpleStore({
543 name : '-roo-edit-' + i,
549 triggerAction: 'all',
552 width: item.width ? item.width : 130,
554 'select': function(c, r, i) {
555 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
564 tb.addField( new Roo.form.TextField({
572 tb.addField( new Roo.form.TextField({
573 name: '-roo-edit-' + i,
580 'change' : function(f, nv, ov) {
581 tb.selectedNode.setAttribute(f.attrname, nv);
596 // undo does not work.
598 var sn = tb.selectedNode;
600 var pn = sn.parentNode;
602 var stn = sn.childNodes[0];
603 var en = sn.childNodes[sn.childNodes.length - 1 ];
604 while (sn.childNodes.length) {
605 var node = sn.childNodes[0];
606 sn.removeChild(node);
608 pn.insertBefore(node, sn);
612 var range = editor.createRange();
614 range.setStart(stn,0);
615 range.setEnd(en,0); //????
616 //range.selectNode(sel);
619 var selection = editor.getSelection();
620 selection.removeAllRanges();
621 selection.addRange(range);
625 //_this.updateToolbar(null, null, pn);
626 _this.updateToolbar(null, null, null);
627 this.footDisp.dom.innerHTML = '';
637 tb.el.on('click', function(e){
638 e.preventDefault(); // what does this do?
640 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
643 // dont need to disable them... as they will get hidden
648 buildFooter : function()
651 var fel = this.editor.wrap.createChild();
652 this.footer = new Roo.Toolbar(fel);
653 // toolbar has scrolly on left / right?
654 var footDisp= new Roo.Toolbar.Fill();
660 handler : function() {
661 _t.footDisp.scrollTo('left',0,true)
665 this.footer.add( footDisp );
670 handler : function() {
672 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
676 var fel = Roo.get(footDisp.el);
677 fel.addClass('x-editor-context');
678 this.footDispWrap = fel;
679 this.footDispWrap.overflow = 'hidden';
681 this.footDisp = fel.createChild();
682 this.footDispWrap.on('click', this.onContextClick, this)
686 onContextClick : function (ev,dom)
689 var cn = dom.className;
691 if (!cn.match(/x-ed-loc-/)) {
694 var n = cn.split('-').pop();
695 var ans = this.footerEls;
699 var range = this.editor.createRange();
701 range.selectNodeContents(sel);
702 //range.selectNode(sel);
705 var selection = this.editor.getSelection();
706 selection.removeAllRanges();
707 selection.addRange(range);
711 this.updateToolbar(null, null, sel);