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 || {};
43 Roo.form.HtmlEditor.ToolbarContext.types = {
55 opts : [ [""],[ "left"],[ "right"],[ "center"],[ "top"]],
117 opts : [[""],[ "left"],[ "center"],[ "right"],[ "justify"],[ "char"]],
122 opts : [[""],[ "top"],[ "middle"],[ "bottom"],[ "baseline"]],
176 // should we really allow this??
177 // should this just be
190 Roo.form.HtmlEditor.ToolbarContext[ 'font-family'] = {
192 style : 'fontFamily',
195 [ 'helvetica,arial,sans-serif'],
198 [ 'Times New Roman'],
205 Roo.each([ 'SPAN', 'DIV' , 'TD' , 'P' ], function(tg) {
206 var cx= Roo.form.HtmlEditor.ToolbarContext;
207 if (typeof(cx.types[tg]) == 'undefined') {
211 cx.types[tg]['font-family'] = cx[ 'font-family'] ;
214 Roo.form.HtmlEditor.ToolbarContext.types
217 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
225 * @cfg {Object} disable List of toolbar elements to disable
230 * @cfg {Object} styles List of styles
231 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
233 * These must be defined in the page, so they get rendered correctly..
244 init : function(editor)
246 this.editor = editor;
249 var fid = editor.frameId;
251 function btn(id, toggle, handler){
252 var xid = fid + '-'+ id ;
256 cls : 'x-btn-icon x-edit-'+id,
257 enableToggle:toggle !== false,
258 scope: editor, // was editor...
259 handler:handler||editor.relayBtnCmd,
260 clickEvent:'mousedown',
261 tooltip: etb.buttonTips[id] || undefined, ///tips ???
265 // create a new element.
266 var wdiv = editor.wrap.createChild({
268 }, editor.wrap.dom.firstChild.nextSibling, true);
270 // can we do this more than once??
275 // disable everything...
276 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
281 this.toolbars[i] = this.buildToolbar(ty[i],i);
283 this.tb = this.toolbars.BODY;
287 editor.on('hide', function( ) { this.footer.hide() }, this);
288 editor.on('show', function( ) { this.footer.show() }, this);
291 this.rendered = true;
294 editor.on('editorevent', this.updateToolbar, this);
295 // other toolbars need to implement this..
296 //editor.on('editmodechange', this.updateToolbar, this);
302 * Protected method that will not generally be called directly. It triggers
303 * a toolbar update by reading the markup state of the current selection in the editor.
305 updateToolbar: function(editor,ev,sel){
308 // capture mouse up - this is handy for selecting images..
309 // perhaps should go somewhere else...
310 if(!this.editor.activated){
311 this.editor.onFirstFocus();
315 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
316 // selectNode - might want to handle IE?
318 (ev.type == 'mouseup' || ev.type == 'click' ) &&
319 ev.target && ev.target.tagName == 'IMG') {
320 // they have click on an image...
321 // let's see if we can change the selection...
324 var nodeRange = sel.ownerDocument.createRange();
326 nodeRange.selectNode(sel);
328 nodeRange.selectNodeContents(sel);
330 //nodeRange.collapse(true);
331 var s = editor.win.getSelection();
333 s.addRange(nodeRange);
337 var updateFooter = sel ? false : true;
340 var ans = this.editor.getAllAncestors();
343 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
346 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
347 sel = sel ? sel : this.editor.doc.body;
348 sel = sel.tagName.length ? sel : this.editor.doc.body;
351 // pick a menu that exists..
352 var tn = sel.tagName.toUpperCase();
353 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
355 tn = sel.tagName.toUpperCase();
357 var lastSel = this.tb.selectedNode
359 this.tb.selectedNode = sel;
361 // if current menu does not match..
362 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
365 ///console.log("show: " + tn);
366 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
369 this.tb.items.first().el.innerHTML = tn + ': ';
373 if (this.tb.fields) {
374 this.tb.fields.each(function(e) {
376 e.setValue(sel.style[e.stylename]);
379 e.setValue(sel.getAttribute(e.attrname));
383 var hasStyles = false;
384 for(var i in this.styles) {
391 var st = this.tb.fields.item(0);
393 st.store.removeAll();
396 var cn = sel.className.split(/\s+/);
399 if (this.styles['*']) {
401 Roo.each(this.styles['*'], function(v) {
402 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
405 if (this.styles[tn]) {
406 Roo.each(this.styles[tn], function(v) {
407 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
411 st.store.loadData(avs);
415 // flag our selected Node.
416 this.tb.selectedNode = sel;
419 Roo.menu.MenuMgr.hideAll();
424 //this.footDisp.dom.innerHTML = '';
431 this.footerEls = ans.reverse();
432 Roo.each(this.footerEls, function(a,i) {
434 html += html.length ? ' > ' : '';
436 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
441 var sz = this.footDisp.up('td').getSize();
442 this.footDisp.dom.style.width = (sz.width -10) + 'px';
443 this.footDisp.dom.style.marginLeft = '5px';
445 this.footDisp.dom.style.overflow = 'hidden';
447 this.footDisp.dom.innerHTML = html;
449 //this.editorsyncValue();
456 onDestroy : function(){
459 this.tb.items.each(function(item){
461 item.menu.removeAll();
463 item.menu.el.destroy();
471 onFirstFocus: function() {
472 // need to do this for all the toolbars..
473 this.tb.items.each(function(item){
477 buildToolbar: function(tlist, nm)
479 var editor = this.editor;
480 // create a new element.
481 var wdiv = editor.wrap.createChild({
483 }, editor.wrap.dom.firstChild.nextSibling, true);
486 var tb = new Roo.Toolbar(wdiv);
489 tb.add(nm+ ": ");
492 for(var i in this.styles) {
497 if (styles && styles.length) {
499 // this needs a multi-select checkbox...
500 tb.addField( new Roo.form.ComboBox({
501 store: new Roo.data.SimpleStore({
503 fields: ['val', 'selected'],
506 name : '-roo-edit-className',
507 attrname : 'className',
512 triggerAction: 'all',
513 emptyText:'Select Style',
517 'select': function(c, r, i) {
518 // initial support only for on class per el..
519 tb.selectedNode.className = r ? r.get('val') : '';
529 for (var i in tlist) {
532 tb.add(item.title + ": ");
538 // opts == pulldown..
539 tb.addField( new Roo.form.ComboBox({
540 store: new Roo.data.SimpleStore({
545 name : '-roo-edit-' + i,
547 stylename : item.style ? item.style : false,
552 triggerAction: 'all',
555 width: item.width ? item.width : 130,
557 'select': function(c, r, i) {
559 tb.selectedNode.style[c.stylename] = r.get('val');
562 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
571 tb.addField( new Roo.form.TextField({
579 tb.addField( new Roo.form.TextField({
580 name: '-roo-edit-' + i,
587 'change' : function(f, nv, ov) {
588 tb.selectedNode.setAttribute(f.attrname, nv);
603 // undo does not work.
605 var sn = tb.selectedNode;
607 var pn = sn.parentNode;
609 var stn = sn.childNodes[0];
610 var en = sn.childNodes[sn.childNodes.length - 1 ];
611 while (sn.childNodes.length) {
612 var node = sn.childNodes[0];
613 sn.removeChild(node);
615 pn.insertBefore(node, sn);
619 var range = editor.createRange();
621 range.setStart(stn,0);
622 range.setEnd(en,0); //????
623 //range.selectNode(sel);
626 var selection = editor.getSelection();
627 selection.removeAllRanges();
628 selection.addRange(range);
632 //_this.updateToolbar(null, null, pn);
633 _this.updateToolbar(null, null, null);
634 this.footDisp.dom.innerHTML = '';
644 tb.el.on('click', function(e){
645 e.preventDefault(); // what does this do?
647 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
650 // dont need to disable them... as they will get hidden
655 buildFooter : function()
658 var fel = this.editor.wrap.createChild();
659 this.footer = new Roo.Toolbar(fel);
660 // toolbar has scrolly on left / right?
661 var footDisp= new Roo.Toolbar.Fill();
667 handler : function() {
668 _t.footDisp.scrollTo('left',0,true)
672 this.footer.add( footDisp );
677 handler : function() {
679 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
683 var fel = Roo.get(footDisp.el);
684 fel.addClass('x-editor-context');
685 this.footDispWrap = fel;
686 this.footDispWrap.overflow = 'hidden';
688 this.footDisp = fel.createChild();
689 this.footDispWrap.on('click', this.onContextClick, this)
693 onContextClick : function (ev,dom)
696 var cn = dom.className;
698 if (!cn.match(/x-ed-loc-/)) {
701 var n = cn.split('-').pop();
702 var ans = this.footerEls;
706 var range = this.editor.createRange();
708 range.selectNodeContents(sel);
709 //range.selectNode(sel);
712 var selection = this.editor.getSelection();
713 selection.removeAllRanges();
714 selection.addRange(range);
718 this.updateToolbar(null, null, sel);