~ubuntu-it-wiki/wiki-ubuntu-it/wiki-repo

« back to all changes in this revision

Viewing changes to applets/moinFCKplugins/moinlink/fck_link.js

  • Committer: Leo Iannacone
  • Date: 2011-06-02 15:05:37 UTC
  • Revision ID: l3on@ubuntu.com-20110602150537-ycrnf58qf67uf593
Added applets for gui editor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * FCKeditor - The text editor for internet
 
3
 * Copyright (C) 2003-2004 Frederico Caldeira Knabben
 
4
 * 
 
5
 * Licensed under the terms of the GNU Lesser General Public License:
 
6
 *   http://www.opensource.org/licenses/lgpl-license.php
 
7
 * 
 
8
 * For further information visit:
 
9
 *   http://www.fckeditor.net/
 
10
 * 
 
11
 * File Name: fck_link.js
 
12
 *  Scripts related to the Link dialog window (see fck_link.html).
 
13
 * 
 
14
 * Version:  2.0 FC (Preview)
 
15
 * Modified: 2005-03-12 15:16:39
 
16
 * 
 
17
 * File Authors:
 
18
 *   Frederico Caldeira Knabben (fredck@fckeditor.net)
 
19
 */
 
20
 
 
21
var dialog      = window.parent ;
 
22
var oEditor  = dialog.InnerDialogLoaded();
 
23
var FCK   = oEditor.FCK;
 
24
var FCKLang  = oEditor.FCKLang;
 
25
var FCKConfig = oEditor.FCKConfig;
 
26
var isNameRelatedLink = false;
 
27
 
 
28
//#### Dialog Tabs
 
29
 
 
30
// Set the dialog tabs.
 
31
window.parent.AddTab('Info', FCKLang.DlgLnkInfoTab);
 
32
 
 
33
// Function called when a dialog tag is selected.
 
34
function OnDialogTabChange(tabCode)
 
35
{
 
36
  ShowE('divInfo'  , (tabCode == 'Info'));
 
37
}
 
38
 
 
39
// Extends the String object, creating a "EndsWith" method on it.
 
40
// this method is part of fckeditor dialog common library
 
41
String.prototype.EndsWith = function( value, ignoreCase )
 
42
{
 
43
  var L1 = this.length ;
 
44
  var L2 = value.length ;
 
45
 
 
46
  if ( L2 > L1 )
 
47
    return false ;
 
48
 
 
49
  if ( ignoreCase )
 
50
  {
 
51
    var oRegex = new RegExp( value + '$' , 'i' ) ;
 
52
    return oRegex.test( this ) ;
 
53
  }
 
54
  else
 
55
    return ( L2 == 0 || this.substr( L1 - L2, L2 ) == value ) ;
 
56
}
 
57
 
 
58
//#### Regular Expressions library.
 
59
var oRegex = new Object();
 
60
 
 
61
oRegex.UriProtocol = new RegExp('');
 
62
oRegex.UriProtocol.compile('^(((http|https|ftp|file|news):\/\/)|mailto:)', 'gi');
 
63
 
 
64
oRegex.UrlOnChangeProtocol = new RegExp('');
 
65
oRegex.UrlOnChangeProtocol.compile('^(http|https|ftp|file|news)://(?=.)', 'gi');
 
66
 
 
67
oRegex.UrlOnChangeTestOther = new RegExp('');
 
68
oRegex.UrlOnChangeTestOther.compile('^(javascript:|#)', 'gi'); // was: (...#|/)
 
69
 
 
70
oRegex.ReserveTarget = new RegExp('');
 
71
oRegex.ReserveTarget.compile('^_(blank|self|top|parent)$', 'i');
 
72
 
 
73
//#### Parser Functions
 
74
 
 
75
var oParser = new Object();
 
76
 
 
77
oParser.ParseEMailUrl = function(emailUrl)
 
78
{
 
79
  // Initializes the EMailInfo object.
 
80
  var oEMailInfo = new Object();
 
81
  oEMailInfo.Address = '';
 
82
  oEMailInfo.Subject = '';
 
83
  oEMailInfo.Body  = '';
 
84
 
 
85
  var oParts = emailUrl.match(/^([^\?]+)\??(.+)?/);
 
86
  if (oParts)
 
87
  {
 
88
    // Set the e-mail address.
 
89
    oEMailInfo.Address = oParts[1];
 
90
 
 
91
    // Look for the optional e-mail parameters.
 
92
    if (oParts[2])
 
93
    {
 
94
      var oMatch = oParts[2].match(/(^|&)subject=([^&]+)/i);
 
95
      if (oMatch)
 
96
        oEMailInfo.Subject = unescape(oMatch[2]);
 
97
 
 
98
      oMatch = oParts[2].match(/(^|&)body=([^&]+)/i);
 
99
      if (oMatch)
 
100
        oEMailInfo.Body = unescape(oMatch[2]);
 
101
    }
 
102
  }
 
103
  return oEMailInfo;
 
104
}
 
105
 
 
106
oParser.CreateEMailUri = function(address, subject, body)
 
107
{
 
108
  var sBaseUri = 'mailto:' + address;
 
109
 
 
110
  var sParams = '';
 
111
 
 
112
  if (subject.length > 0)
 
113
    sParams = '?subject=' + escape(subject);
 
114
 
 
115
  if (body.length > 0)
 
116
  {
 
117
    sParams += (sParams.length == 0 ? '?' : '&');
 
118
    sParams += 'body=' + escape(body);
 
119
  }
 
120
 
 
121
  return sBaseUri + sParams;
 
122
}
 
123
 
 
124
//#### Initialization Code
 
125
 
 
126
// oLink: The actual selected link in the editor.
 
127
var oLink = dialog.Selection.GetSelection().MoveToAncestorNode( 'A' ) ;
 
128
if ( oLink )
 
129
  FCK.Selection.SelectNode( oLink ) ;
 
130
 
 
131
window.onload = function()
 
132
{
 
133
  // Translate the dialog box texts.
 
134
  oEditor.FCKLanguageManager.TranslatePage(document);
 
135
 
 
136
  // Load the selected link information (if any).
 
137
  var firstElement = LoadSelection();
 
138
 
 
139
  // Update the dialog box.
 
140
  SetLinkType(GetE('cmbLinkType').value);
 
141
 
 
142
  // Show the initial dialog content.
 
143
  GetE('divInfo').style.display = '';
 
144
 
 
145
  // Activate the "OK" button.
 
146
  window.parent.SetOkButton(true);
 
147
 
 
148
  // select first text input element of dialog for usability
 
149
  SelectField(firstElement);
 
150
}
 
151
 
 
152
function LoadSelection()
 
153
{
 
154
  // variable for first element of dialog
 
155
  var firstElement = 'txtPagename';
 
156
 
 
157
  if (!oLink) return firstElement;
 
158
 
 
159
  var sType = 'url';
 
160
 
 
161
  // Get the actual Link href.
 
162
  var sHRef = ''+oLink.getAttribute('href',2);
 
163
 
 
164
  // Search for the protocol.
 
165
  var sProtocol = oRegex.UriProtocol.exec(sHRef);
 
166
 
 
167
  if (sProtocol)
 
168
  {
 
169
    sProtocol = sProtocol[0].toLowerCase();
 
170
    GetE('cmbLinkProtocol').value = sProtocol;
 
171
 
 
172
    // Remove the protocol and get the remainig URL.
 
173
    var sUrl = sHRef.replace(oRegex.UriProtocol, '');
 
174
 
 
175
    if (sProtocol == 'mailto:') // It is an e-mail link.
 
176
    {
 
177
      var oEMailInfo = oParser.ParseEMailUrl(sUrl);
 
178
      GetE('txtUrl').value = oEMailInfo.Address;
 
179
    }
 
180
    else    // It is a normal link.
 
181
    {
 
182
      sType = 'url';
 
183
      GetE('txtUrl').value = sUrl;
 
184
    }
 
185
 
 
186
    firstElement  = 'txtUrl';
 
187
  }
 
188
  else if (oLink.getAttribute('class')=='interwiki' || oLink.getAttribute('class')=='badinterwiki')
 
189
  {
 
190
    sType = 'interwiki';
 
191
    GetE('sctInterwiki').value = oLink.getAttribute('title');
 
192
    GetE('txtInterwikipagename').value = decodeUrl(sHRef);
 
193
    firstElement = 'txtInterwikipagename';
 
194
  }
 
195
  else if (sHRef.StartsWith(FCKConfig['WikiBasePath']))
 
196
  {
 
197
    sType = 'wiki';
 
198
    sHRef = sHRef.Remove(0, FCKConfig['WikiBasePath'].length);
 
199
 
 
200
    // make links to subpages of own page relative links
 
201
    if (sHRef.StartsWith(FCKConfig['WikiPage']))
 
202
      sHRef = sHRef.Remove(0, FCKConfig['WikiPage'].length);
 
203
 
 
204
    // relative link ../
 
205
    if (oLink.innerHTML.StartsWith('../') && sHRef.EndsWith(oLink.innerHTML.substring(3, oLink.innerHTML.length))) 
 
206
      sHRef = oLink.innerHTML;
 
207
 
 
208
    GetE('txtPagename').value = decodeUrl(sHRef);
 
209
    firstElement  = 'txtPagename';
 
210
  }
 
211
  else     // It is another type of link.
 
212
  {
 
213
    sType = 'url';
 
214
 
 
215
    GetE('cmbLinkProtocol').value = '';
 
216
    GetE('txtUrl').value = sHRef;
 
217
    firstElement  = 'txtUrl';
 
218
  }
 
219
 
 
220
  // Update the Link type combo.
 
221
  GetE('cmbLinkType').value = sType;
 
222
 
 
223
  // when inner html of link and url of link are same set isNameRelatedLink true
 
224
  // if isNameRelatedLink is true, inner html is change when url change
 
225
  if (sHRef == oLink.innerHTML) {
 
226
    isNameRelatedLink = true;
 
227
  }
 
228
 
 
229
  return firstElement;
 
230
}
 
231
 
 
232
//#### Link type selection.
 
233
function SetLinkType(linkType)
 
234
{
 
235
  ShowE('divLinkTypeWiki'  , (linkType == 'wiki'));
 
236
  ShowE('divLinkTypeInterwiki' , (linkType == 'interwiki'));
 
237
  ShowE('divLinkTypeUrl'  , (linkType == 'url'));
 
238
}
 
239
 
 
240
//#### Called when user selects Wikipage.
 
241
function OnChangePagename(pagename)
 
242
{
 
243
  GetE("txtPagename").value = pagename;
 
244
}
 
245
 
 
246
//#### Called while the user types the URL.
 
247
function OnUrlChange()
 
248
{
 
249
  var sUrl = GetE('txtUrl').value;
 
250
  var sProtocol = oRegex.UrlOnChangeProtocol.exec(sUrl);
 
251
 
 
252
  if (sProtocol)
 
253
  {
 
254
    sUrl = sUrl.substr(sProtocol[0].length);
 
255
    GetE('txtUrl').value = sUrl;
 
256
    GetE('cmbLinkProtocol').value = sProtocol[0].toLowerCase();
 
257
  }
 
258
  else if (oRegex.UrlOnChangeTestOther.test(sUrl))
 
259
  {
 
260
    GetE('cmbLinkProtocol').value = '';
 
261
  }
 
262
}
 
263
 
 
264
//#### The OK button was hit.
 
265
function Ok()
 
266
{
 
267
  var sUri;
 
268
  var sText = '';
 
269
 
 
270
  switch (GetE('cmbLinkType').value)
 
271
  {
 
272
    case 'wiki' :
 
273
      sUri = GetE('txtPagename').value;
 
274
      if (sUri.length == 0)
 
275
      {
 
276
        alert(FCKLang.DlnLnkMsgNoUrl);
 
277
        return false;
 
278
      }
 
279
 
 
280
      sText = sUri;
 
281
 
 
282
      // pages starting with "/" are sub pages of current page, e.g. /SubPage 
 
283
      if (sUri[0] == '/')
 
284
      {
 
285
        sUri = GetE('basepage').value + sUri
 
286
      }
 
287
 
 
288
      sUri = FCKConfig['WikiBasePath'] + encodeUrl(sUri);
 
289
      break;
 
290
 
 
291
    case 'interwiki' :
 
292
      sUri = GetE('txtInterwikipagename').value;
 
293
 
 
294
      if (sUri.length == 0)
 
295
      {
 
296
        alert(FCKLang.DlnLnkMsgNoUrl);
 
297
        return false;
 
298
      }
 
299
 
 
300
      sText = sUri;
 
301
      sUri = encodeUrl(sUri);
 
302
      break;
 
303
 
 
304
    case 'url' :
 
305
      sUri = GetE('txtUrl').value;
 
306
 
 
307
      if (sUri.length == 0)
 
308
      {
 
309
        alert(FCKLang.DlnLnkMsgNoUrl);
 
310
        return false;
 
311
      }
 
312
 
 
313
      sUri = GetE('cmbLinkProtocol').value + sUri;
 
314
      sText = sUri;
 
315
      sUri = encodeUrl(sUri);
 
316
      break;
 
317
  }
 
318
 
 
319
  // Modifying an existent link.
 
320
  if (oLink) {
 
321
    oLink.href = sUri;
 
322
    SetAttribute( oLink, '_fcksavedurl', sUri ) ;
 
323
    if (isNameRelatedLink) {
 
324
      oLink.innerHTML = sText;
 
325
    }
 
326
  }
 
327
  else   // Creating a new link.
 
328
  {
 
329
    oLink = oEditor.FCK.CreateLink(sUri)[0];
 
330
    if (! oLink)
 
331
    {
 
332
      oLink = oEditor.FCK.CreateElement('A');
 
333
      oLink.href = sUri;
 
334
      oLink.appendChild(oEditor.FCK.EditorDocument.createTextNode(sText)); 
 
335
    }
 
336
  }
 
337
 
 
338
  if (GetE('cmbLinkType').value == 'interwiki')
 
339
  { 
 
340
    SetAttribute(oLink, 'class', 'badinterwiki'); // Bug on IE.5.5 makes this ineffective! Works on IE6/Moz....
 
341
    SetAttribute(oLink, 'title', GetE('sctInterwiki').value);
 
342
  }
 
343
 
 
344
  return true;
 
345
}
 
346
 
 
347
function SetUrl(url)
 
348
{
 
349
  document.getElementById('txtUrl').value = url;
 
350
  OnUrlChange();
 
351
}