jwysiwyg_rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.document +5 -0
  2. data/Gemfile +14 -0
  3. data/Gemfile.lock +98 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.rdoc +19 -0
  6. data/Rakefile +53 -0
  7. data/VERSION +1 -0
  8. data/app/assets/images/ajax-loader.gif +0 -0
  9. data/app/assets/images/jquery.wysiwyg.bg.png +0 -0
  10. data/app/assets/images/jquery.wysiwyg.gif +0 -0
  11. data/app/assets/images/jquery.wysiwyg.jpg +0 -0
  12. data/app/assets/images/jquery.wysiwyg.no-alpha.gif +0 -0
  13. data/app/assets/images/plugins/fileManager/icon.png +0 -0
  14. data/app/assets/images/plugins/fileManager/images/application.png +0 -0
  15. data/app/assets/images/plugins/fileManager/images/code.png +0 -0
  16. data/app/assets/images/plugins/fileManager/images/css.png +0 -0
  17. data/app/assets/images/plugins/fileManager/images/db.png +0 -0
  18. data/app/assets/images/plugins/fileManager/images/directory.png +0 -0
  19. data/app/assets/images/plugins/fileManager/images/doc.png +0 -0
  20. data/app/assets/images/plugins/fileManager/images/file.png +0 -0
  21. data/app/assets/images/plugins/fileManager/images/film.png +0 -0
  22. data/app/assets/images/plugins/fileManager/images/flash.png +0 -0
  23. data/app/assets/images/plugins/fileManager/images/folder_open.png +0 -0
  24. data/app/assets/images/plugins/fileManager/images/html.png +0 -0
  25. data/app/assets/images/plugins/fileManager/images/java.png +0 -0
  26. data/app/assets/images/plugins/fileManager/images/linux.png +0 -0
  27. data/app/assets/images/plugins/fileManager/images/mkdir.png +0 -0
  28. data/app/assets/images/plugins/fileManager/images/music.png +0 -0
  29. data/app/assets/images/plugins/fileManager/images/pdf.png +0 -0
  30. data/app/assets/images/plugins/fileManager/images/php.png +0 -0
  31. data/app/assets/images/plugins/fileManager/images/picture.png +0 -0
  32. data/app/assets/images/plugins/fileManager/images/ppt.png +0 -0
  33. data/app/assets/images/plugins/fileManager/images/prev-directory.png +0 -0
  34. data/app/assets/images/plugins/fileManager/images/psd.png +0 -0
  35. data/app/assets/images/plugins/fileManager/images/remove.png +0 -0
  36. data/app/assets/images/plugins/fileManager/images/rename.png +0 -0
  37. data/app/assets/images/plugins/fileManager/images/ruby.png +0 -0
  38. data/app/assets/images/plugins/fileManager/images/script.png +0 -0
  39. data/app/assets/images/plugins/fileManager/images/txt.png +0 -0
  40. data/app/assets/images/plugins/fileManager/images/upload.png +0 -0
  41. data/app/assets/images/plugins/fileManager/images/xls.png +0 -0
  42. data/app/assets/images/plugins/fileManager/images/zip.png +0 -0
  43. data/app/assets/javascripts/controls/wysiwyg.colorpicker.js +250 -0
  44. data/app/assets/javascripts/controls/wysiwyg.cssWrap.js +134 -0
  45. data/app/assets/javascripts/controls/wysiwyg.image.js +285 -0
  46. data/app/assets/javascripts/controls/wysiwyg.link.js +249 -0
  47. data/app/assets/javascripts/controls/wysiwyg.table.js +129 -0
  48. data/app/assets/javascripts/i18n/lang.cs.js +116 -0
  49. data/app/assets/javascripts/i18n/lang.de.js +94 -0
  50. data/app/assets/javascripts/i18n/lang.en.js +117 -0
  51. data/app/assets/javascripts/i18n/lang.es.js +94 -0
  52. data/app/assets/javascripts/i18n/lang.fr.js +94 -0
  53. data/app/assets/javascripts/i18n/lang.he.js +94 -0
  54. data/app/assets/javascripts/i18n/lang.hr.js +98 -0
  55. data/app/assets/javascripts/i18n/lang.it.js +94 -0
  56. data/app/assets/javascripts/i18n/lang.ja.js +95 -0
  57. data/app/assets/javascripts/i18n/lang.nl.js +94 -0
  58. data/app/assets/javascripts/i18n/lang.pl.js +94 -0
  59. data/app/assets/javascripts/i18n/lang.pt_br.js +94 -0
  60. data/app/assets/javascripts/i18n/lang.ru.js +117 -0
  61. data/app/assets/javascripts/i18n/lang.se.js +94 -0
  62. data/app/assets/javascripts/i18n/lang.sl.js +94 -0
  63. data/app/assets/javascripts/i18n/lang.zh-cn.js +93 -0
  64. data/app/assets/javascripts/plugins/wysiwyg.autoload.js +61 -0
  65. data/app/assets/javascripts/plugins/wysiwyg.fileManager.js +572 -0
  66. data/app/assets/javascripts/plugins/wysiwyg.fullscreen.js +141 -0
  67. data/app/assets/javascripts/plugins/wysiwyg.i18n.js +99 -0
  68. data/app/assets/javascripts/plugins/wysiwyg.rmFormat.js +357 -0
  69. data/app/assets/stylesheets/jquery.wysiwyg.css +95 -0
  70. data/app/assets/stylesheets/jquery.wysiwyg.modal.css +62 -0
  71. data/app/assets/stylesheets/jquery.wysiwyg.old-school.css +63 -0
  72. data/app/assets/stylesheets/jwysiwyg.css +1 -0
  73. data/app/assets/stylesheets/plugins/fileManager/wysiwyg.fileManager.css +113 -0
  74. data/jwysiwyg_rails.gemspec +129 -0
  75. data/lib/engine.rb +8 -0
  76. data/lib/jwysiwyg_rails.rb +14 -0
  77. data/test/helper.rb +18 -0
  78. data/test/test_jwysiwyg_rails.rb +7 -0
  79. metadata +219 -0
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Controls: Element CSS Wrapper plugin
3
+ *
4
+ * Depends on jWYSIWYG
5
+ *
6
+ * By Yotam Bar-On (https://github.com/tudmotu)
7
+ */
8
+ (function ($) {
9
+ if (undefined === $.wysiwyg) {
10
+ throw "wysiwyg.cssWrap.js depends on $.wysiwyg";
11
+ }
12
+ /* For core enhancements #143
13
+ $.wysiwyg.ui.addControl("cssWrap", {
14
+ visible : false,
15
+ groupIndex: 6,
16
+ tooltip: "CSS Wrapper",
17
+ exec: function () {
18
+ $.wysiwyg.controls.cssWrap.init(this);
19
+ }
20
+ }
21
+ */
22
+ if (!$.wysiwyg.controls) {
23
+ $.wysiwyg.controls = {};
24
+ }
25
+
26
+ /*
27
+ * Wysiwyg namespace: public properties and methods
28
+ */
29
+ $.wysiwyg.controls.cssWrap = {
30
+ init: function (Wysiwyg) {
31
+ var self = this, formWrapHtml, key, translation,
32
+ dialogReplacements = {
33
+ legend : "Wrap Element",
34
+ wrapperType : "Wrapper Type",
35
+ ID : "ID",
36
+ "class" : "Class",
37
+ wrap : "Wrap",
38
+ unwrap: "Unwrap",
39
+ cancel : "Cancel"
40
+ };
41
+
42
+ formWrapHtml = '<form class="wysiwyg"><fieldset><legend>{legend}</legend>' +
43
+ '<div class="wysiwyg-dialogRow"><label>{wrapperType}: &nbsp;<select name="type"><option value="span">Span</option><option value="div">Div</option></select></label></div>' +
44
+ '<div class="wysiwyg-dialogRow"><label>{ID}: &nbsp;<input name="id" type="text" /></label></div>' +
45
+ '<div class="wysiwyg-dialogRow"><label>{class}: &nbsp;<input name="class" type="text" /></label></div>' +
46
+ '<div class="wysiwyg-dialogRow"><input type="button" class="button cssWrap-unwrap" style="display:none;" value="{unwrap}"/></label>' +
47
+ '<input type="submit" class="button cssWrap-submit" value="{wrap}"/></label>' +
48
+ '<input type="reset" class="button cssWrap-cancel" value="{cancel}"/></div></fieldset></form>';
49
+
50
+ for (key in dialogReplacements) {
51
+ if ($.wysiwyg.i18n) {
52
+ translation = $.wysiwyg.i18n.t(dialogReplacements[key]);
53
+ if (translation === dialogReplacements[key]) { // if not translated search in dialogs
54
+ translation = $.wysiwyg.i18n.t(dialogReplacements[key], "dialogs");
55
+ }
56
+ dialogReplacements[key] = translation;
57
+ }
58
+ formWrapHtml = formWrapHtml.replace("{" + key + "}", dialogReplacements[key]);
59
+ }
60
+ if (!$(".wysiwyg-dialog-wrapper").length) {
61
+ $(formWrapHtml).appendTo("body");
62
+ $("form.wysiwyg").dialog({
63
+ modal: true,
64
+ open: function (ev, ui) {
65
+ var $this = $(this), range = Wysiwyg.getInternalRange(), common, $nodeName;
66
+ // We make sure that there is some selection:
67
+ if (range) {
68
+ if ($.browser.msie) {
69
+ Wysiwyg.ui.focus();
70
+ }
71
+ common = $(range.commonAncestorContainer);
72
+ } else {
73
+ alert("You must select some elements before you can wrap them.");
74
+ $this.dialog("close");
75
+ return 0;
76
+ }
77
+ $nodeName = range.commonAncestorContainer.nodeName.toLowerCase();
78
+ // If the selection is already a .wysiwygCssWrapper, then we want to change it and not double-wrap it.
79
+ if (common.parent(".wysiwygCssWrapper").length) {
80
+ alert(common.parent(".wysiwygCssWrapper").get(0).nodeName.toLowerCase());
81
+ $this.find("select[name=type]").val(common.parent(".wysiwygCssWrapper").get(0).nodeName.toLowerCase());
82
+ $this.find("select[name=type]").attr("disabled", "disabled");
83
+ $this.find("input[name=id]").val(common.parent(".wysiwygCssWrapper").attr("id"));
84
+ $this.find("input[name=class]").val(common.parent(".wysiwygCssWrapper").attr("class").replace('wysiwygCssWrapper ', ''));
85
+ // Add the "unwrap" button:
86
+ $("form.wysiwyg").find(".cssWrap-unwrap").show();
87
+ $("form.wysiwyg").find(".cssWrap-unwrap").click(function (e) {
88
+ e.preventDefault();
89
+ if ($nodeName !== "body") {
90
+ common.unwrap();
91
+ }
92
+ $this.dialog("close");
93
+ return 1;
94
+ });
95
+ }
96
+ // Submit button.
97
+ $("form.wysiwyg").find(".cssWrap-submit").click(function (e) {
98
+ e.preventDefault();
99
+ var $wrapper = $("form.wysiwyg").find("select[name=type]").val(),
100
+ $id = $("form.wysiwyg").find("input[name=id]").val(),
101
+ $class = $("form.wysiwyg").find("input[name=class]").val();
102
+
103
+ if ($nodeName !== "body") {
104
+ // If the selection is already a .wysiwygCssWrapper, then we want to change it and not double-wrap it.
105
+ if (common.parent(".wysiwygCssWrapper").length) {
106
+ common.parent(".wysiwygCssWrapper").attr("id", $class);
107
+ common.parent(".wysiwygCssWrapper").attr("class", $class);
108
+ } else {
109
+ common.wrap("<" + $wrapper + " id=\"" + $id + "\" class=\"" + "wysiwygCssWrapper " + $class + "\"/>");
110
+ }
111
+ } else {
112
+ // Currently no implemntation for if $nodeName == 'body'.
113
+ }
114
+ $this.dialog("close");
115
+ });
116
+ // Cancel button.
117
+ $("form.wysiwyg").find(".cssWrap-cancel").click(function (e) {
118
+ e.preventDefault();
119
+ $this.dialog("close");
120
+ return 1;
121
+ });
122
+ },
123
+ close: function () {
124
+ $(this).dialog("destroy");
125
+ $(this).remove();
126
+ }
127
+ });
128
+ Wysiwyg.saveContent();
129
+ }
130
+ $(Wysiwyg.editorDoc).trigger("editorRefresh.wysiwyg");
131
+ return 1;
132
+ }
133
+ };
134
+ })(jQuery);
@@ -0,0 +1,285 @@
1
+ /**
2
+ * Controls: Image plugin
3
+ *
4
+ * Depends on jWYSIWYG
5
+ */
6
+ (function ($) {
7
+ "use strict";
8
+
9
+ if (undefined === $.wysiwyg) {
10
+ throw "wysiwyg.image.js depends on $.wysiwyg";
11
+ }
12
+
13
+ if (!$.wysiwyg.controls) {
14
+ $.wysiwyg.controls = {};
15
+ }
16
+
17
+ /*
18
+ * Wysiwyg namespace: public properties and methods
19
+ */
20
+ $.wysiwyg.controls.image = {
21
+ groupIndex: 6,
22
+ visible: true,
23
+ exec: function () {
24
+ $.wysiwyg.controls.image.init(this);
25
+ },
26
+ tags: ["img"],
27
+ tooltip: "Insert image",
28
+ init: function (Wysiwyg) {
29
+ var self = this, elements, adialog, dialog, formImageHtml, regexp, dialogReplacements, key, translation,
30
+ img = {
31
+ alt: "",
32
+ self: Wysiwyg.dom ? Wysiwyg.dom.getElement("img") : null, // link to element node
33
+ src: "http://",
34
+ title: ""
35
+ };
36
+
37
+ dialogReplacements = {
38
+ legend : "Insert Image",
39
+ preview : "Preview",
40
+ url : "URL",
41
+ title : "Title",
42
+ description : "Description",
43
+ width : "Width",
44
+ height : "Height",
45
+ original : "Original W x H",
46
+ "float" : "Float",
47
+ floatNone : "None",
48
+ floatLeft : "Left",
49
+ floatRight : "Right",
50
+ submit : "Insert Image",
51
+ reset : "Cancel",
52
+ fileManagerIcon : "Select file from server"
53
+ };
54
+
55
+ formImageHtml = '<form class="wysiwyg" id="wysiwyg-addImage"><fieldset>' +
56
+ '<div class="form-row"><span class="form-row-key">{preview}:</span><div class="form-row-value"><img src="" alt="{preview}" style="margin: 2px; padding:5px; max-width: 100%; overflow:hidden; max-height: 100px; border: 1px solid rgb(192, 192, 192);"/></div></div>' +
57
+ '<div class="form-row"><label for="name">{url}:</label><div class="form-row-value"><input type="text" name="src" value=""/>';
58
+
59
+ if ($.wysiwyg.fileManager && $.wysiwyg.fileManager.ready) {
60
+ // Add the File Manager icon:
61
+ formImageHtml += '<div class="wysiwyg-fileManager" title="{fileManagerIcon}"/>';
62
+ }
63
+
64
+ formImageHtml += '</div></div>' +
65
+ '<div class="form-row"><label for="name">{title}:</label><div class="form-row-value"><input type="text" name="imgtitle" value=""/></div></div>' +
66
+ '<div class="form-row"><label for="name">{description}:</label><div class="form-row-value"><input type="text" name="description" value=""/></div></div>' +
67
+ '<div class="form-row"><label for="name">{width} x {height}:</label><div class="form-row-value"><input type="text" name="width" value="" class="width-small"/> x <input type="text" name="height" value="" class="width-small"/></div></div>' +
68
+ '<div class="form-row"><label for="name">{original}:</label><div class="form-row-value"><input type="text" name="naturalWidth" value="" class="width-small" disabled="disabled"/> x ' +
69
+ '<input type="text" name="naturalHeight" value="" class="width-small" disabled="disabled"/></div></div>' +
70
+ '<div class="form-row"><label for="name">{float}:</label><div class="form-row-value"><select name="float">' +
71
+ '<option value="">{floatNone}</option>' +
72
+ '<option value="left">{floatLeft}</option>' +
73
+ '<option value="right">{floatRight}</option></select></div></div>' +
74
+ '<div class="form-row form-row-last"><label for="name"></label><div class="form-row-value"><input type="submit" class="button" value="{submit}"/> ' +
75
+ '<input type="reset" value="{reset}"/></div></div></fieldset></form>';
76
+
77
+ for (key in dialogReplacements) {
78
+ if ($.wysiwyg.i18n) {
79
+ translation = $.wysiwyg.i18n.t(dialogReplacements[key], "dialogs.image");
80
+
81
+ if (translation === dialogReplacements[key]) { // if not translated search in dialogs
82
+ translation = $.wysiwyg.i18n.t(dialogReplacements[key], "dialogs");
83
+ }
84
+
85
+ dialogReplacements[key] = translation;
86
+ }
87
+
88
+ regexp = new RegExp("{" + key + "}", "g");
89
+ formImageHtml = formImageHtml.replace(regexp, dialogReplacements[key]);
90
+ }
91
+
92
+ if (img.self) {
93
+ img.src = img.self.src ? img.self.src : "";
94
+ img.alt = img.self.alt ? img.self.alt : "";
95
+ img.title = img.self.title ? img.self.title : "";
96
+ img.width = img.self.width ? img.self.width : "";
97
+ img.height = img.self.height ? img.self.height : "";
98
+ img.styleFloat = $(img.self).css("float");
99
+ }
100
+
101
+ adialog = new $.wysiwyg.dialog(Wysiwyg, {
102
+ "title" : dialogReplacements.legend,
103
+ "content" : formImageHtml
104
+ });
105
+
106
+ $(adialog).bind("afterOpen", function (e, dialog) {
107
+ dialog.find("form#wysiwyg-addImage").submit(function (e) {
108
+ e.preventDefault();
109
+ self.processInsert(dialog.container, Wysiwyg, img);
110
+
111
+ adialog.close();
112
+ return false;
113
+ });
114
+
115
+ // File Manager (select file):
116
+ if ($.wysiwyg.fileManager) {
117
+ $("div.wysiwyg-fileManager").bind("click", function () {
118
+ $.wysiwyg.fileManager.init(function (selected) {
119
+ dialog.find("input[name=src]").val(selected);
120
+ dialog.find("input[name=src]").trigger("change");
121
+ });
122
+ });
123
+ }
124
+
125
+ $("input:reset", dialog).click(function (e) {
126
+ adialog.close();
127
+
128
+ return false;
129
+ });
130
+
131
+ $("fieldset", dialog).click(function (e) {
132
+ e.stopPropagation();
133
+ });
134
+
135
+ self.makeForm(dialog, img);
136
+ });
137
+
138
+ adialog.open();
139
+
140
+ $(Wysiwyg.editorDoc).trigger("editorRefresh.wysiwyg");
141
+ },
142
+
143
+ processInsert: function (context, Wysiwyg, img) {
144
+ var image,
145
+ url = $('input[name="src"]', context).val(),
146
+ title = $('input[name="imgtitle"]', context).val(),
147
+ description = $('input[name="description"]', context).val(),
148
+ width = $('input[name="width"]', context).val(),
149
+ height = $('input[name="height"]', context).val(),
150
+ styleFloat = $('select[name="float"]', context).val(),
151
+ styles = [],
152
+ style = "",
153
+ found,
154
+ baseUrl;
155
+
156
+ if (Wysiwyg.options.controlImage && Wysiwyg.options.controlImage.forceRelativeUrls) {
157
+ baseUrl = window.location.protocol + "//" + window.location.hostname
158
+ + (window.location.port ? ":" + window.location.port : "");
159
+ if (0 === url.indexOf(baseUrl)) {
160
+ url = url.substr(baseUrl.length);
161
+ }
162
+ }
163
+
164
+ if (img.self) {
165
+ // to preserve all img attributes
166
+ $(img.self).attr("src", url)
167
+ .attr("title", title)
168
+ .attr("alt", description)
169
+ .css("float", styleFloat);
170
+
171
+ if (width.toString().match(/^[0-9]+(px|%)?$/)) {
172
+ $(img.self).css("width", width);
173
+ } else {
174
+ $(img.self).css("width", "");
175
+ }
176
+
177
+ if (height.toString().match(/^[0-9]+(px|%)?$/)) {
178
+ $(img.self).css("height", height);
179
+ } else {
180
+ $(img.self).css("height", "");
181
+ }
182
+
183
+ Wysiwyg.saveContent();
184
+ } else {
185
+ found = width.toString().match(/^[0-9]+(px|%)?$/);
186
+ if (found) {
187
+ if (found[1]) {
188
+ styles.push("width: " + width + ";");
189
+ } else {
190
+ styles.push("width: " + width + "px;");
191
+ }
192
+ }
193
+
194
+ found = height.toString().match(/^[0-9]+(px|%)?$/);
195
+ if (found) {
196
+ if (found[1]) {
197
+ styles.push("height: " + height + ";");
198
+ } else {
199
+ styles.push("height: " + height + "px;");
200
+ }
201
+ }
202
+
203
+ if (styleFloat.length > 0) {
204
+ styles.push("float: " + styleFloat + ";");
205
+ }
206
+
207
+ if (styles.length > 0) {
208
+ style = ' style="' + styles.join(" ") + '"';
209
+ }
210
+
211
+ image = "<img src='" + url + "' title='" + title + "' alt='" + description + "'" + style + "/>";
212
+ Wysiwyg.insertHtml(image);
213
+ }
214
+ },
215
+
216
+ makeForm: function (form, img) {
217
+ form.find("input[name=src]").val(img.src);
218
+ form.find("input[name=imgtitle]").val(img.title);
219
+ form.find("input[name=description]").val(img.alt);
220
+ form.find('input[name="width"]').val(img.width);
221
+ form.find('input[name="height"]').val(img.height);
222
+ form.find('select[name="float"]').val(img.styleFloat);
223
+ form.find('img').attr("src", img.src);
224
+
225
+ form.find('img').bind("load", function () {
226
+ if (form.find('img').get(0).naturalWidth) {
227
+ form.find('input[name="naturalWidth"]').val(form.find('img').get(0).naturalWidth);
228
+ form.find('input[name="naturalHeight"]').val(form.find('img').get(0).naturalHeight);
229
+ } else if (form.find('img').attr("naturalWidth")) {
230
+ form.find('input[name="naturalWidth"]').val(form.find('img').attr("naturalWidth"));
231
+ form.find('input[name="naturalHeight"]').val(form.find('img').attr("naturalHeight"));
232
+ }
233
+ });
234
+
235
+ form.find("input[name=src]").bind("change", function () {
236
+ form.find('img').attr("src", this.value);
237
+ });
238
+
239
+ return form;
240
+ }
241
+ };
242
+
243
+ $.wysiwyg.insertImage = function (object, url, attributes) {
244
+ return object.each(function () {
245
+ var Wysiwyg = $(this).data("wysiwyg"),
246
+ image,
247
+ attribute;
248
+
249
+ if (!Wysiwyg) {
250
+ return this;
251
+ }
252
+
253
+ if (!url || url.length === 0) {
254
+ return this;
255
+ }
256
+
257
+ if ($.browser.msie) {
258
+ Wysiwyg.ui.focus();
259
+ }
260
+
261
+ if (attributes) {
262
+ Wysiwyg.editorDoc.execCommand("insertImage", false, "#jwysiwyg#");
263
+ image = Wysiwyg.getElementByAttributeValue("img", "src", "#jwysiwyg#");
264
+
265
+ if (image) {
266
+ image.src = url;
267
+
268
+ for (attribute in attributes) {
269
+ if (attributes.hasOwnProperty(attribute)) {
270
+ image.setAttribute(attribute, attributes[attribute]);
271
+ }
272
+ }
273
+ }
274
+ } else {
275
+ Wysiwyg.editorDoc.execCommand("insertImage", false, url);
276
+ }
277
+
278
+ Wysiwyg.saveContent();
279
+
280
+ $(Wysiwyg.editorDoc).trigger("editorRefresh.wysiwyg");
281
+
282
+ return this;
283
+ });
284
+ };
285
+ })(jQuery);
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Controls: Link plugin
3
+ *
4
+ * Depends on jWYSIWYG
5
+ *
6
+ * By: Esteban Beltran (academo) <sergies@gmail.com>
7
+ */
8
+ (function ($) {
9
+ "use strict";
10
+
11
+ if (undefined === $.wysiwyg) {
12
+ throw "wysiwyg.link.js depends on $.wysiwyg";
13
+ }
14
+
15
+ if (!$.wysiwyg.controls) {
16
+ $.wysiwyg.controls = {};
17
+ }
18
+
19
+ /*
20
+ * Wysiwyg namespace: public properties and methods
21
+ */
22
+ $.wysiwyg.controls.link = {
23
+ init: function (Wysiwyg) {
24
+ var self = this, elements, dialog, url, a, selection,
25
+ formLinkHtml, dialogReplacements, key, translation, regexp,
26
+ baseUrl, img;
27
+
28
+ dialogReplacements = {
29
+ legend: "Insert Link",
30
+ url : "Link URL",
31
+ title : "Link Title",
32
+ target: "Link Target",
33
+ submit: "Insert Link",
34
+ reset: "Cancel"
35
+ };
36
+
37
+ formLinkHtml = '<form class="wysiwyg"><fieldset><legend>{legend}</legend>' +
38
+ '<label>{url}: <input type="text" name="linkhref" value=""/></label>' +
39
+ '<label>{title}: <input type="text" name="linktitle" value=""/></label>' +
40
+ '<label>{target}: <input type="text" name="linktarget" value=""/></label>' +
41
+ '<input type="submit" class="button" value="{submit}"/> ' +
42
+ '<input type="reset" value="{reset}"/></fieldset></form>';
43
+
44
+ for (key in dialogReplacements) {
45
+ if ($.wysiwyg.i18n) {
46
+ translation = $.wysiwyg.i18n.t(dialogReplacements[key], "dialogs.link");
47
+
48
+ if (translation === dialogReplacements[key]) { // if not translated search in dialogs
49
+ translation = $.wysiwyg.i18n.t(dialogReplacements[key], "dialogs");
50
+ }
51
+
52
+ dialogReplacements[key] = translation;
53
+ }
54
+
55
+ regexp = new RegExp("{" + key + "}", "g");
56
+ formLinkHtml = formLinkHtml.replace(regexp, dialogReplacements[key]);
57
+ }
58
+
59
+ a = {
60
+ self: Wysiwyg.dom.getElement("a"), // link to element node
61
+ href: "http://",
62
+ title: "",
63
+ target: ""
64
+ };
65
+
66
+ if (a.self) {
67
+ a.href = a.self.href ? a.self.href : a.href;
68
+ a.title = a.self.title ? a.self.title : "";
69
+ a.target = a.self.target ? a.self.target : "";
70
+ }
71
+
72
+ if ($.fn.dialog) {
73
+ elements = $(formLinkHtml);
74
+ elements.find("input[name=linkhref]").val(a.href);
75
+ elements.find("input[name=linktitle]").val(a.title);
76
+ elements.find("input[name=linktarget]").val(a.target);
77
+
78
+ if ($.browser.msie) {
79
+ try {
80
+ dialog = elements.appendTo(Wysiwyg.editorDoc.body);
81
+ } catch (err) {
82
+ dialog = elements.appendTo("body");
83
+ }
84
+ } else {
85
+ dialog = elements.appendTo("body");
86
+ }
87
+
88
+ dialog.dialog({
89
+ modal: true,
90
+ open: function (ev, ui) {
91
+ $("input:submit", dialog).click(function (e) {
92
+ e.preventDefault();
93
+
94
+ var url = $('input[name="linkhref"]', dialog).val(),
95
+ title = $('input[name="linktitle"]', dialog).val(),
96
+ target = $('input[name="linktarget"]', dialog).val(),
97
+ baseUrl,
98
+ img;
99
+
100
+ if (Wysiwyg.options.controlLink.forceRelativeUrls) {
101
+ baseUrl = window.location.protocol + "//" + window.location.hostname;
102
+ if (0 === url.indexOf(baseUrl)) {
103
+ url = url.substr(baseUrl.length);
104
+ }
105
+ }
106
+
107
+ if (a.self) {
108
+ if ("string" === typeof (url)) {
109
+ if (url.length > 0) {
110
+ // to preserve all link attributes
111
+ $(a.self).attr("href", url).attr("title", title).attr("target", target);
112
+ } else {
113
+ $(a.self).replaceWith(a.self.innerHTML);
114
+ }
115
+ }
116
+ } else {
117
+ if ($.browser.msie) {
118
+ Wysiwyg.ui.returnRange();
119
+ }
120
+
121
+ //Do new link element
122
+ selection = Wysiwyg.getRangeText();
123
+ img = Wysiwyg.dom.getElement("img");
124
+
125
+ if ((selection && selection.length > 0) || img) {
126
+ if ($.browser.msie) {
127
+ Wysiwyg.ui.focus();
128
+ }
129
+
130
+ if ("string" === typeof (url)) {
131
+ if (url.length > 0) {
132
+ Wysiwyg.editorDoc.execCommand("createLink", false, url);
133
+ } else {
134
+ Wysiwyg.editorDoc.execCommand("unlink", false, null);
135
+ }
136
+ }
137
+
138
+ a.self = Wysiwyg.dom.getElement("a");
139
+
140
+ $(a.self).attr("href", url).attr("title", title);
141
+
142
+ /**
143
+ * @url https://github.com/akzhan/jwysiwyg/issues/16
144
+ */
145
+ $(a.self).attr("target", target);
146
+ } else if (Wysiwyg.options.messages.nonSelection) {
147
+ window.alert(Wysiwyg.options.messages.nonSelection);
148
+ }
149
+ }
150
+
151
+ Wysiwyg.saveContent();
152
+
153
+ $(dialog).dialog("close");
154
+ });
155
+ $("input:reset", dialog).click(function (e) {
156
+ e.preventDefault();
157
+ $(dialog).dialog("close");
158
+ });
159
+ },
160
+ close: function (ev, ui) {
161
+ dialog.dialog("destroy");
162
+ dialog.remove();
163
+ }
164
+ });
165
+ } else {
166
+ if (a.self) {
167
+ url = window.prompt("URL", a.href);
168
+
169
+ if (Wysiwyg.options.controlLink.forceRelativeUrls) {
170
+ baseUrl = window.location.protocol + "//" + window.location.hostname;
171
+ if (0 === url.indexOf(baseUrl)) {
172
+ url = url.substr(baseUrl.length);
173
+ }
174
+ }
175
+
176
+ if ("string" === typeof (url)) {
177
+ if (url.length > 0) {
178
+ $(a.self).attr("href", url);
179
+ } else {
180
+ $(a.self).replaceWith(a.self.innerHTML);
181
+ }
182
+ }
183
+ } else {
184
+ //Do new link element
185
+ selection = Wysiwyg.getRangeText();
186
+ img = Wysiwyg.dom.getElement("img");
187
+
188
+ if ((selection && selection.length > 0) || img) {
189
+ if ($.browser.msie) {
190
+ Wysiwyg.ui.focus();
191
+ Wysiwyg.editorDoc.execCommand("createLink", true, null);
192
+ } else {
193
+ url = window.prompt(dialogReplacements.url, a.href);
194
+
195
+ if (Wysiwyg.options.controlLink.forceRelativeUrls) {
196
+ baseUrl = window.location.protocol + "//" + window.location.hostname;
197
+ if (0 === url.indexOf(baseUrl)) {
198
+ url = url.substr(baseUrl.length);
199
+ }
200
+ }
201
+
202
+ if ("string" === typeof (url)) {
203
+ if (url.length > 0) {
204
+ Wysiwyg.editorDoc.execCommand("createLink", false, url);
205
+ } else {
206
+ Wysiwyg.editorDoc.execCommand("unlink", false, null);
207
+ }
208
+ }
209
+ }
210
+ } else if (Wysiwyg.options.messages.nonSelection) {
211
+ window.alert(Wysiwyg.options.messages.nonSelection);
212
+ }
213
+ }
214
+
215
+ Wysiwyg.saveContent();
216
+ }
217
+
218
+ $(Wysiwyg.editorDoc).trigger("editorRefresh.wysiwyg");
219
+ }
220
+ };
221
+
222
+ $.wysiwyg.createLink = function (object, url) {
223
+ return object.each(function () {
224
+ var oWysiwyg = $(this).data("wysiwyg"),
225
+ selection;
226
+
227
+ if (!oWysiwyg) {
228
+ return this;
229
+ }
230
+
231
+ if (!url || url.length === 0) {
232
+ return this;
233
+ }
234
+
235
+ selection = oWysiwyg.getRangeText();
236
+
237
+ if (selection && selection.length > 0) {
238
+ if ($.browser.msie) {
239
+ oWysiwyg.ui.focus();
240
+ }
241
+ oWysiwyg.editorDoc.execCommand("unlink", false, null);
242
+ oWysiwyg.editorDoc.execCommand("createLink", false, url);
243
+ } else if (oWysiwyg.options.messages.nonSelection) {
244
+ window.alert(oWysiwyg.options.messages.nonSelection);
245
+ }
246
+ return this;
247
+ });
248
+ };
249
+ })(jQuery);