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"]],
132 style : 'fontFamily',
133 displayField: 'display',
134 optname : 'font-family',
183 // should we really allow this??
184 // should this just be
195 style : 'fontFamily',
196 displayField: 'display',
197 optname : 'font-family',
204 style : 'fontFamily',
205 displayField: 'display',
206 optname : 'font-family',
213 style : 'fontFamily',
214 displayField: 'display',
215 optname : 'font-family',
226 // this should be configurable.. - you can either set it up using stores, or modify options somehwere..
227 Roo.form.HtmlEditor.ToolbarContext.stores = false;
229 Roo.form.HtmlEditor.ToolbarContext.options = {
231 [ 'Helvetica,Arial,sans-serif', 'Helvetica'],
232 [ 'Courier New', 'Courier New'],
233 [ 'Tahoma', 'Tahoma'],
234 [ 'Times New Roman,serif', 'Times'],
235 [ 'Verdana','Verdana' ]
239 // fixme - these need to be configurable..
242 Roo.form.HtmlEditor.ToolbarContext.types
245 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
253 * @cfg {Object} disable List of toolbar elements to disable
258 * @cfg {Object} styles List of styles
259 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
261 * These must be defined in the page, so they get rendered correctly..
272 init : function(editor)
274 this.editor = editor;
275 Roo.log('HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!111');
278 var fid = editor.frameId;
280 function btn(id, toggle, handler){
281 var xid = fid + '-'+ id ;
285 cls : 'x-btn-icon x-edit-'+id,
286 enableToggle:toggle !== false,
287 scope: editor, // was editor...
288 handler:handler||editor.relayBtnCmd,
289 clickEvent:'mousedown',
290 tooltip: etb.buttonTips[id] || undefined, ///tips ???
294 // create a new element.
295 var wdiv = editor.wrap.createChild({
297 }, editor.wrap.dom.firstChild.nextSibling, true);
299 // can we do this more than once??
304 // disable everything...
305 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
310 this.toolbars[i] = this.buildToolbar(ty[i],i);
312 this.tb = this.toolbars.BODY;
316 editor.on('hide', function( ) { this.footer.hide() }, this);
317 editor.on('show', function( ) { this.footer.show() }, this);
320 this.rendered = true;
323 editor.on('editorevent', this.updateToolbar, this);
324 // other toolbars need to implement this..
325 //editor.on('editmodechange', this.updateToolbar, this);
331 * Protected method that will not generally be called directly. It triggers
332 * a toolbar update by reading the markup state of the current selection in the editor.
334 updateToolbar: function(editor,ev,sel){
337 // capture mouse up - this is handy for selecting images..
338 // perhaps should go somewhere else...
339 if(!this.editor.activated){
340 this.editor.onFirstFocus();
344 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
345 // selectNode - might want to handle IE?
347 (ev.type == 'mouseup' || ev.type == 'click' ) &&
348 ev.target && ev.target.tagName == 'IMG') {
349 // they have click on an image...
350 // let's see if we can change the selection...
353 var nodeRange = sel.ownerDocument.createRange();
355 nodeRange.selectNode(sel);
357 nodeRange.selectNodeContents(sel);
359 //nodeRange.collapse(true);
360 var s = editor.win.getSelection();
362 s.addRange(nodeRange);
366 var updateFooter = sel ? false : true;
369 var ans = this.editor.getAllAncestors();
372 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
375 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
376 sel = sel ? sel : this.editor.doc.body;
377 sel = sel.tagName.length ? sel : this.editor.doc.body;
380 // pick a menu that exists..
381 var tn = sel.tagName.toUpperCase();
382 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
384 tn = sel.tagName.toUpperCase();
386 var lastSel = this.tb.selectedNode
388 this.tb.selectedNode = sel;
390 // if current menu does not match..
391 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
394 ///console.log("show: " + tn);
395 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
398 this.tb.items.first().el.innerHTML = tn + ': ';
402 if (this.tb.fields) {
403 this.tb.fields.each(function(e) {
405 e.setValue(sel.style[e.stylename]);
408 e.setValue(sel.getAttribute(e.attrname));
412 var hasStyles = false;
413 for(var i in this.styles) {
420 var st = this.tb.fields.item(0);
422 st.store.removeAll();
425 var cn = sel.className.split(/\s+/);
428 if (this.styles['*']) {
430 Roo.each(this.styles['*'], function(v) {
431 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
434 if (this.styles[tn]) {
435 Roo.each(this.styles[tn], function(v) {
436 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
440 st.store.loadData(avs);
444 // flag our selected Node.
445 this.tb.selectedNode = sel;
448 Roo.menu.MenuMgr.hideAll();
453 //this.footDisp.dom.innerHTML = '';
460 this.footerEls = ans.reverse();
461 Roo.each(this.footerEls, function(a,i) {
463 html += html.length ? ' > ' : '';
465 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
470 var sz = this.footDisp.up('td').getSize();
471 this.footDisp.dom.style.width = (sz.width -10) + 'px';
472 this.footDisp.dom.style.marginLeft = '5px';
474 this.footDisp.dom.style.overflow = 'hidden';
476 this.footDisp.dom.innerHTML = html;
478 //this.editorsyncValue();
485 onDestroy : function(){
488 this.tb.items.each(function(item){
490 item.menu.removeAll();
492 item.menu.el.destroy();
500 onFirstFocus: function() {
501 // need to do this for all the toolbars..
502 this.tb.items.each(function(item){
506 buildToolbar: function(tlist, nm)
508 var editor = this.editor;
509 // create a new element.
510 var wdiv = editor.wrap.createChild({
512 }, editor.wrap.dom.firstChild.nextSibling, true);
515 var tb = new Roo.Toolbar(wdiv);
518 tb.add(nm+ ": ");
521 for(var i in this.styles) {
526 if (styles && styles.length) {
528 // this needs a multi-select checkbox...
529 tb.addField( new Roo.form.ComboBox({
530 store: new Roo.data.SimpleStore({
532 fields: ['val', 'selected'],
535 name : '-roo-edit-className',
536 attrname : 'className',
541 triggerAction: 'all',
542 emptyText:'Select Style',
546 'select': function(c, r, i) {
547 // initial support only for on class per el..
548 tb.selectedNode.className = r ? r.get('val') : '';
556 var tbc = Roo.form.HtmlEditor.ToolbarContext;
557 var tbops = tbc.options;
559 for (var i in tlist) {
562 tb.add(item.title + ": ");
565 //optname == used so you can configure the options available..
566 var opts = item.opts ? item.opts : false;
568 opts = tbops[item.optname];
573 // opts == pulldown..
574 tb.addField( new Roo.form.ComboBox({
575 store: typeof(tbc.stores[i]) != 'undefined' ? Roo.factory(tbc.stores[i],Roo.data) : new Roo.data.SimpleStore({
577 fields: ['val', 'display'],
580 name : '-roo-edit-' + i,
582 stylename : item.style ? item.style : false,
583 displayField: item.displayField ? item.displayField : 'val',
586 mode: typeof(tbc.stores[i]) != 'undefined' ? 'remote' : 'local',
588 triggerAction: 'all',
591 width: item.width ? item.width : 130,
593 'select': function(c, r, i) {
595 tb.selectedNode.style[c.stylename] = r.get('val');
598 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
607 tb.addField( new Roo.form.TextField({
615 tb.addField( new Roo.form.TextField({
616 name: '-roo-edit-' + i,
623 'change' : function(f, nv, ov) {
624 tb.selectedNode.setAttribute(f.attrname, nv);
639 // undo does not work.
641 var sn = tb.selectedNode;
643 var pn = sn.parentNode;
645 var stn = sn.childNodes[0];
646 var en = sn.childNodes[sn.childNodes.length - 1 ];
647 while (sn.childNodes.length) {
648 var node = sn.childNodes[0];
649 sn.removeChild(node);
651 pn.insertBefore(node, sn);
655 var range = editor.createRange();
657 range.setStart(stn,0);
658 range.setEnd(en,0); //????
659 //range.selectNode(sel);
662 var selection = editor.getSelection();
663 selection.removeAllRanges();
664 selection.addRange(range);
668 //_this.updateToolbar(null, null, pn);
669 _this.updateToolbar(null, null, null);
670 _this.footDisp.dom.innerHTML = '';
680 tb.el.on('click', function(e){
681 e.preventDefault(); // what does this do?
683 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
686 // dont need to disable them... as they will get hidden
691 buildFooter : function()
694 var fel = this.editor.wrap.createChild();
695 this.footer = new Roo.Toolbar(fel);
696 // toolbar has scrolly on left / right?
697 var footDisp= new Roo.Toolbar.Fill();
703 handler : function() {
704 _t.footDisp.scrollTo('left',0,true)
708 this.footer.add( footDisp );
713 handler : function() {
715 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
719 var fel = Roo.get(footDisp.el);
720 fel.addClass('x-editor-context');
721 this.footDispWrap = fel;
722 this.footDispWrap.overflow = 'hidden';
724 this.footDisp = fel.createChild();
725 this.footDispWrap.on('click', this.onContextClick, this)
729 onContextClick : function (ev,dom)
732 var cn = dom.className;
734 if (!cn.match(/x-ed-loc-/)) {
737 var n = cn.split('-').pop();
738 var ans = this.footerEls;
742 var range = this.editor.createRange();
744 range.selectNodeContents(sel);
745 //range.selectNode(sel);
748 var selection = this.editor.getSelection();
749 selection.removeAllRanges();
750 selection.addRange(range);
754 this.updateToolbar(null, null, sel);