Administrator
2023-04-21 195945efc5db921a4c9eb8cf9421c172273293f5
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
 <b>Wysiwyg</b>. A wrapper for Bootstrap wyswiwyg plugin.
 It's just a wrapper so you still need to include Bootstrap wysiwyg script first.
*/
(function($ , undefined) {
    $.fn.ace_wysiwyg = function($options , undefined) {
        var options = $.extend( {
            speech_button:true,
            wysiwyg:{}
        }, $options);
 
        var color_values = [
            '#ac725e','#d06b64','#f83a22','#fa573c','#ff7537','#ffad46',
            '#42d692','#16a765','#7bd148','#b3dc6c','#fbe983','#fad165',
            '#92e1c0','#9fe1e7','#9fc6e7','#4986e7','#9a9cff','#b99aff',
            '#c2c2c2','#cabdbf','#cca6ac','#f691b2','#cd74e6','#a47ae2',
            '#444444'
        ]
 
        var button_defaults =
        {
            'font' : {
                values:['Arial', 'Courier', 'Comic Sans MS', 'Helvetica', 'Open Sans', 'Tahoma', 'Verdana'],
                icon:'fa fa-font',
                title:'Font'
            },
            'fontSize' : {
                values:{5:'Huge', 3:'Normal', 1:'Small'},
                icon:'fa fa-text-height',
                title:'Font Size'
            },
            'bold' : {
                icon : 'fa fa-bold',
                title : 'Bold (Ctrl/Cmd+B)'
            },
            'italic' : {
                icon : 'fa fa-italic',
                title : 'Italic (Ctrl/Cmd+I)'
            },
            'strikethrough' : {
                icon : 'fa fa-strikethrough',
                title : 'Strikethrough'
            },
            'underline' : {
                icon : 'fa fa-underline',
                title : 'Underline'
            },
            'insertunorderedlist' : {
                icon : 'fa fa-list-ul',
                title : 'Bullet list'
            },
            'insertorderedlist' : {
                icon : 'fa fa-list-ol',
                title : 'Number list'
            },
            'outdent' : {
                icon : 'fa fa-outdent',
                title : 'Reduce indent (Shift+Tab)'
            },
            'indent' : {
                icon : 'fa fa-indent',
                title : 'Indent (Tab)'
            },
            'justifyleft' : {
                icon : 'fa fa-align-left',
                title : 'Align Left (Ctrl/Cmd+L)'
            },
            'justifycenter' : {
                icon : 'fa fa-align-center',
                title : 'Center (Ctrl/Cmd+E)'
            },
            'justifyright' : {
                icon : 'fa fa-align-right',
                title : 'Align Right (Ctrl/Cmd+R)'
            },
            'justifyfull' : {
                icon : 'fa fa-align-justify',
                title : 'Justify (Ctrl/Cmd+J)'
            },
            'createLink' : {
                icon : 'fa fa-link',
                title : 'Hyperlink',
                button_text : 'Add',
                placeholder : 'URL',
                button_class : 'btn-primary'
            },
            'unlink' : {
                icon : 'fa fa-chain-broken',
                title : 'Remove Hyperlink'
            },
            'insertImage' : {
                icon : 'fa fa-picture-o',
                title : 'Insert picture',
                button_text : '<i class="'+ ace.vars['icon'] + 'fa fa-file"></i> Choose Image &hellip;',
                placeholder : 'Image URL',
                button_insert : 'Insert',
                button_class : 'btn-success',
                button_insert_class : 'btn-primary',
                choose_file: true //show the choose file button?
            },
            'foreColor' : {
                values : color_values,
                title : 'Change Color'
            },
            'backColor' : {
                values : color_values,
                title : 'Change Background Color'
            },
            'undo' : {
                icon : 'fa fa-undo',
                title : 'Undo (Ctrl/Cmd+Z)'
            },
            'redo' : {
                icon : 'fa fa-repeat',
                title : 'Redo (Ctrl/Cmd+Y)'
            },
            'viewSource' : {
                icon : 'fa fa-code',
                title : 'View Source'
            }
        }
        
        var toolbar_buttons =
        options.toolbar ||
        [
            'font',
            null,
            'fontSize',
            null,
            'bold',
            'italic',
            'strikethrough',
            'underline',
            null,
            'insertunorderedlist',
            'insertorderedlist',
            'outdent',
            'indent',
            null,
            'justifyleft',
            'justifycenter',
            'justifyright',
            'justifyfull',
            null,
            'createLink',
            'unlink',
            null,
            'insertImage',
            null,
            'foreColor',
            null,
            'undo',
            'redo',
            null,
            'viewSource'
        ]
 
 
        this.each(function() {
            var toolbar = ' <div class="wysiwyg-toolbar btn-toolbar center"> <div class="btn-group"> ';
 
            for(var tb in toolbar_buttons) if(toolbar_buttons.hasOwnProperty(tb)) {
                var button = toolbar_buttons[tb];
                if(button === null){
                    toolbar += ' </div> <div class="btn-group"> ';
                    continue;
                }
                
                if(typeof button == "string" && button in button_defaults) {
                    button = button_defaults[button];
                    button.name = toolbar_buttons[tb];
                } else if(typeof button == "object" && button.name in button_defaults) {
                    button = $.extend(button_defaults[button.name] , button);
                }
                else continue;
                
                var className = "className" in button ? button.className : 'btn-default';
                switch(button.name) {
                    case 'font':
                        toolbar += ' <a class="btn btn-sm '+className+' dropdown-toggle" data-toggle="dropdown" title="'+button.title+'"><i class="'+ ace.vars['icon'] + button.icon+'"></i><i class="' + ace.vars['icon'] + 'fa fa-angle-down icon-on-right"></i></a> ';
                        toolbar += ' <ul class="dropdown-menu dropdown-light dropdown-caret">';
                        for(var font in button.values)
                            if(button.values.hasOwnProperty(font))
                                toolbar += ' <li><a data-edit="fontName ' + button.values[font] +'" style="font-family:\''+ button.values[font]  +'\'">'+button.values[font]  + '</a></li> '
                        toolbar += ' </ul>';
                    break;
 
                    case 'fontSize':
                        toolbar += ' <a class="btn btn-sm '+className+' dropdown-toggle" data-toggle="dropdown" title="'+button.title+'"><i class="'+ ace.vars['icon'] + button.icon+'"></i>&nbsp;<i class="'+ ace.vars['icon'] + 'fa fa-angle-down icon-on-right"></i></a> ';
                        toolbar += ' <ul class="dropdown-menu dropdown-light dropdown-caret"> ';
                        for(var size in button.values)
                            if(button.values.hasOwnProperty(size))
                                toolbar += ' <li><a data-edit="fontSize '+size+'"><font size="'+size+'">'+ button.values[size] +'</font></a></li> '
                        toolbar += ' </ul> ';
                    break;
 
                    case 'createLink':
                        toolbar += ' <div class="btn-group"> <a class="btn btn-sm '+className+' dropdown-toggle" data-toggle="dropdown" title="'+button.title+'"><i class="'+ ace.vars['icon'] + button.icon+'"></i></a> ';
                        toolbar += ' <div class="dropdown-menu dropdown-caret dropdown-menu-right">\
                             <div class="input-group">\
                                <input class="form-control" placeholder="'+button.placeholder+'" type="text" data-edit="'+button.name+'" />\
                                <span class="input-group-btn">\
                                    <button class="btn btn-sm '+button.button_class+'" type="button">'+button.button_text+'</button>\
                                </span>\
                             </div>\
                        </div> </div>';
                    break;
 
                    case 'insertImage':
                        toolbar += ' <div class="btn-group"> <a class="btn btn-sm '+className+' dropdown-toggle" data-toggle="dropdown" title="'+button.title+'"><i class="'+ ace.vars['icon'] + button.icon+'"></i></a> ';
                        toolbar += ' <div class="dropdown-menu dropdown-caret dropdown-menu-right">\
                             <div class="input-group">\
                                <input class="form-control" placeholder="'+button.placeholder+'" type="text" data-edit="'+button.name+'" />\
                                <span class="input-group-btn">\
                                    <button class="btn btn-sm '+button.button_insert_class+'" type="button">'+button.button_insert+'</button>\
                                </span>\
                             </div>';
                            if( button.choose_file && 'FileReader' in window ) toolbar +=
                             '<div class="space-2"></div>\
                             <label class="center block no-margin-bottom">\
                                <button class="btn btn-sm '+button.button_class+' wysiwyg-choose-file" type="button">'+button.button_text+'</button>\
                                <input type="file" data-edit="'+button.name+'" />\
                              </label>'
                        toolbar += ' </div> </div>';
                    break;
 
                    case 'foreColor':
                    case 'backColor':
                        toolbar += ' <select class="hide wysiwyg_colorpicker" title="'+button.title+'"> ';
                        $.each(button.values, function (_, color) {
                            toolbar += ' <option value="' + color + '">' + color + '</option> ';
                        });
                        toolbar += ' </select> ';
                        toolbar += ' <input style="display:none;" disabled class="hide" type="text" data-edit="'+button.name+'" /> ';
                    break;
 
                    case 'viewSource':
                        toolbar += ' <a class="btn btn-sm '+className+'" data-view="source" title="'+button.title+'"><i class="'+ ace.vars['icon'] + button.icon+'"></i></a> ';
                    break;
                    default:
                        toolbar += ' <a class="btn btn-sm '+className+'" data-edit="'+button.name+'" title="'+button.title+'"><i class="'+ ace.vars['icon'] + button.icon+'"></i></a> ';
                    break;
                }
            }
            toolbar += ' </div> ';
            ////////////
            var speech_input;
            if (options.speech_button && 'onwebkitspeechchange' in (speech_input = document.createElement('input'))) {
                toolbar += ' <input class="wysiwyg-speech-input" type="text" data-edit="inserttext" x-webkit-speech />';
            }
            speech_input = null;
            ////////////
            toolbar += ' </div> ';
 
 
            //if we have a function to decide where to put the toolbar, then call that
            if(options.toolbar_place) toolbar = options.toolbar_place.call(this, toolbar);
            //otherwise put it just before our DIV
            else toolbar = $(this).before(toolbar).prev();
 
            toolbar.find('a[title]').tooltip({animation:false, container:'body'});
            toolbar.find('.dropdown-menu input[type=text]').on('click', function() {return false})
            .on('change', function() {$(this).closest('.dropdown-menu').siblings('.dropdown-toggle').dropdown('toggle')})
            .on('keydown', function (e) {
                if(e.which == 27) {
                    this.value = '';
                    $(this).change();
                }
                else if(e.which == 13) {
                    e.preventDefault();
                    e.stopPropagation();
                    $(this).change();
                }
            });
            
            toolbar.find('input[type=file]').prev().on(ace.click_event, function (e) { 
                $(this).next().click();
            });
            toolbar.find('.wysiwyg_colorpicker').each(function() {
                $(this).ace_colorpicker({pull_right:true}).change(function(){
                    $(this).nextAll('input').eq(0).val(this.value).change();
                }).next().find('.btn-colorpicker').tooltip({title: this.title, animation:false, container:'body'})
            });
            
            
            var self = $(this);
            //view source
            var view_source = false;
            toolbar.find('a[data-view=source]').on('click', function(e){
                e.preventDefault();
                
                if(!view_source) {
                    $('<textarea />')
                    .css({'width':self.outerWidth(), 'height':self.outerHeight()})
                    .val(self.html())
                    .insertAfter(self)
                    self.hide();
                    
                    $(this).addClass('active');
                }
                else {
                    var textarea = self.next();
                    self.html(textarea.val()).show();
                    textarea.remove();
                    
                    $(this).removeClass('active');
                }
                
                view_source = !view_source;
            });
 
 
            var $options = $.extend({}, { activeToolbarClass: 'active' , toolbarSelector : toolbar }, options.wysiwyg || {})
            $(this).wysiwyg( $options );
        });
 
        return this;
    }
 
 
})(window.jQuery);