annotate media/js/tiny_mce/plugins/layer/editor_plugin_src.js @ 106:cb72577785df

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