-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.custom-highlight.js
209 lines (161 loc) · 6.88 KB
/
jquery.custom-highlight.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// the semi-colon before function invocation is a safety net against concatenated
// scripts and/or other plugins which may not be closed properly.
;(function ( $, window, document, undefined ) {
"use strict";
// undefined is used here as the undefined global variable in ECMAScript 3 is
// mutable (ie. it can be changed by someone else). undefined isn't really being
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined
// can no longer be modified.
// window and document are passed through as local variable rather than global
// as this (slightly) quickens the resolution process and can be more efficiently
// minified (especially when both are regularly referenced in your plugin).
// Create the defaults once
var pluginName = "customHighlight",
defaults = {
actions: [{"test_action":"Test action"}],
position: "left",
textcolor: "on",
backgroundcolor: "off"
};
// The actual plugin constructor
function Plugin ( element, options ) {
this.element = element;
// jQuery has an extend method which merges the contents of two or
// more objects, storing the result in the first object. The first object
// is generally empty as we don't want to alter the default options for
// future instances of the plugin
this.settings = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.init();
}
// Avoid Plugin.prototype conflicts
$.extend(Plugin.prototype, {
init: function () {
// Place initialization logic here
// You already have access to the DOM element and
// the options via the instance, e.g. this.element
// and this.settings
// you can add more functions like the one below and
// call them like so: this.yourOtherFunction(this.element, this.settings).
// retrieve the mouse coordinates if needed
var mouseX;
var mouseY;
jQuery(document).mousemove(function (e) {
mouseX = e.pageX;
mouseY = e.pageY - 10;
});
var self = this;
$(this.element).mouseup(function (e) {
if (self.getSelectedText() != "") {
var curr_span_id = self.add_cs_class(self.getSelectedText());
var curr_span_id_for_use = '#' + curr_span_id;
//Check settings and apply colors if needed.
if (self.settings.textcolor == "on")
{
var r_color = '#' + Math.random().toString(16).slice(2, 8);
$("#" + curr_span_id).css('color', r_color);
}
if (self.settings.backgroundcolor == "on")
{
var b_color = '#' + Math.random().toString(16).slice(2, 8);
if (r_color != b_color)
{
//Add classes to element for checking
$( "#" + curr_span_id ).add( ".light" ).css( "color", "#3A393C" );
$( "#" + curr_span_id ).add( ".dark" ).css( "color", "#FBFBFB" );
//Assign the element a background color
$("#" + curr_span_id).css('background-color', b_color);
// Target your element
$("#" + curr_span_id).colourBrightness();
}
else {
var b_color = '#' + Math.random().toString(16).slice(2, 8);
//Add classes to element for checking
$( "#" + curr_span_id ).add( ".light" ).css( "color", "#3A393C" );
$( "#" + curr_span_id ).add( ".dark" ).css( "color", "#FBFBFB" );
//Assign the element a background color
$("#" + curr_span_id).css('background-color', b_color);
// Target your element
$("#" + curr_span_id).colourBrightness();
}
}
//Check number of objects within json array. If more than one then remove the default and proceed to iterate through the actions to create the relevant buttons.
var num_of_objects = self.settings.actions.length;
var all_actions = self.settings.actions;
var first;
for (var key in all_actions[0]) {
//Get first object
first = key;
}
if (first == "test_action")
{
//Set the buttons
var all_buttons = "<button type='button' class='btn' onclick=test_action('" + curr_span_id_for_use + "')>Test Action</button>";
}
else
{
var arr = [];
for(var x in all_actions){
arr.push(all_actions[x]);
var buttons = '';
for (var i in arr) {
var but_array = arr[i];
var the_ch_function_name = Object.keys(but_array)[0];
var the_ch_value_name = but_array[the_ch_function_name];
buttons = buttons + "<button type='button' class='btn' onclick="+ the_ch_function_name + "('" + curr_span_id_for_use + "')>" + the_ch_value_name + "</button>" + " ";
}
//Set the buttons
var all_buttons = buttons;
}
}
$("#" + curr_span_id).tooltipster({
content: all_buttons,
multiple: true,
position: self.settings.position,
delay: 100,
maxWidth: 500,
speed: 300,
interactive: true,
animation: 'grow',
trigger: 'hover',
contentAsHTML: true
});
}
});
},
getSelectedText: function () {
// some logic
var t = (document.all) ? document.selection.createRange().text : document.getSelection();
return t;
},
add_cs_class: function (s_text) {
// some logic
var selection = s_text;
var selection_text = selection.toString();
// Add a span around the selected text, along with a class and an ID
var span = document.createElement('SPAN');
span.textContent = " + " + selection_text + " + ";
span.classList.add("cs-editing");
span.classList.add("tooltip");
var range = selection.getRangeAt(0);
range.deleteContents();
range.insertNode(span);
var custom_hash_1 = Math.random().toString(36).slice(2);
span.id = "cs-editing-" + custom_hash_1;
return span.id;
},
test_action: function () {
alert("Here's a Test Action!");
}
});
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[ pluginName ] = function ( options ) {
return this.each(function() {
if ( !$.data( this, "plugin_" + pluginName ) ) {
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
}
});
};
})( jQuery, window, document );