|
1 | 1 | (function () { |
2 | 2 | function Form() { |
| 3 | + var reply_selector = '#newMessage'; |
| 4 | + |
3 | 5 | /** |
4 | 6 | * Key, value pair of editors which have been initialised. |
5 | 7 | * Key is the text editor element ID. Value is bool (initialised or not). |
|
8 | 10 | */ |
9 | 11 | var editors = {}; |
10 | 12 |
|
| 13 | + var isInitialised = function (selector) { |
| 14 | + return editors.hasOwnProperty(selector) && editors[selector] === true; |
| 15 | + } |
| 16 | + |
11 | 17 | var initEditor = function(selector, $form, opts) { |
12 | | - if (editors.hasOwnProperty(selector) && editors[selector] === true) { |
| 18 | + if (isInitialised(selector)) { |
13 | 19 | return; |
14 | 20 | } |
15 | 21 |
|
|
77 | 83 | }); |
78 | 84 | }; |
79 | 85 |
|
| 86 | + this.destroyReplyForm = function () { |
| 87 | + if (! isInitialised(reply_selector)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + editors[reply_selector] = false; |
| 92 | + $(reply_selector).editor().remove(); |
| 93 | + }; |
| 94 | + |
80 | 95 | this.initReplyForm = function (opts) { |
81 | 96 | var $form = $('.message-form'); |
82 | 97 | initFileUploads($form, 'replyFileUpload'); |
83 | | - initEditor('#newMessage', $form, opts); |
| 98 | + initEditor(reply_selector, $form, opts); |
84 | 99 | }; |
85 | 100 |
|
86 | 101 | this.initNotesForm = function () { |
|
100 | 115 | }; |
101 | 116 | } |
102 | 117 |
|
103 | | - App.extend('TicketViewForm', Form); |
| 118 | + App.extend('TicketViewForm', new Form); |
104 | 119 |
|
105 | 120 | $(document).ready(function() { |
106 | 121 |
|
107 | 122 | var polling = new PollReplies(30000); |
108 | 123 | polling.start(); |
109 | 124 |
|
110 | 125 | // Initialise reply editor. |
111 | | - var form = new Form(); |
112 | 126 | if ($('.sp-reply-type .sp-action[data-type=0]').hasClass('sp-active')) { |
113 | | - form.initReplyForm(); |
| 127 | + App.TicketViewForm.initReplyForm(); |
114 | 128 | } |
115 | 129 |
|
116 | 130 | // Enable hide/show password toggle |
|
133 | 147 | switch ($(this).data('type')) { |
134 | 148 | case 1: |
135 | 149 | $form = $('.notes-form').toggle(); |
136 | | - form.initNotesForm(); |
| 150 | + App.TicketViewForm.initNotesForm(); |
137 | 151 | break; |
138 | 152 |
|
139 | 153 | case 2: |
|
153 | 167 | }; |
154 | 168 |
|
155 | 169 | $form = $('.forward-form').toggle(); |
156 | | - form.initForwardForm(isFresh ? editor_opts : {}); |
| 170 | + App.TicketViewForm.initForwardForm(isFresh ? editor_opts : {}); |
157 | 171 | if (isFresh) { |
158 | 172 | return; |
159 | 173 | } |
|
162 | 176 |
|
163 | 177 | default: |
164 | 178 | $form = $('.message-form:not(.edit)').toggle(); |
165 | | - form.initReplyForm(); |
| 179 | + App.TicketViewForm.initReplyForm(); |
166 | 180 | } |
167 | 181 |
|
168 | 182 | // If form is now visible, focus in editor and scroll to it. |
|
0 commit comments