source: trunk/phpgwapi/js/htmlarea/plugins/UploadImage/upload-image.js @ 2

Revision 2, 6.5 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1// UploadImage Plugin for HTMLArea-3.0
2// Implementation by Mihai Bazon.  Sponsored by http://thycotic.com
3//
4// htmlArea v3.0 - Copyright (c) 2002 interactivetools.com, inc.
5// This notice MUST stay intact for use (see license.txt).
6//
7// A free WYSIWYG editor replacement for <textarea> fields.
8// For full source code and docs, visit http://www.interactivetools.com/
9//
10// Version 3.0 developed by Mihai Bazon for InteractiveTools.
11//   http://dynarch.com/mishoo
12//
13
14function UploadImage(editor) {
15        this.editor = editor;
16
17        var cfg = editor.config;
18//      cfg.fullPage = true;
19        var tt = UploadImage.I18N;
20        var self = this;
21
22/*      cfg.registerButton("UploadImage", tt["Upload Image"], editor.imgURL("up_image.gif", "UploadImage"), false,
23        function(editor, id) {
24                self.buttonPress(editor, id);
25        });
26*/
27        cfg.registerButton("UploadImage", tt["Upload Image"], editor.imgURL("up_image.gif", "UploadImage"), false,
28                           function(editor, id) {
29                                   self.buttonPress();
30                           });
31
32        // add a new line in the toolbar
33        cfg.toolbar[0].splice(29, 0, "separator");
34        cfg.toolbar[0].splice(30, 0, "UploadImage");
35};
36
37UploadImage._pluginInfo = {
38        name          : "UploadImage",
39        version       : "1.0",
40        developer     : "Pim Snel",
41        developer_url : "http://lingewoud.com",
42        c_owner       : "Mihai Bazon",
43        sponsor       : "Lingewoud b.v. - Netherlands",
44        sponsor_url   : "http://lingewoud.com",
45        license       : "GPL"
46};
47
48UploadImage.prototype.zzzbuttonPress = function(editor, id)
49{
50        var self = this;
51        switch (id)
52        {
53            case "UploadImage":
54                var doc = editor._doc;
55                var links = doc.getElementsByTagName("link");
56                var style1 = '';
57                var style2 = '';
58                for (var i = links.length; --i >= 0;)
59                {
60                        var link = links[i];
61                        if (/stylesheet/i.test(link.rel))
62                        {
63                                if (/alternate/i.test(link.rel))
64                                        style2 = link.href;
65                                else
66                                        style1 = link.href;
67                        }
68                }
69                var title = doc.getElementsByTagName("title")[0];
70                title = title ? title.innerHTML : '';
71
72                var init =
73                {
74                        f_doctype      : editor.doctype,
75                        f_title        : title,
76                        f_body_bgcolor : HTMLArea._colorToRgb(doc.body.style.backgroundColor),
77                        f_body_fgcolor : HTMLArea._colorToRgb(doc.body.style.color),
78                        f_base_style   : style1,
79                        f_alt_style    : style2,
80
81                        editor         : editor
82                };
83
84                Dialog(_editor_url+"plugins/UploadImage/popups/insert_image.php", function(image)
85                {
86                        self._insertImage(image);
87//                      self.setDocProp(params);
88//                      alert(params[1]);
89                }, init);
90
91/*              editor._popupDialog("plugin://UploadImage/insert_image.php", function(params) {
92                                self.setDocProp(params);
93                                }, init);*/
94        break;
95        }
96};
97
98
99
100// Called when the user clicks on "InsertImage" button.  If an image is already
101// there, it will just modify it's properties.
102UploadImage.prototype.buttonPress = function(image) {
103       
104        var doc = editor._doc;
105//      var editor = this;      // for nested functions
106        var outparam = null;
107
108        var init =
109        {
110                f_doctype      : editor.doctype,
111                editor         : editor
112        };
113
114        if (typeof image == "undefined") {
115                image = editor.getParentElement();
116                if (image && !/^img$/i.test(image.tagName))
117                        image = null;
118        }
119
120        if (image) outparam = {
121                f_url    : HTMLArea.is_ie ? editor.stripBaseURL(image.src) : image.getAttribute("src"),
122                f_alt    : image.alt,
123                f_border : image.border,
124                f_align  : image.align,
125                f_vert   : image.vspace,
126                f_horiz  : image.hspace
127        };
128
129
130        Dialog(_editor_url+"plugins/UploadImage/popups/insert_image.php", function(param){
131        if (!param) {   // user must have pressed Cancel
132        return false;
133        }
134               
135                var img = image;
136//              alert(param.f_url);
137                if (!img) {
138                        var sel = editor._getSelection();
139                        var range = editor._createRange(sel);
140                        editor._doc.execCommand("insertimage", false, param.f_url);
141                       
142                        if (HTMLArea.is_ie) {
143                                img = range.parentElement();
144                                // wonder if this works...
145                                if (img.tagName.toLowerCase() != "img") {
146                                        img = img.previousSibling;
147                                }
148                        } else {
149                                img = range.startContainer.previousSibling;
150                        }
151                } else {
152                        img.src = param.f_url;
153                }
154                for (field in param) {
155                        var value = param[field];
156                        switch (field) {
157                                case "f_alt"    : img.alt        = value; break;
158                                case "f_border" : img.border = parseInt(value || "0"); break;
159                                case "f_align"  : img.align      = value; break;
160                                case "f_vert"   : img.vspace = parseInt(value || "0"); break;
161                                case "f_horiz"  : img.hspace = parseInt(value || "0"); break;
162                        }
163                }
164        }, outparam);
165};
166
167
168
169
170
171UploadImage.prototype.setDocProp = function(params) {
172        var txt = "";
173        var doc = this.editor._doc;
174        var head = doc.getElementsByTagName("head")[0];
175        var links = doc.getElementsByTagName("link");
176        var style1 = null;
177        var style2 = null;
178        for (var i = links.length; --i >= 0;) {
179                var link = links[i];
180                if (/stylesheet/i.test(link.rel)) {
181                        if (/alternate/i.test(link.rel))
182                                style2 = link;
183                        else
184                                style1 = link;
185                }
186        }
187        function createLink(alt) {
188                var link = doc.createElement("link");
189                link.rel = alt ? "alternate stylesheet" : "stylesheet";
190                head.appendChild(link);
191                return link;
192        };
193
194        if (!style1 && params.f_base_style)
195                style1 = createLink(false);
196        if (params.f_base_style)
197                style1.href = params.f_base_style;
198        else if (style1)
199                head.removeChild(style1);
200
201        if (!style2 && params.f_alt_style)
202                style2 = createLink(true);
203        if (params.f_alt_style)
204                style2.href = params.f_alt_style;
205        else if (style2)
206                head.removeChild(style2);
207
208
209        //cfg.registerButton("my-sample", "Class: sample", "ed_custom.gif", false,
210                        function testinsert(arg1,arg2) {
211                        if (HTMLArea.is_ie) {
212                        editor.insertHTML("<img src=\"arg1\" alt=\"\" />");
213                        var r = editor._doc.selection.createRange();
214                        r.move("character", -2);
215                        r.moveEnd("character", 2);
216                        r.select();
217                        } else { // Gecko/W3C compliant
218                        var n = editor._doc.createElement("img");
219                        n.className = "sample";
220                        editor.insertNodeAtSelection(n);
221                        var sel = editor._iframe.contentWindow.getSelection();
222                        sel.removeAllRanges();
223                        var r = editor._doc.createRange();
224                        r.setStart(n, 0);
225                        r.setEnd(n, 0);
226                        sel.addRange(r);
227                        }
228                        }
229
230                        testinsert(params[1]);
231
232                        for (var i in params) {
233
234       
235
236//              alert(params[i]);
237                var val = params[i];
238                switch (i) {
239                    case "f_title":
240                        var title = doc.getElementsByTagName("title")[0];
241                        if (!title) {
242                                title = doc.createElement("title");
243                                head.appendChild(title);
244                        } else while (node = title.lastChild)
245                                title.removeChild(node);
246                        if (!HTMLArea.is_ie)
247                                title.appendChild(doc.createTextNode(val));
248                        else
249                                doc.title = val;
250                        break;
251                    case "f_doctype":
252                        this.editor.setDoctype(val);
253                        break;
254                    case "f_body_bgcolor":
255                        doc.body.style.backgroundColor = val;
256                        break;
257                    case "f_body_fgcolor":
258                        doc.body.style.color = val;
259                        break;
260                }
261        }
262};
Note: See TracBrowser for help on using the repository browser.