forked from cmelbye/jwysiwyg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwysiwyg.cssWrap.js
134 lines (130 loc) · 4.96 KB
/
wysiwyg.cssWrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Controls: Element CSS Wrapper plugin
*
* Depends on jWYSIWYG
*
* By Yotam Bar-On (https://github.com/tudmotu)
*/
(function ($) {
if (undefined === $.wysiwyg) {
throw "wysiwyg.cssWrap.js depends on $.wysiwyg";
}
/* For core enhancements #143
$.wysiwyg.ui.addControl("cssWrap", {
visible : false,
groupIndex: 6,
tooltip: "CSS Wrapper",
exec: function () {
$.wysiwyg.controls.cssWrap.init(this);
}
}
*/
if (!$.wysiwyg.controls) {
$.wysiwyg.controls = {};
}
/*
* Wysiwyg namespace: public properties and methods
*/
$.wysiwyg.controls.cssWrap = {
init: function (Wysiwyg) {
var self = this, formWrapHtml, key, translation,
dialogReplacements = {
legend : "Wrap Element",
wrapperType : "Wrapper Type",
ID : "ID",
"class" : "Class",
wrap : "Wrap",
unwrap: "Unwrap",
cancel : "Cancel"
};
formWrapHtml = '<form class="wysiwyg"><fieldset><legend>{legend}</legend>' +
'<div class="wysiwyg-dialogRow"><label>{wrapperType}: <select name="type"><option value="span">Span</option><option value="div">Div</option></select></label></div>' +
'<div class="wysiwyg-dialogRow"><label>{ID}: <input name="id" type="text" /></label></div>' +
'<div class="wysiwyg-dialogRow"><label>{class}: <input name="class" type="text" /></label></div>' +
'<div class="wysiwyg-dialogRow"><input type="button" class="button cssWrap-unwrap" style="display:none;" value="{unwrap}"/></label>' +
'<input type="submit" class="button cssWrap-submit" value="{wrap}"/></label>' +
'<input type="reset" class="button cssWrap-cancel" value="{cancel}"/></div></fieldset></form>';
for (key in dialogReplacements) {
if ($.wysiwyg.i18n) {
translation = $.wysiwyg.i18n.t(dialogReplacements[key]);
if (translation === dialogReplacements[key]) { // if not translated search in dialogs
translation = $.wysiwyg.i18n.t(dialogReplacements[key], "dialogs");
}
dialogReplacements[key] = translation;
}
formWrapHtml = formWrapHtml.replace("{" + key + "}", dialogReplacements[key]);
}
if (!$(".wysiwyg-dialog-wrapper").length) {
$(formWrapHtml).appendTo("body");
$("form.wysiwyg").dialog({
modal: true,
open: function (ev, ui) {
var $this = $(this), range = Wysiwyg.getInternalRange(), common, $nodeName;
// We make sure that there is some selection:
if (range) {
if ($.browser.msie) {
Wysiwyg.ui.focus();
}
common = $(range.commonAncestorContainer);
} else {
alert("You must select some elements before you can wrap them.");
$this.dialog("close");
return 0;
}
$nodeName = range.commonAncestorContainer.nodeName.toLowerCase();
// If the selection is already a .wysiwygCssWrapper, then we want to change it and not double-wrap it.
if (common.parent(".wysiwygCssWrapper").length) {
alert(common.parent(".wysiwygCssWrapper").get(0).nodeName.toLowerCase());
$this.find("select[name=type]").val(common.parent(".wysiwygCssWrapper").get(0).nodeName.toLowerCase());
$this.find("select[name=type]").attr("disabled", "disabled");
$this.find("input[name=id]").val(common.parent(".wysiwygCssWrapper").attr("id"));
$this.find("input[name=class]").val(common.parent(".wysiwygCssWrapper").attr("class").replace('wysiwygCssWrapper ', ''));
// Add the "unwrap" button:
$("form.wysiwyg").find(".cssWrap-unwrap").show();
$("form.wysiwyg").find(".cssWrap-unwrap").click(function (e) {
e.preventDefault();
if ($nodeName !== "body") {
common.unwrap();
}
$this.dialog("close");
return 1;
});
}
// Submit button.
$("form.wysiwyg").find(".cssWrap-submit").click(function (e) {
e.preventDefault();
var $wrapper = $("form.wysiwyg").find("select[name=type]").val(),
$id = $("form.wysiwyg").find("input[name=id]").val(),
$class = $("form.wysiwyg").find("input[name=class]").val();
if ($nodeName !== "body") {
// If the selection is already a .wysiwygCssWrapper, then we want to change it and not double-wrap it.
if (common.parent(".wysiwygCssWrapper").length) {
common.parent(".wysiwygCssWrapper").attr("id", $class);
common.parent(".wysiwygCssWrapper").attr("class", $class);
} else {
common.wrap("<" + $wrapper + " id=\"" + $id + "\" class=\"" + "wysiwygCssWrapper " + $class + "\"/>");
}
} else {
// Currently no implemntation for if $nodeName == 'body'.
}
$this.dialog("close");
});
// Cancel button.
$("form.wysiwyg").find(".cssWrap-cancel").click(function (e) {
e.preventDefault();
$this.dialog("close");
return 1;
});
},
close: function () {
$(this).dialog("destroy");
$(this).remove();
}
});
Wysiwyg.saveContent();
}
$(Wysiwyg.editorDoc).trigger("editorRefresh.wysiwyg");
return 1;
}
};
})(jQuery);