-
Notifications
You must be signed in to change notification settings - Fork 110
/
new_edit.js
165 lines (155 loc) · 6.41 KB
/
new_edit.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
import 'bootstrap-sass/assets/javascripts/bootstrap/collapse';
import { Tinymce } from '../../../utils/tinymce';
import { isObject, isString } from '../../../utils/isType';
import getConstant from '../../../constants';
import expandCollapseAll from '../../../utils/expandCollapseAll';
import onChangeQuestionFormat from '../questions/sharedEventHandlers';
import initQuestionOption from '../question_options/index';
$(() => {
// Attach handlers for the expand/collapse all accordions
expandCollapseAll();
Tinymce.init({ selector: '.phase' });
const parentSelector = '.section-group';
const initQuestion = (context) => {
const target = $(`#${context}`);
if (isObject(target)) {
// For some reason the toolbar options are retained after the call to
// Tinymce.init() on the views/notifications/edit.js file. Tried 'Object.assign'
// instead of '$.extend' but it made no difference.
Tinymce.init({
selector: `#${context} .question`,
init_instance_callback(editor) {
// When the text editor changes to blank, set the corresponding destroy
// field to true (if present).
editor.on('Change', () => {
const $texteditor = $(editor.targetElm);
const $fieldset = $texteditor.parents('fieldset');
const $hiddenField = $fieldset.find('input[type=hidden][name$="[_destroy]"]');
$hiddenField.val(editor.getContent() === '');
});
},
});
initQuestionOption(context);
// Swap in the question_formats when the user selects an option based question type
$(`#${context} select.question_format`).change((e) => {
onChangeQuestionFormat(e);
});
}
};
$('.question_container').each((i, element) => {
const questionId = $(element).attr('id');
initQuestion(questionId);
});
const getQuestionPanel = (target) => {
let panelBody;
if (isObject(target)) {
panelBody = target.closest('.question_container');
if (!isObject(panelBody) || !isString(panelBody.attr('id'))) {
panelBody = target.closest('.panel-body').find('.new-question');
}
}
return panelBody;
};
const initSection = (selector) => {
if (isString(selector)) {
// Wire up the section and its Questions
// For some reason the toolbar options are retained after the call to Tinymce.init() on
// the views/notifications/edit.js file. Tried 'Object.assign' instead of '$.extend' but it
// made no difference
Tinymce.init({
selector: `${selector} .section`,
init_instance_callback(editor) {
// When the text editor changes to blank, set the corresponding destroy
// field to true (if present).
editor.on('Change', () => {
const $texteditor = $(editor.targetElm);
const $fieldset = $texteditor.parents('fieldset');
const $hiddenField = $fieldset.find('input[type=hidden][id$="_destroy"]');
$hiddenField.val(editor.getContent() === '');
});
},
});
const questionForm = $(selector).find('.question_form');
if (questionForm.length > 0) {
// Load Tinymce when the 'show' form has a question form.
// ONLY applicable for template customizations
Tinymce.init({
selector: `${selector} .question_form .question`,
init_instance_callback(editor) {
// When the text editor changes to blank, set the corresponding destroy
// field to true (if present).
editor.on('Change', () => {
const $texteditor = $(editor.targetElm);
const $fieldset = $texteditor.parents('fieldset');
const $hiddenField = $fieldset.find('input[type=hidden][id$="_destroy"]');
$hiddenField.val(editor.getContent() === '');
});
},
});
}
}
};
// Attach handlers for the Section expansion
$(parentSelector).on('ajax:before', 'a.ajaxified-section[data-remote="true"]', (e) => {
const panelBody = $(e.target).parent().find('.panel-body');
return panelBody.attr('data-loaded') === 'false';
});
$(parentSelector).on('ajax:success', 'a.ajaxified-section[data-remote="true"]', (e, data) => {
const panelBody = $(e.target).parent().find('.panel-body');
const panel = panelBody.parent();
if (isObject(panelBody)) {
// Display the section's html
panelBody.attr('data-loaded', 'true');
panelBody.html(data);
// Wire up the section
initSection(`#${panel.attr('id')}`);
}
});
// Attach handlers for the Question show/edit/new
$(parentSelector).on('ajax:before', 'a.ajaxified-question[data-remote="true"]', (e) => {
const panelBody = getQuestionPanel($(e.target));
if (isObject(panelBody)) {
// Release any Tinymce editors that have been loaded
panelBody.find('.question').each((idx, el) => {
Tinymce.destroyEditorById($(el).attr('id'));
});
}
});
$(parentSelector).on('ajax:success', 'a.ajaxified-question[data-remote="true"]', (e, data) => {
const target = $(e.target);
const panelBody = getQuestionPanel(target);
if (isObject(panelBody)) {
const id = panelBody.attr('id');
// Display the section's html
panelBody.html(data);
initQuestion(id);
if (panelBody.is('.new-question')) {
target.hide();
}
}
});
$(parentSelector).on('ajax:error', 'a.ajaxified-question[data-remote="true"]', (e) => {
const panelBody = getQuestionPanel($(e.target));
if (isObject(panelBody)) {
panelBody.html(`<div class="pull-right alert alert-warning" role="alert">${getConstant('AJAX_UNABLE_TO_LOAD_TEMPLATE_SECTION')}</div>`);
}
});
// When we cancel the new question we just remove the form and its Tinymce editors
$(parentSelector).on('click', '.cancel-new-question', (e) => {
e.preventDefault();
const target = $(e.target);
const panel = target.closest('.question_container');
panel.find('.question').each((idx, el) => {
Tinymce.destroyEditorById($(el).attr('id'));
});
panel.html('');
panel.closest('.panel-body').find('.new-question-button a.ajaxified-question[data-remote="true"]').show();
});
// Handle the section that has focus on initial page load
const currentSection = $('.section-group .in');
if (currentSection.length > 0) {
initSection(`#${currentSection.attr('id')}`);
}
// Handle the new section
initSection('#new_section_new_section');
});