annotate media/js/tiny_mce/plugins/layer/editor_plugin_src.js @ 201:bc958bc3c6eb

Futzing with CSS side blocks
author Brian Neal <bgneal@gmail.com>
date Mon, 12 Apr 2010 02:56:31 +0000
parents 149c3567fec1
children
rev   line source
bgneal@45 1 /**
bgneal@183 2 * editor_plugin_src.js
bgneal@45 3 *
bgneal@183 4 * Copyright 2009, Moxiecode Systems AB
bgneal@183 5 * Released under LGPL License.
bgneal@183 6 *
bgneal@183 7 * License: http://tinymce.moxiecode.com/license
bgneal@183 8 * Contributing: http://tinymce.moxiecode.com/contributing
bgneal@45 9 */
bgneal@45 10
bgneal@45 11 (function() {
bgneal@45 12 tinymce.create('tinymce.plugins.Layer', {
bgneal@45 13 init : function(ed, url) {
bgneal@45 14 var t = this;
bgneal@45 15
bgneal@45 16 t.editor = ed;
bgneal@45 17
bgneal@45 18 // Register commands
bgneal@45 19 ed.addCommand('mceInsertLayer', t._insertLayer, t);
bgneal@45 20
bgneal@45 21 ed.addCommand('mceMoveForward', function() {
bgneal@45 22 t._move(1);
bgneal@45 23 });
bgneal@45 24
bgneal@45 25 ed.addCommand('mceMoveBackward', function() {
bgneal@45 26 t._move(-1);
bgneal@45 27 });
bgneal@45 28
bgneal@45 29 ed.addCommand('mceMakeAbsolute', function() {
bgneal@45 30 t._toggleAbsolute();
bgneal@45 31 });
bgneal@45 32
bgneal@45 33 // Register buttons
bgneal@45 34 ed.addButton('moveforward', {title : 'layer.forward_desc', cmd : 'mceMoveForward'});
bgneal@45 35 ed.addButton('movebackward', {title : 'layer.backward_desc', cmd : 'mceMoveBackward'});
bgneal@45 36 ed.addButton('absolute', {title : 'layer.absolute_desc', cmd : 'mceMakeAbsolute'});
bgneal@45 37 ed.addButton('insertlayer', {title : 'layer.insertlayer_desc', cmd : 'mceInsertLayer'});
bgneal@45 38
bgneal@45 39 ed.onInit.add(function() {
bgneal@45 40 if (tinymce.isIE)
bgneal@45 41 ed.getDoc().execCommand('2D-Position', false, true);
bgneal@45 42 });
bgneal@45 43
bgneal@45 44 ed.onNodeChange.add(t._nodeChange, t);
bgneal@45 45 ed.onVisualAid.add(t._visualAid, t);
bgneal@45 46 },
bgneal@45 47
bgneal@45 48 getInfo : function() {
bgneal@45 49 return {
bgneal@45 50 longname : 'Layer',
bgneal@45 51 author : 'Moxiecode Systems AB',
bgneal@45 52 authorurl : 'http://tinymce.moxiecode.com',
bgneal@45 53 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/layer',
bgneal@45 54 version : tinymce.majorVersion + "." + tinymce.minorVersion
bgneal@45 55 };
bgneal@45 56 },
bgneal@45 57
bgneal@45 58 // Private methods
bgneal@45 59
bgneal@45 60 _nodeChange : function(ed, cm, n) {
bgneal@45 61 var le, p;
bgneal@45 62
bgneal@45 63 le = this._getParentLayer(n);
bgneal@45 64 p = ed.dom.getParent(n, 'DIV,P,IMG');
bgneal@45 65
bgneal@45 66 if (!p) {
bgneal@45 67 cm.setDisabled('absolute', 1);
bgneal@45 68 cm.setDisabled('moveforward', 1);
bgneal@45 69 cm.setDisabled('movebackward', 1);
bgneal@45 70 } else {
bgneal@45 71 cm.setDisabled('absolute', 0);
bgneal@45 72 cm.setDisabled('moveforward', !le);
bgneal@45 73 cm.setDisabled('movebackward', !le);
bgneal@45 74 cm.setActive('absolute', le && le.style.position.toLowerCase() == "absolute");
bgneal@45 75 }
bgneal@45 76 },
bgneal@45 77
bgneal@45 78 // Private methods
bgneal@45 79
bgneal@45 80 _visualAid : function(ed, e, s) {
bgneal@45 81 var dom = ed.dom;
bgneal@45 82
bgneal@45 83 tinymce.each(dom.select('div,p', e), function(e) {
bgneal@45 84 if (/^(absolute|relative|static)$/i.test(e.style.position)) {
bgneal@45 85 if (s)
bgneal@45 86 dom.addClass(e, 'mceItemVisualAid');
bgneal@45 87 else
bgneal@45 88 dom.removeClass(e, 'mceItemVisualAid');
bgneal@45 89 }
bgneal@45 90 });
bgneal@45 91 },
bgneal@45 92
bgneal@45 93 _move : function(d) {
bgneal@45 94 var ed = this.editor, i, z = [], le = this._getParentLayer(ed.selection.getNode()), ci = -1, fi = -1, nl;
bgneal@45 95
bgneal@45 96 nl = [];
bgneal@45 97 tinymce.walk(ed.getBody(), function(n) {
bgneal@45 98 if (n.nodeType == 1 && /^(absolute|relative|static)$/i.test(n.style.position))
bgneal@45 99 nl.push(n);
bgneal@45 100 }, 'childNodes');
bgneal@45 101
bgneal@45 102 // Find z-indexes
bgneal@45 103 for (i=0; i<nl.length; i++) {
bgneal@45 104 z[i] = nl[i].style.zIndex ? parseInt(nl[i].style.zIndex) : 0;
bgneal@45 105
bgneal@45 106 if (ci < 0 && nl[i] == le)
bgneal@45 107 ci = i;
bgneal@45 108 }
bgneal@45 109
bgneal@45 110 if (d < 0) {
bgneal@45 111 // Move back
bgneal@45 112
bgneal@45 113 // Try find a lower one
bgneal@45 114 for (i=0; i<z.length; i++) {
bgneal@45 115 if (z[i] < z[ci]) {
bgneal@45 116 fi = i;
bgneal@45 117 break;
bgneal@45 118 }
bgneal@45 119 }
bgneal@45 120
bgneal@45 121 if (fi > -1) {
bgneal@45 122 nl[ci].style.zIndex = z[fi];
bgneal@45 123 nl[fi].style.zIndex = z[ci];
bgneal@45 124 } else {
bgneal@45 125 if (z[ci] > 0)
bgneal@45 126 nl[ci].style.zIndex = z[ci] - 1;
bgneal@45 127 }
bgneal@45 128 } else {
bgneal@45 129 // Move forward
bgneal@45 130
bgneal@45 131 // Try find a higher one
bgneal@45 132 for (i=0; i<z.length; i++) {
bgneal@45 133 if (z[i] > z[ci]) {
bgneal@45 134 fi = i;
bgneal@45 135 break;
bgneal@45 136 }
bgneal@45 137 }
bgneal@45 138
bgneal@45 139 if (fi > -1) {
bgneal@45 140 nl[ci].style.zIndex = z[fi];
bgneal@45 141 nl[fi].style.zIndex = z[ci];
bgneal@45 142 } else
bgneal@45 143 nl[ci].style.zIndex = z[ci] + 1;
bgneal@45 144 }
bgneal@45 145
bgneal@45 146 ed.execCommand('mceRepaint');
bgneal@45 147 },
bgneal@45 148
bgneal@45 149 _getParentLayer : function(n) {
bgneal@45 150 return this.editor.dom.getParent(n, function(n) {
bgneal@45 151 return n.nodeType == 1 && /^(absolute|relative|static)$/i.test(n.style.position);
bgneal@45 152 });
bgneal@45 153 },
bgneal@45 154
bgneal@45 155 _insertLayer : function() {
bgneal@45 156 var ed = this.editor, p = ed.dom.getPos(ed.dom.getParent(ed.selection.getNode(), '*'));
bgneal@45 157
bgneal@45 158 ed.dom.add(ed.getBody(), 'div', {
bgneal@45 159 style : {
bgneal@45 160 position : 'absolute',
bgneal@45 161 left : p.x,
bgneal@45 162 top : (p.y > 20 ? p.y : 20),
bgneal@45 163 width : 100,
bgneal@45 164 height : 100
bgneal@45 165 },
bgneal@45 166 'class' : 'mceItemVisualAid'
bgneal@45 167 }, ed.selection.getContent() || ed.getLang('layer.content'));
bgneal@45 168 },
bgneal@45 169
bgneal@45 170 _toggleAbsolute : function() {
bgneal@45 171 var ed = this.editor, le = this._getParentLayer(ed.selection.getNode());
bgneal@45 172
bgneal@45 173 if (!le)
bgneal@45 174 le = ed.dom.getParent(ed.selection.getNode(), 'DIV,P,IMG');
bgneal@45 175
bgneal@45 176 if (le) {
bgneal@45 177 if (le.style.position.toLowerCase() == "absolute") {
bgneal@45 178 ed.dom.setStyles(le, {
bgneal@45 179 position : '',
bgneal@45 180 left : '',
bgneal@45 181 top : '',
bgneal@45 182 width : '',
bgneal@45 183 height : ''
bgneal@45 184 });
bgneal@45 185
bgneal@45 186 ed.dom.removeClass(le, 'mceItemVisualAid');
bgneal@45 187 } else {
bgneal@45 188 if (le.style.left == "")
bgneal@45 189 le.style.left = 20 + 'px';
bgneal@45 190
bgneal@45 191 if (le.style.top == "")
bgneal@45 192 le.style.top = 20 + 'px';
bgneal@45 193
bgneal@45 194 if (le.style.width == "")
bgneal@45 195 le.style.width = le.width ? (le.width + 'px') : '100px';
bgneal@45 196
bgneal@45 197 if (le.style.height == "")
bgneal@45 198 le.style.height = le.height ? (le.height + 'px') : '100px';
bgneal@45 199
bgneal@45 200 le.style.position = "absolute";
bgneal@45 201 ed.addVisual(ed.getBody());
bgneal@45 202 }
bgneal@45 203
bgneal@45 204 ed.execCommand('mceRepaint');
bgneal@45 205 ed.nodeChanged();
bgneal@45 206 }
bgneal@45 207 }
bgneal@45 208 });
bgneal@45 209
bgneal@45 210 // Register plugin
bgneal@45 211 tinymce.PluginManager.add('layer', tinymce.plugins.Layer);
bgneal@45 212 })();