-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.js
148 lines (130 loc) · 5.27 KB
/
plugin.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Text Color Plugin
*/
GENTICS.Aloha.ColorSelector = new GENTICS.Aloha.Plugin('com.gentics.aloha.plugins.ColorSelector');
/**
* Configure the available languages
*/
GENTICS.Aloha.ColorSelector.languages = ['en', 'ru'];
/**
* Configure the available colors
*/
GENTICS.Aloha.ColorSelector.config = {
colors: [
'black', 'dimgray', 'gray', 'darkgray', 'lightgray', 'white',
'red', 'green', 'blue', 'yellowgreen', 'cadetblue', 'coral'
]
};
/**
* Initialize the plugin and set initialize flag on true
*/
GENTICS.Aloha.ColorSelector.init = function () {
var that = this,
style = jQuery('<style>' +
'.GENTICS_dropdown_colorbox {padding: 4px; position: absolute; background: #FFFFFF; z-index: 11001; border: 1px solid #AAAAAA;}'+
'.GENTICS_button_text_color {background: url("/javascripts/aloha/deps/extjs/resources/images/default/editor/tb-sprite.gif") repeat scroll -160px 0 transparent !important}' +
'.GENTICS_button_text_background {background: url("/javascripts/aloha/deps/extjs/resources/images/default/editor/tb-sprite.gif") repeat scroll -176px 0 transparent !important}' +
'</style>');
jQuery.each(GENTICS.Aloha.ColorSelector.config.colors, function(index, value){
style.append('button.GENTICS_button_'+ value +' { background: '+ value +' !important; border: 0; float: left; margin: 3px; width: 16px; height: 16px; border: 1px solid #888;} ');
});
style.appendTo('head');
var layer = new Layer();
var buttons = {};
jQuery(['color', 'background-color']).each(function (index, style) {
buttons[style] = new GENTICS.Aloha.ui.Button({
'iconClass': 'GENTICS_button_text_' + style.split('-')[0],
'size': 'small',
'toggle': true,
'onclick': function (button) {
layer.display(button, style);
}
});
GENTICS.Aloha.FloatingMenu.addButton(
'GENTICS.Aloha.continuoustext',
buttons[style],
that.i18n('floatingmenu.tab.format'),
1
);
});
function Layer () {}
Layer.prototype.display = function (target, style) {
if (this.visible) {
if (this.visible === style) {
return;
} else {
this.hide();
}
}
var that = this;
this.targetButton = buttons[style];
this.targetButton.setPressed(true);
this.target = jQuery(target.el.dom);
var offset = this.target.offset();
this.el = jQuery('<div class="GENTICS_dropdown_colorbox"></div>');
this.el.css('top', offset.top + this.target.height());
this.el.css('left', offset.left);
this.populateColors(style);
this.el.width(132);
this.el.height(44);
this.el.show();
jQuery('body').append(this.el).bind('click', function(e) {
if (that.visible) {
that.hide();
}
});
this.editable = GENTICS.Aloha.activeEditable;
setTimeout(function () {
that.visible = style;
}, 10);
};
Layer.prototype.populateColors = function (style) {
var that = this;
jQuery(GENTICS.Aloha.ColorSelector.config.colors).each(function (index, value) {
var b = jQuery('<button class="GENTICS_button_'+ value +'"></button>');
b.bind('mousedown', function () {
if (that.editable) {
that.editable.obj[0].focus();
}
applyFormat(style, value);
that.hide();
return false;
});
that.el.append(b);
});
};
Layer.prototype.hide = function () {
this.el.remove();
this.visible = false;
jQuery('body').unbind('click');
this.targetButton.setPressed(false);
};
function applyFormat (style, value) {
var markupClassName = 'GENTICS_markup_' + [style.replace('-', '_'), value].join('_');
var markup = jQuery('<span class="' + markupClassName + '" style="' + style + ':' + value + '"></span>');
var rangeObject = GENTICS.Aloha.Selection.rangeObject;
// check whether the markup is found in the range (at the start of the range)
var foundMarkup = rangeObject.findMarkup(function() {
return this.nodeName.toLowerCase() == markup.get(0).nodeName.toLowerCase() && this.className === markupClassName;
}, GENTICS.Aloha.activeEditable.obj);
if (foundMarkup) {
// remove the markup
if (rangeObject.isCollapsed()) {
// when the range is collapsed, we remove exactly the one DOM element
GENTICS.Utils.Dom.removeFromDOM(foundMarkup, rangeObject, true);
} else {
// the range is not collapsed, so we remove the markup from the range
GENTICS.Utils.Dom.removeMarkup(rangeObject, markup, GENTICS.Aloha.activeEditable.obj);
}
} else {
// when the range is collapsed, extend it to a word
if (rangeObject.isCollapsed()) {
GENTICS.Utils.Dom.extendToWord(rangeObject);
}
// add the markup
GENTICS.Utils.Dom.addMarkup(rangeObject, markup);
}
// select the modified range
rangeObject.select();
}
};