Consulter le code

Create scripts.js

Ivan FELICITE a écrit le 16/02/2020 14:11:42
Affichage de 1 fichier(s) changé(s)
1 1
new file mode 100644
... ...
@@ -0,0 +1,259 @@
0
+"use strict";
1
+
2
+// Insert text into textarea at Caret Position
3
+function insert_at_caret(areaId, text) {
4
+    var txtarea = document.getElementById(areaId),
5
+        scrollPos = txtarea.scrollTop,
6
+        strPos = 0,
7
+        br = ((txtarea.selectionStart || txtarea.selectionStart === '0') ?
8
+            "ff" : (document.selection ? "ie" : false)),
9
+        range;
10
+
11
+    if (br === "ie") {
12
+        txtarea.focus();
13
+        range = document.selection.createRange();
14
+        range.moveStart('character', -txtarea.value.length);
15
+        strPos = range.text.length;
16
+    } else if (br === "ff") {
17
+        strPos = txtarea.selectionStart;
18
+    }
19
+
20
+    var front = (txtarea.value).substring(0, strPos),
21
+        back = (txtarea.value).substring(strPos, txtarea.value.length);
22
+
23
+    txtarea.value = front + text + back;
24
+    strPos = strPos + text.length;
25
+    if (br === "ie") {
26
+        txtarea.focus();
27
+        range = document.selection.createRange();
28
+        range.moveStart('character', -txtarea.value.length);
29
+        range.moveStart('character', strPos);
30
+        range.moveEnd('character', 0);
31
+        range.select();
32
+    } else if (br === "ff") {
33
+        txtarea.selectionStart = strPos;
34
+        txtarea.selectionEnd = strPos;
35
+        txtarea.focus();
36
+    }
37
+    txtarea.scrollTop = scrollPos;
38
+}
39
+
40
+// takes mdl_email_template row as JSON, array with names to use in form fields.
41
+function inject_email_template(template_fields, email_template) {
42
+    $.each(email_template, function (key, val) {
43
+        // remove prefix from key
44
+        key = key.replace("email_template_", "");
45
+        // if key is in template_fields, apply value to form field
46
+        if (val && template_fields.indexOf(key) > -1) {
47
+            if (key === 'body') {
48
+                $("#" + key).text(val);
49
+            } else if (key === 'pdf_template') {
50
+                $("#" + key).val(val).trigger('change');
51
+            } else {
52
+                $("#" + key).val(val);
53
+            }
54
+        }
55
+    });
56
+}
57
+
58
+function update_email_template_preview() {
59
+    $('#email-template-preview').contents().find("body").html($('.email-template-body').val());
60
+}
61
+
62
+// Insert HTML tags into textarea
63
+function insert_html_tag(tag_type, destination_id) {
64
+    var text, sel, text_area, selectedText, startPos, endPos, replace, replaceText, len;
65
+    switch (tag_type) {
66
+        case 'text-bold':
67
+            text = ['<b>', '</b>'];
68
+            break;
69
+        case 'text-italic':
70
+            text = ['<em>', '</em>'];
71
+            break;
72
+        case 'text-paragraph':
73
+            text = ['<p>', '</p>'];
74
+            break;
75
+        case 'text-linebreak':
76
+            text = ['<br>', ''];
77
+            break;
78
+
79
+        case 'text-h1':
80
+            text = ['<h1>', '</h1>'];
81
+            break;
82
+        case 'text-h2':
83
+            text = ['<h2>', '</h2>'];
84
+            break;
85
+        case 'text-h3':
86
+            text = ['<h3>', '</h3>'];
87
+            break;
88
+        case 'text-h4':
89
+            text = ['<h4>', '</h4>'];
90
+            break;
91
+
92
+        case 'text-code':
93
+            text = ['<code>', '</code>'];
94
+            break;
95
+        case 'text-hr':
96
+            text = ['<hr/>', ''];
97
+            break;
98
+        case 'text-css':
99
+            text = ['<style></style>', ''];
100
+            break;
101
+    }
102
+
103
+    // Get the selected text
104
+    text_area = document.getElementById(destination_id);
105
+    if (document.selection !== undefined) {
106
+        text_area.focus();
107
+        sel = document.selection.createRange();
108
+        selectedText = sel.text;
109
+    }
110
+    else if (text_area.selectionStart !== '') {
111
+        startPos = text_area.selectionStart;
112
+        endPos = text_area.selectionEnd;
113
+        selectedText = text_area.value.substring(startPos, endPos);
114
+    }
115
+
116
+    // Check if <style> should be added
117
+    if (tag_type === 'text-css') {
118
+        replace = text[0] + '\n\r' + text_area.value;
119
+        $(text_area).val(replace);
120
+        update_email_template_preview();
121
+        return true;
122
+    }
123
+
124
+    // Check if there is only one HTML tag
125
+    if (text[1].length === 0) {
126
+        insert_at_caret(destination_id, text[0]);
127
+        update_email_template_preview();
128
+        return true;
129
+    }
130
+
131
+    // Check if text is selected, replace it or just insert the tag at cursor position
132
+    if (!selectedText || !selectedText.length) {
133
+        text = text[0] + text[1];
134
+        insert_at_caret(destination_id, text);
135
+        update_email_template_preview();
136
+    } else {
137
+        replaceText = text[0] + selectedText + text[1];
138
+        len = text_area.value.length;
139
+        replace = text_area.value.substring(0, startPos) + replaceText + text_area.value.substring(endPos, len);
140
+        $(text_area).val(replace);
141
+        update_email_template_preview();
142
+    }
143
+}
144
+
145
+$(document).ready(function () {
146
+
147
+    // Automatical CSRF protection for all POST requests
148
+    $.ajaxPrefilter(function (options) {
149
+        if (options.type === 'post' || options.type === 'POST' || options.type === 'Post') {
150
+            if (options.data === '') {
151
+                options.data += '?_ip_csrf=' + Cookies.get('ip_csrf_cookie');
152
+            } else {
153
+                options.data += '&_ip_csrf=' + Cookies.get('ip_csrf_cookie');
154
+            }
155
+        }
156
+    });
157
+
158
+    $(document).ajaxComplete(function () {
159
+        $('[name="_ip_csrf"]').val(Cookies.get('ip_csrf_cookie'));
160
+    });
161
+
162
+    // Correct the height of the content area
163
+    var $content = $('#content'),
164
+        $html = $('html');
165
+
166
+    var documentHeight = $html.outerHeight(),
167
+        navbarHeight = $('.navbar').outerHeight(),
168
+        headerbarHeight = $('#headerbar').outerHeight(),
169
+        submenuHeight = $('#submenu').outerHeight(),
170
+        contentHeight = documentHeight - navbarHeight - headerbarHeight - submenuHeight;
171
+    if ($content.outerHeight() < contentHeight) {
172
+        $content.outerHeight(contentHeight);
173
+    }
174
+
175
+    // Dropdown Datepicker fix
176
+    $html.click(function () {
177
+        $('.dropdown-menu:visible').not('.datepicker').removeAttr('style');
178
+    });
179
+
180
+    // Tooltips
181
+    $('[data-toggle="tooltip"]').tooltip();
182
+
183
+    // Select2 for all select inputs
184
+    $('.simple-select').select2();
185
+
186
+    // Enable clipboard toggles
187
+    var clipboards = new ClipboardJS('.to-clipboard');
188
+
189
+    // Keep track of the last "taggable" input/textarea
190
+    $('.taggable').on('focus', function () {
191
+        window.lastTaggableClicked = this;
192
+    });
193
+
194
+    // Template Tag handling
195
+    $('.tag-select').select2().on('change', function (event) {
196
+        var select = $(event.currentTarget);
197
+        // Add the tag to the field
198
+        if (typeof window.lastTaggableClicked !== 'undefined') {
199
+            insert_at_caret(window.lastTaggableClicked.id, select.val());
200
+        }
201
+
202
+        // Reset the select and exit
203
+        select.val([]);
204
+        return false;
205
+    });
206
+
207
+    // HTML tags to email templates textarea
208
+    $('.html-tag').click(function () {
209
+        var tag_type = $(this).data('tagType');
210
+        var body_id = $('.email-template-body').attr('id');
211
+        insert_html_tag(tag_type, body_id);
212
+    });
213
+
214
+    // Email Template Preview handling
215
+    var email_template_body_id = $('.email-template-body').attr('id');
216
+
217
+    if ($('#email_template_preview').empty()) {
218
+        update_email_template_preview();
219
+    }
220
+
221
+    $(email_template_body_id).bind('input propertychange', function () {
222
+        update_email_template_preview();
223
+    });
224
+
225
+    $('#email-template-preview-reload').click(function () {
226
+        update_email_template_preview();
227
+    });
228
+
229
+    // Fullpage loader
230
+    $(document).on('click', '.ajax-loader', function () {
231
+        $('#fullpage-loader').fadeIn(200);
232
+        window.fullpageloaderTimeout = window.setTimeout(function () {
233
+            $('#loader-error').fadeIn(200);
234
+            $('#loader-icon').removeClass('fa-spin').addClass('text-danger');
235
+        }, 10000);
236
+    });
237
+
238
+    $(document).on('click', '.fullpage-loader-close', function () {
239
+        $('#fullpage-loader').fadeOut(200);
240
+        $('#loader-error').hide();
241
+        $('#loader-icon').addClass('fa-spin').removeClass('text-danger');
242
+        clearTimeout(window.fullpageloaderTimeout);
243
+    });
244
+
245
+    var password_input = $('.passwordmeter-input');
246
+    if (password_input) {
247
+        password_input.on('input', function(){
248
+            var strength = zxcvbn(password_input.val());
249
+
250
+            $('.passmeter-2, .passmeter-3').hide();
251
+            if (strength.score === 4) {
252
+                $('.passmeter-2, .passmeter-3').show();
253
+            } else if (strength.score === 3) {
254
+                $('.passmeter-2').show();
255
+            }
256
+        });
257
+    }
258
+});