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 || {};
40 Roo.form.HtmlEditor.ToolbarContext.types = {
52 opts : [ [""],[ "left"],[ "right"],[ "center"],[ "top"]],
114 opts : [[""],[ "left"],[ "center"],[ "right"],[ "justify"],[ "char"]],
119 opts : [[""],[ "top"],[ "middle"],[ "bottom"],[ "baseline"]],
173 // should we really allow this??
174 // should this just be
189 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
197 * @cfg {Object} disable List of toolbar elements to disable
202 * @cfg {Object} styles List of styles
203 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
205 * These must be defined in the page, so they get rendered correctly..
216 init : function(editor)
218 this.editor = editor;
221 var fid = editor.frameId;
223 function btn(id, toggle, handler){
224 var xid = fid + '-'+ id ;
228 cls : 'x-btn-icon x-edit-'+id,
229 enableToggle:toggle !== false,
230 scope: editor, // was editor...
231 handler:handler||editor.relayBtnCmd,
232 clickEvent:'mousedown',
233 tooltip: etb.buttonTips[id] || undefined, ///tips ???
237 // create a new element.
238 var wdiv = editor.wrap.createChild({
240 }, editor.wrap.dom.firstChild.nextSibling, true);
242 // can we do this more than once??
247 // disable everything...
248 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
253 this.toolbars[i] = this.buildToolbar(ty[i],i);
255 this.tb = this.toolbars.BODY;
259 editor.on('hide', function( ) { this.footer.hide() }, this);
260 editor.on('show', function( ) { this.footer.show() }, this);
263 this.rendered = true;
266 editor.on('editorevent', this.updateToolbar, this);
267 // other toolbars need to implement this..
268 //editor.on('editmodechange', this.updateToolbar, this);
274 * Protected method that will not generally be called directly. It triggers
275 * a toolbar update by reading the markup state of the current selection in the editor.
277 updateToolbar: function(editor,ev,sel){
280 // capture mouse up - this is handy for selecting images..
281 // perhaps should go somewhere else...
282 if(!this.editor.activated){
283 this.editor.onFirstFocus();
287 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
288 // selectNode - might want to handle IE?
290 (ev.type == 'mouseup' || ev.type == 'click' ) &&
291 ev.target && ev.target.tagName == 'IMG') {
292 // they have click on an image...
293 // let's see if we can change the selection...
296 var nodeRange = sel.ownerDocument.createRange();
298 nodeRange.selectNode(sel);
300 nodeRange.selectNodeContents(sel);
302 //nodeRange.collapse(true);
303 var s = editor.win.getSelection();
305 s.addRange(nodeRange);
309 var updateFooter = sel ? false : true;
312 var ans = this.editor.getAllAncestors();
315 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
318 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
319 sel = sel ? sel : this.editor.doc.body;
320 sel = sel.tagName.length ? sel : this.editor.doc.body;
323 // pick a menu that exists..
324 var tn = sel.tagName.toUpperCase();
325 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
327 tn = sel.tagName.toUpperCase();
329 var lastSel = this.tb.selectedNode
331 this.tb.selectedNode = sel;
333 // if current menu does not match..
334 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
337 ///console.log("show: " + tn);
338 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
341 this.tb.items.first().el.innerHTML = tn + ': ';
345 if (this.tb.fields) {
346 this.tb.fields.each(function(e) {
347 e.setValue(sel.getAttribute(e.attrname));
351 var hasStyles = false;
352 for(var i in this.styles) {
359 var st = this.tb.fields.item(0);
361 st.store.removeAll();
364 var cn = sel.className.split(/\s+/);
367 if (this.styles['*']) {
369 Roo.each(this.styles['*'], function(v) {
370 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
373 if (this.styles[tn]) {
374 Roo.each(this.styles[tn], function(v) {
375 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
379 st.store.loadData(avs);
383 // flag our selected Node.
384 this.tb.selectedNode = sel;
387 Roo.menu.MenuMgr.hideAll();
392 this.footDisp.dom.innerHTML = '';
399 this.footerEls = ans.reverse();
400 Roo.each(this.footerEls, function(a,i) {
402 html += html.length ? ' > ' : '';
404 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
409 var sz = this.footDisp.up('td').getSize();
410 this.footDisp.dom.style.width = (sz.width -10) + 'px';
411 this.footDisp.dom.style.marginLeft = '5px';
413 this.footDisp.dom.style.overflow = 'hidden';
415 this.footDisp.dom.innerHTML = html;
417 //this.editorsyncValue();
422 onDestroy : function(){
425 this.tb.items.each(function(item){
427 item.menu.removeAll();
429 item.menu.el.destroy();
437 onFirstFocus: function() {
438 // need to do this for all the toolbars..
439 this.tb.items.each(function(item){
443 buildToolbar: function(tlist, nm)
445 var editor = this.editor;
446 // create a new element.
447 var wdiv = editor.wrap.createChild({
449 }, editor.wrap.dom.firstChild.nextSibling, true);
452 var tb = new Roo.Toolbar(wdiv);
455 tb.add(nm+ ": ");
458 for(var i in this.styles) {
463 if (styles && styles.length) {
465 // this needs a multi-select checkbox...
466 tb.addField( new Roo.form.ComboBox({
467 store: new Roo.data.SimpleStore({
469 fields: ['val', 'selected'],
472 name : '-roo-edit-className',
473 attrname : 'className',
478 triggerAction: 'all',
479 emptyText:'Select Style',
483 'select': function(c, r, i) {
484 // initial support only for on class per el..
485 tb.selectedNode.className = r ? r.get('val') : '';
495 for (var i in tlist) {
498 tb.add(item.title + ": ");
504 // opts == pulldown..
505 tb.addField( new Roo.form.ComboBox({
506 store: new Roo.data.SimpleStore({
511 name : '-roo-edit-' + i,
517 triggerAction: 'all',
520 width: item.width ? item.width : 130,
522 'select': function(c, r, i) {
523 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
532 tb.addField( new Roo.form.TextField({
540 tb.addField( new Roo.form.TextField({
541 name: '-roo-edit-' + i,
548 'change' : function(f, nv, ov) {
549 tb.selectedNode.setAttribute(f.attrname, nv);
564 // undo does not work.
566 var sn = tb.selectedNode;
568 var pn = sn.parentNode;
570 var stn = sn.childNodes[0];
571 var en = sn.childNodes[sn.childNodes.length - 1 ];
572 while (sn.childNodes.length) {
573 var node = sn.childNodes[0];
574 sn.removeChild(node);
576 pn.insertBefore(node, sn);
580 var range = editor.createRange();
582 range.setStart(stn,0);
583 range.setEnd(en,0); //????
584 //range.selectNode(sel);
587 var selection = editor.getSelection();
588 selection.removeAllRanges();
589 selection.addRange(range);
593 _this.updateToolbar(null, null, pn);
603 tb.el.on('click', function(e){
604 e.preventDefault(); // what does this do?
606 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
609 // dont need to disable them... as they will get hidden
614 buildFooter : function()
617 var fel = this.editor.wrap.createChild();
618 this.footer = new Roo.Toolbar(fel);
619 // toolbar has scrolly on left / right?
620 var footDisp= new Roo.Toolbar.Fill();
626 handler : function() {
627 _t.footDisp.scrollTo('left',0,true)
631 this.footer.add( footDisp );
636 handler : function() {
638 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
642 var fel = Roo.get(footDisp.el);
643 fel.addClass('x-editor-context');
644 this.footDispWrap = fel;
645 this.footDispWrap.overflow = 'hidden';
647 this.footDisp = fel.createChild();
648 this.footDispWrap.on('click', this.onContextClick, this)
652 onContextClick : function (ev,dom)
655 var cn = dom.className;
657 if (!cn.match(/x-ed-loc-/)) {
660 var n = cn.split('-').pop();
661 var ans = this.footerEls;
665 var range = this.editor.createRange();
667 range.selectNodeContents(sel);
668 //range.selectNode(sel);
671 var selection = this.editor.getSelection();
672 selection.removeAllRanges();
673 selection.addRange(range);
677 this.updateToolbar(null, null, sel);