Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
!function($, wysi) {
2
    "use strict";
3
 
4
    var tpl = {
5
        "font-styles": function(locale, options) {
6
            var size = (options && options.size) ? ' btn-'+options.size : '';
7
            return "<li class='dropdown'>" +
8
              "<a class='btn default dropdown-toggle" + size + "' data-toggle='dropdown' href='#'>" +
9
              "<i class='icon-font'></i>&nbsp;<span class='current-font'>" + locale.font_styles.normal + "</span>&nbsp;<b class='caret'></b>" +
10
              "</a>" +
11
              "<ul class='dropdown-menu'>" +
12
                "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='div' tabindex='-1'>" + locale.font_styles.normal + "</a></li>" +
13
                "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h1' tabindex='-1'>" + locale.font_styles.h1 + "</a></li>" +
14
                "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h2' tabindex='-1'>" + locale.font_styles.h2 + "</a></li>" +
15
                "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h3' tabindex='-1'>" + locale.font_styles.h3 + "</a></li>" +
16
                "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h4'>" + locale.font_styles.h4 + "</a></li>" +
17
                "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h5'>" + locale.font_styles.h5 + "</a></li>" +
18
                "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h6'>" + locale.font_styles.h6 + "</a></li>" +
19
              "</ul>" +
20
            "</li>";
21
        },
22
 
23
        "emphasis": function(locale, options) {
24
            var size = (options && options.size) ? ' btn-'+options.size : '';
25
            return "<li>" +
26
              "<div class='btn-group'>" +
27
                "<a class='btn default" + size + "' data-wysihtml5-command='bold' title='CTRL+B' tabindex='-1'>" + locale.emphasis.bold + "</a>" +
28
                "<a class='btn default" + size + "' data-wysihtml5-command='italic' title='CTRL+I' tabindex='-1'>" + locale.emphasis.italic + "</a>" +
29
                "<a class='btn default" + size + "' data-wysihtml5-command='underline' title='CTRL+U' tabindex='-1'>" + locale.emphasis.underline + "</a>" +
30
              "</div>" +
31
            "</li>";
32
        },
33
 
34
        "lists": function(locale, options) {
35
            var size = (options && options.size) ? ' btn-'+options.size : '';
36
            return "<li>" +
37
              "<div class='btn-group'>" +
38
                "<a class='btn default" + size + "' data-wysihtml5-command='insertUnorderedList' title='" + locale.lists.unordered + "' tabindex='-1'><i class='fa fa-list'></i></a>" +
39
                "<a class='btn default" + size + "' data-wysihtml5-command='insertOrderedList' title='" + locale.lists.ordered + "' tabindex='-1'><i class='fa fa-th-list'></i></a>" +
40
                "<a class='btn default" + size + "' data-wysihtml5-command='Outdent' title='" + locale.lists.outdent + "' tabindex='-1'><i class='fa fa-outdent'></i></a>" +
41
                "<a class='btn default" + size + "' data-wysihtml5-command='Indent' title='" + locale.lists.indent + "' tabindex='-1'><i class='fa fa-indent'></i></a>" +
42
              "</div>" +
43
            "</li>";
44
        },
45
 
46
        "link": function(locale, options) {
47
            var size = (options && options.size) ? ' btn-'+options.size : '';
48
            return "<li>" +
49
              "<div class='bootstrap-wysihtml5-insert-link-modal modal fade'>" +
50
              ($.fn.modalmanager ? "" : "<div class='modal-dialog'>") +
51
              " <div class='modal-content'>" +
52
                "<div class='modal-header'>" +
53
                  "<a class='close' data-dismiss='modal'>&times;</a>" +
54
                  "<h3>" + locale.link.insert + "</h3>" +
55
                "</div>" +
56
                "<div class='modal-body'>" +
57
                  "<input value='http://' class='bootstrap-wysihtml5-insert-link-url form-control input-xlarge'>" +
58
                  "<label style='margin-top:5px;'> <input type='checkbox' class='bootstrap-wysihtml5-insert-link-target' checked>" + locale.link.target + "</label>" +
59
                "</div>" +
60
                "<div class='modal-footer'>" +
61
                  "<a href='#' class='btn default' data-dismiss='modal'>" + locale.link.cancel + "</a>" +
62
                  "<a href='#' class='btn btn-primary' data-dismiss='modal'>" + locale.link.insert + "</a>" +
63
                "</div>" +
64
              "</div>" +
65
               ($.fn.modalmanager ? "" : "</div>") +
66
              "</div>" +
67
              "<a class='btn default" + size + "' data-wysihtml5-command='createLink' title='" + locale.link.insert + "' tabindex='-1'><i class='fa fa-share'></i></a>" +
68
            "</li>";
69
        },
70
 
71
        "image": function(locale, options) {
72
            var size = (options && options.size) ? ' btn-'+options.size : '';
73
            return "<li>" +
74
              "<div class='bootstrap-wysihtml5-insert-image-modal modal fade'>" +
75
              ($.fn.modalmanager ? "" : "<div class='modal-dialog'>") +
76
              " <div class='modal-content'>" +
77
                "<div class='modal-header'>" +
78
                  "<a class='close' data-dismiss='modal'>&times;</a>" +
79
                  "<h3>" + locale.image.insert + "</h3>" +
80
                "</div>" +
81
                "<div class='modal-body'>" +
82
                  "<input value='http://' class='bootstrap-wysihtml5-insert-image-url form-control input-xlarge'>" +
83
                "</div>" +
84
                "<div class='modal-footer'>" +
85
                  "<a href='#' class='btn default' data-dismiss='modal'>" + locale.image.cancel + "</a>" +
86
                  "<a href='#' class='btn btn-primary' data-dismiss='modal'>" + locale.image.insert + "</a>" +
87
                "</div>" +
88
              "</div>" +
89
               ($.fn.modalmanager ? "" : "</div>") +
90
              "</div>" +
91
              "<a class='btn default" + size + "' data-wysihtml5-command='insertImage' title='" + locale.image.insert + "' tabindex='-1'><i class='fa fa-picture-o'></i></a>" +
92
            "</li>";
93
        },
94
 
95
        "html": function(locale, options) {
96
            var size = (options && options.size) ? ' btn-'+options.size : '';
97
            return "<li>" +
98
              "<div class='btn-group'>" +
99
                "<a class='btn default" + size + "' data-wysihtml5-action='change_view' title='" + locale.html.edit + "' tabindex='-1'><i class='fa fa-pencil'></i></a>" +
100
              "</div>" +
101
            "</li>";
102
        },
103
 
104
        "color": function(locale, options) {
105
            var size = (options && options.size) ? ' btn-'+options.size : '';
106
            return "<li class='dropdown'>" +
107
              "<a class='btn default dropdown-toggle" + size + "' data-toggle='dropdown' href='#' tabindex='-1'>" +
108
                "<span class='current-color'>" + locale.colours.black + "</span>&nbsp;<b class='caret'></b>" +
109
              "</a>" +
110
              "<ul class='dropdown-menu'>" +
111
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='black'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='black'>" + locale.colours.black + "</a></li>" +
112
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='silver'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='silver'>" + locale.colours.silver + "</a></li>" +
113
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='gray'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='gray'>" + locale.colours.gray + "</a></li>" +
114
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='maroon'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='maroon'>" + locale.colours.maroon + "</a></li>" +
115
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='red'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='red'>" + locale.colours.red + "</a></li>" +
116
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='purple'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='purple'>" + locale.colours.purple + "</a></li>" +
117
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='green'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='green'>" + locale.colours.green + "</a></li>" +
118
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='olive'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='olive'>" + locale.colours.olive + "</a></li>" +
119
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='navy'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='navy'>" + locale.colours.navy + "</a></li>" +
120
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='blue'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='blue'>" + locale.colours.blue + "</a></li>" +
121
                "<li><div class='wysihtml5-colors' data-wysihtml5-command-value='orange'></div><a class='wysihtml5-colors-title' data-wysihtml5-command='foreColor' data-wysihtml5-command-value='orange'>" + locale.colours.orange + "</a></li>" +
122
              "</ul>" +
123
            "</li>";
124
        }
125
    };
126
 
127
    var templates = function(key, locale, options) {
128
        return tpl[key](locale, options);
129
    };
130
 
131
 
132
    var Wysihtml5 = function(el, options) {
133
        this.el = el;
134
        var toolbarOpts = options || defaultOptions;
135
        for(var t in toolbarOpts.customTemplates) {
136
          tpl[t] = toolbarOpts.customTemplates[t];
137
        }
138
        this.toolbar = this.createToolbar(el, toolbarOpts);
139
        this.editor =  this.createEditor(options);
140
 
141
        window.editor = this.editor;
142
 
143
        $('iframe.wysihtml5-sandbox').each(function(i, el){
144
            $(el.contentWindow).off('focus.wysihtml5').on({
145
                'focus.wysihtml5' : function(){
146
                    $('li.dropdown').removeClass('open');
147
                }
148
            });
149
        });
150
    };
151
 
152
    Wysihtml5.prototype = {
153
 
154
        constructor: Wysihtml5,
155
 
156
        createEditor: function(options) {
157
            options = options || {};
158
 
159
            // Add the toolbar to a clone of the options object so multiple instances
160
            // of the WYISYWG don't break because "toolbar" is already defined
161
            options = $.extend(true, {}, options);
162
            options.toolbar = this.toolbar[0];
163
 
164
            var editor = new wysi.Editor(this.el[0], options);
165
 
166
            if(options && options.events) {
167
                for(var eventName in options.events) {
168
                    editor.on(eventName, options.events[eventName]);
169
                }
170
            }
171
            return editor;
172
        },
173
 
174
        createToolbar: function(el, options) {
175
            var self = this;
176
            var toolbar = $("<ul/>", {
177
                'class' : "wysihtml5-toolbar",
178
                'style': "display:none"
179
            });
180
            var culture = options.locale || defaultOptions.locale || "en";
181
            for(var key in defaultOptions) {
182
                var value = false;
183
 
184
                if(options[key] !== undefined) {
185
                    if(options[key] === true) {
186
                        value = true;
187
                    }
188
                } else {
189
                    value = defaultOptions[key];
190
                }
191
 
192
                if(value === true) {
193
                    toolbar.append(templates(key, locale[culture], options));
194
 
195
                    if(key === "html") {
196
                        this.initHtml(toolbar);
197
                    }
198
 
199
                    if(key === "link") {
200
                        this.initInsertLink(toolbar);
201
                    }
202
 
203
                    if(key === "image") {
204
                        this.initInsertImage(toolbar);
205
                    }
206
                }
207
            }
208
 
209
            if(options.toolbar) {
210
                for(key in options.toolbar) {
211
                    toolbar.append(options.toolbar[key]);
212
                }
213
            }
214
 
215
            toolbar.find("a[data-wysihtml5-command='formatBlock']").click(function(e) {
216
                var target = e.target || e.srcElement;
217
                var el = $(target);
218
                self.toolbar.find('.current-font').text(el.html());
219
            });
220
 
221
            toolbar.find("a[data-wysihtml5-command='foreColor']").click(function(e) {
222
                var target = e.target || e.srcElement;
223
                var el = $(target);
224
                self.toolbar.find('.current-color').text(el.html());
225
            });
226
 
227
            this.el.before(toolbar);
228
 
229
            return toolbar;
230
        },
231
 
232
        initHtml: function(toolbar) {
233
            var changeViewSelector = "a[data-wysihtml5-action='change_view']";
234
            toolbar.find(changeViewSelector).click(function(e) {
235
                toolbar.find('a.btn').not(changeViewSelector).toggleClass('disabled');
236
            });
237
        },
238
 
239
        initInsertImage: function(toolbar) {
240
            var self = this;
241
            var insertImageModal = toolbar.find('.bootstrap-wysihtml5-insert-image-modal');
242
            var urlInput = insertImageModal.find('.bootstrap-wysihtml5-insert-image-url');
243
            var insertButton = insertImageModal.find('a.btn-primary');
244
            var initialValue = urlInput.val();
245
            var caretBookmark;
246
 
247
            var insertImage = function() {
248
                var url = urlInput.val();
249
                urlInput.val(initialValue);
250
                self.editor.currentView.element.focus();
251
                if (caretBookmark) {
252
                  self.editor.composer.selection.setBookmark(caretBookmark);
253
                  caretBookmark = null;
254
                }
255
                self.editor.composer.commands.exec("insertImage", url);
256
            };
257
 
258
            urlInput.keypress(function(e) {
259
                if(e.which == 13) {
260
                    insertImage();
261
                    insertImageModal.modal('hide');
262
                }
263
            });
264
 
265
            insertButton.click(insertImage);
266
 
267
            insertImageModal.on('shown', function() {
268
                urlInput.focus();
269
            });
270
 
271
            insertImageModal.on('hide', function() {
272
                self.editor.currentView.element.focus();
273
            });
274
 
275
            toolbar.find('a[data-wysihtml5-command=insertImage]').click(function() {
276
                var activeButton = $(this).hasClass("wysihtml5-command-active");
277
 
278
                if (!activeButton) {
279
                    self.editor.currentView.element.focus(false);
280
                    caretBookmark = self.editor.composer.selection.getBookmark();
281
                    insertImageModal.appendTo('body').modal('show');
282
                    insertImageModal.on('click.dismiss.modal', '[data-dismiss="modal"]', function(e) {
283
                        e.stopPropagation();
284
                    });
285
                    return false;
286
                }
287
                else {
288
                    return true;
289
                }
290
            });
291
        },
292
 
293
        initInsertLink: function(toolbar) {
294
            var self = this;
295
            var insertLinkModal = toolbar.find('.bootstrap-wysihtml5-insert-link-modal');
296
            var urlInput = insertLinkModal.find('.bootstrap-wysihtml5-insert-link-url');
297
            var targetInput = insertLinkModal.find('.bootstrap-wysihtml5-insert-link-target');
298
            var insertButton = insertLinkModal.find('a.btn-primary');
299
            var initialValue = urlInput.val();
300
            var caretBookmark;
301
 
302
            var insertLink = function() {
303
                var url = urlInput.val();
304
                urlInput.val(initialValue);
305
                self.editor.currentView.element.focus();
306
                if (caretBookmark) {
307
                  self.editor.composer.selection.setBookmark(caretBookmark);
308
                  caretBookmark = null;
309
                }
310
 
311
                var newWindow = targetInput.prop("checked");
312
                self.editor.composer.commands.exec("createLink", {
313
                    'href' : url,
314
                    'target' : (newWindow ? '_blank' : '_self'),
315
                    'rel' : (newWindow ? 'nofollow' : '')
316
                });
317
            };
318
            var pressedEnter = false;
319
 
320
            urlInput.keypress(function(e) {
321
                if(e.which == 13) {
322
                    insertLink();
323
                    insertLinkModal.modal('hide');
324
                }
325
            });
326
 
327
            insertButton.click(insertLink);
328
 
329
            insertLinkModal.on('shown', function() {
330
                urlInput.focus();
331
            });
332
 
333
            insertLinkModal.on('hide', function() {
334
                self.editor.currentView.element.focus();
335
            });
336
 
337
            toolbar.find('a[data-wysihtml5-command=createLink]').click(function() {
338
                var activeButton = $(this).hasClass("wysihtml5-command-active");
339
 
340
                if (!activeButton) {
341
                    self.editor.currentView.element.focus(false);
342
                    caretBookmark = self.editor.composer.selection.getBookmark();
343
                    insertLinkModal.appendTo('body').modal('show');
344
                    Metronic.initUniform(); //initialize uniform checkboxes
345
                    insertLinkModal.on('click.dismiss.modal', '[data-dismiss="modal"]', function(e) {
346
                        e.stopPropagation();
347
                    });
348
                    return false;
349
                }
350
                else {
351
                    return true;
352
                }
353
            });
354
        }
355
    };
356
 
357
    // these define our public api
358
    var methods = {
359
        resetDefaults: function() {
360
            $.fn.wysihtml5.defaultOptions = $.extend(true, {}, $.fn.wysihtml5.defaultOptionsCache);
361
        },
362
        bypassDefaults: function(options) {
363
            return this.each(function () {
364
                var $this = $(this);
365
                $this.data('wysihtml5', new Wysihtml5($this, options));
366
            });
367
        },
368
        shallowExtend: function (options) {
369
            var settings = $.extend({}, $.fn.wysihtml5.defaultOptions, options || {}, $(this).data());
370
            var that = this;
371
            return methods.bypassDefaults.apply(that, [settings]);
372
        },
373
        deepExtend: function(options) {
374
            var settings = $.extend(true, {}, $.fn.wysihtml5.defaultOptions, options || {});
375
            var that = this;
376
            return methods.bypassDefaults.apply(that, [settings]);
377
        },
378
        init: function(options) {
379
            var that = this;
380
            return methods.shallowExtend.apply(that, [options]);
381
        }
382
    };
383
 
384
    $.fn.wysihtml5 = function ( method ) {
385
        if ( methods[method] ) {
386
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
387
        } else if ( typeof method === 'object' || ! method ) {
388
            return methods.init.apply( this, arguments );
389
        } else {
390
            $.error( 'Method ' +  method + ' does not exist on jQuery.wysihtml5' );
391
        }    
392
    };
393
 
394
    $.fn.wysihtml5.Constructor = Wysihtml5;
395
 
396
    var defaultOptions = $.fn.wysihtml5.defaultOptions = {
397
        "font-styles": true,
398
        "color": false,
399
        "emphasis": true,
400
        "lists": true,
401
        "html": true,
402
        "link": true,
403
        "image": true,
404
        events: {},
405
        parserRules: {
406
            classes: {
407
                // (path_to_project/lib/css/wysiwyg-color.css)
408
                "wysiwyg-color-silver" : 1,
409
                "wysiwyg-color-gray" : 1,
410
                "wysiwyg-color-white" : 1,
411
                "wysiwyg-color-maroon" : 1,
412
                "wysiwyg-color-red" : 1,
413
                "wysiwyg-color-purple" : 1,
414
                "wysiwyg-color-fuchsia" : 1,
415
                "wysiwyg-color-green" : 1,
416
                "wysiwyg-color-lime" : 1,
417
                "wysiwyg-color-olive" : 1,
418
                "wysiwyg-color-yellow" : 1,
419
                "wysiwyg-color-navy" : 1,
420
                "wysiwyg-color-blue" : 1,
421
                "wysiwyg-color-teal" : 1,
422
                "wysiwyg-color-aqua" : 1,
423
                "wysiwyg-color-orange" : 1
424
            },
425
            tags: {
426
                "b":  {},
427
                "i":  {},
428
                "br": {},
429
                "ol": {},
430
                "ul": {},
431
                "li": {},
432
                "h1": {},
433
                "h2": {},
434
                "h3": {},
435
                "h4": {},
436
                "h5": {},
437
                "h6": {},
438
                "blockquote": {},
439
                "u": 1,
440
                "img": {
441
                    "check_attributes": {
442
                        "width": "numbers",
443
                        "alt": "alt",
444
                        "src": "url",
445
                        "height": "numbers"
446
                    }
447
                },
448
                "a":  {
449
                    check_attributes: {
450
                        'href': "url", // important to avoid XSS
451
                        'target': 'alt',
452
                        'rel': 'alt'
453
                    }
454
                },
455
                "span": 1,
456
                "div": 1,
457
                // to allow save and edit files with code tag hacks
458
                "code": 1,
459
                "pre": 1
460
            }
461
        },
462
        stylesheets: ["wysiwyg-color.css"], // (path_to_project/lib/css/wysiwyg-color.css)
463
        locale: "en"
464
    };
465
 
466
    if (typeof $.fn.wysihtml5.defaultOptionsCache === 'undefined') {
467
        $.fn.wysihtml5.defaultOptionsCache = $.extend(true, {}, $.fn.wysihtml5.defaultOptions);
468
    }
469
 
470
    var locale = $.fn.wysihtml5.locale = {
471
        en: {
472
            font_styles: {
473
                normal: "Normal text",
474
                h1: "Heading 1",
475
                h2: "Heading 2",
476
                h3: "Heading 3",
477
                h4: "Heading 4",
478
                h5: "Heading 5",
479
                h6: "Heading 6"
480
            },
481
            emphasis: {
482
                bold: "Bold",
483
                italic: "Italic",
484
                underline: "Underline"
485
            },
486
            lists: {
487
                unordered: "Unordered list",
488
                ordered: "Ordered list",
489
                outdent: "Outdent",
490
                indent: "Indent"
491
            },
492
            link: {
493
                insert: "Insert link",
494
                cancel: "Cancel",
495
                target: "Open link in new window"
496
            },
497
            image: {
498
                insert: "Insert image",
499
                cancel: "Cancel"
500
            },
501
            html: {
502
                edit: "Edit HTML"
503
            },
504
            colours: {
505
                black: "Black",
506
                silver: "Silver",
507
                gray: "Grey",
508
                maroon: "Maroon",
509
                red: "Red",
510
                purple: "Purple",
511
                green: "Green",
512
                olive: "Olive",
513
                navy: "Navy",
514
                blue: "Blue",
515
                orange: "Orange"
516
            }
517
        }
518
    };
519
 
520
}(window.jQuery, window.wysihtml5);