|
| 1 | +/* |
| 2 | + * Password Management Servlets (PWM) |
| 3 | + * http://www.pwm-project.org |
| 4 | + * |
| 5 | + * Copyright (c) 2006-2009 Novell, Inc. |
| 6 | + * Copyright (c) 2009-2019 The PWM Project |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +// -------------------------- email table handler ------------------------------------ |
| 22 | + |
| 23 | +var EmailTableHandler = {}; |
| 24 | +EmailTableHandler.defaultValue = { |
| 25 | + to:"@User:Email@", |
| 26 | + from:"@DefaultEmailFromAddress@", |
| 27 | + subject:"Subject", |
| 28 | + bodyPlain:"Body", |
| 29 | + bodyHtml:"Body" |
| 30 | +}; |
| 31 | + |
| 32 | +EmailTableHandler.init = function(keyName) { |
| 33 | + console.log('EmailTableHandler init for ' + keyName); |
| 34 | + PWM_CFGEDIT.readSetting(keyName, function(resultValue) { |
| 35 | + PWM_VAR['clientSettingCache'][keyName] = resultValue; |
| 36 | + EmailTableHandler.draw(keyName); |
| 37 | + }); |
| 38 | +}; |
| 39 | + |
| 40 | +EmailTableHandler.draw = function(settingKey) { |
| 41 | + var resultValue = PWM_VAR['clientSettingCache'][settingKey]; |
| 42 | + var parentDiv = 'table_setting_' + settingKey; |
| 43 | + PWM_CFGEDIT.clearDivElements(parentDiv, true); |
| 44 | + PWM_CFGEDIT.clearDivElements(parentDiv, false); |
| 45 | + |
| 46 | + var htmlBody = ''; |
| 47 | + for (var localeName in resultValue) { |
| 48 | + htmlBody += EmailTableHandler.drawRowHtml(settingKey,localeName) |
| 49 | + } |
| 50 | + var parentDivElement = PWM_MAIN.getObject(parentDiv); |
| 51 | + parentDivElement.innerHTML = htmlBody; |
| 52 | + |
| 53 | + for (var localeName in resultValue) { |
| 54 | + EmailTableHandler.instrumentRow(settingKey,localeName) |
| 55 | + } |
| 56 | + |
| 57 | + if (PWM_MAIN.JSLibrary.isEmpty(resultValue)) { |
| 58 | + var htmlBody = '<button class="btn" id="button-addValue-' + settingKey + '">'; |
| 59 | + htmlBody += '<span class="btn-icon pwm-icon pwm-icon-plus-square"></span>Add Value'; |
| 60 | + htmlBody += '</button>'; |
| 61 | + |
| 62 | + var parentDivElement = PWM_MAIN.getObject(parentDiv); |
| 63 | + parentDivElement.innerHTML = htmlBody; |
| 64 | + |
| 65 | + PWM_MAIN.addEventHandler('button-addValue-' + settingKey,'click',function(){ |
| 66 | + PWM_CFGEDIT.resetSetting(settingKey,function(){PWM_CFGEDIT.loadMainPageBody()}); |
| 67 | + }); |
| 68 | + |
| 69 | + } else { |
| 70 | + var addLocaleFunction = function(localeValue) { |
| 71 | + if (!PWM_VAR['clientSettingCache'][settingKey][localeValue]) { |
| 72 | + PWM_VAR['clientSettingCache'][settingKey][localeValue] = EmailTableHandler.defaultValue; |
| 73 | + EmailTableHandler.writeSetting(settingKey,true); |
| 74 | + } |
| 75 | + }; |
| 76 | + UILibrary.addAddLocaleButtonRow(parentDiv, settingKey, addLocaleFunction, Object.keys(PWM_VAR['clientSettingCache'][settingKey])); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +EmailTableHandler.drawRowHtml = function(settingKey, localeName) { |
| 81 | + var localeLabel = localeName === '' ? 'Default Locale' : PWM_GLOBAL['localeInfo'][localeName] + " (" + localeName + ")"; |
| 82 | + var idPrefix = "setting-" + localeName + "-" + settingKey; |
| 83 | + var htmlBody = ''; |
| 84 | + htmlBody += '<table class="noborder" style=""><tr ><td class="noborder" style="max-width: 440px">'; |
| 85 | + htmlBody += '<table>'; |
| 86 | + if (PWM_MAIN.JSLibrary.itemCount(PWM_VAR['clientSettingCache'][settingKey]) > 1) { |
| 87 | + htmlBody += '<tr><td colspan="5" class="title" style="font-size:100%; font-weight:normal">' + localeLabel + '</td></tr>'; |
| 88 | + } |
| 89 | + var outputFunction = function (labelText, typeText) { |
| 90 | + htmlBody += '<tr><td style="text-align:right; border-width:0;">' + labelText + '</td>'; |
| 91 | + htmlBody += '<td id="button-' + typeText + '-' + idPrefix + '" style="border-width:0; width: 15px"><span class="pwm-icon pwm-icon-edit"/></ta>'; |
| 92 | + htmlBody += '<td style=""><div class="configStringPanel" id="panel-' + typeText + '-' + idPrefix + '"></div></td>'; |
| 93 | + htmlBody += '</tr>'; |
| 94 | + }; |
| 95 | + outputFunction('To', 'to'); |
| 96 | + outputFunction('From', 'from'); |
| 97 | + outputFunction('Subject', 'subject'); |
| 98 | + outputFunction('Plain Body', 'bodyPlain'); |
| 99 | + outputFunction('HTML Body', 'bodyHtml'); |
| 100 | + |
| 101 | + htmlBody += '</table></td><td class="noborder" style="width:20px; vertical-align:top">'; |
| 102 | + if (localeName !== '' || PWM_MAIN.JSLibrary.itemCount(PWM_VAR['clientSettingCache'][settingKey]) < 2) { // add remove locale x |
| 103 | + htmlBody += '<div id="button-deleteRow-' + idPrefix + '" style="vertical-align:top" class="delete-row-icon action-icon pwm-icon pwm-icon-times"></div>'; |
| 104 | + } |
| 105 | + htmlBody += '</td></tr></table><br/>'; |
| 106 | + return htmlBody; |
| 107 | +}; |
| 108 | + |
| 109 | + |
| 110 | +EmailTableHandler.instrumentRow = function(settingKey, localeName) { |
| 111 | + var idPrefix = "setting-" + localeName + "-" + settingKey; |
| 112 | + |
| 113 | + UILibrary.addTextValueToElement('panel-to-' + idPrefix,PWM_VAR['clientSettingCache'][settingKey][localeName]['to']); |
| 114 | + PWM_MAIN.addEventHandler('button-to-' + idPrefix,'click',function(){ EmailTableHandler.editor(settingKey,localeName,false,'to',PWM_CONFIG.showString('Instructions_Edit_Email')); }); |
| 115 | + PWM_MAIN.addEventHandler('panel-to-' + idPrefix,'click',function(){ EmailTableHandler.editor(settingKey,localeName,false,'to',PWM_CONFIG.showString('Instructions_Edit_Email')); }); |
| 116 | + |
| 117 | + UILibrary.addTextValueToElement('panel-from-' + idPrefix,PWM_VAR['clientSettingCache'][settingKey][localeName]['from']); |
| 118 | + PWM_MAIN.addEventHandler('button-from-' + idPrefix,'click',function(){ EmailTableHandler.editor(settingKey,localeName,false,'from'); }); |
| 119 | + PWM_MAIN.addEventHandler('panel-from-' + idPrefix,'click',function(){ EmailTableHandler.editor(settingKey,localeName,false,'from'); }); |
| 120 | + |
| 121 | + UILibrary.addTextValueToElement('panel-subject-' + idPrefix,PWM_VAR['clientSettingCache'][settingKey][localeName]['subject']); |
| 122 | + PWM_MAIN.addEventHandler('button-subject-' + idPrefix,'click',function(){ EmailTableHandler.editor(settingKey,localeName,false,'subject'); }); |
| 123 | + PWM_MAIN.addEventHandler('panel-subject-' + idPrefix,'click',function(){ EmailTableHandler.editor(settingKey,localeName,false,'subject'); }); |
| 124 | + |
| 125 | + UILibrary.addTextValueToElement('panel-bodyPlain-' + idPrefix,PWM_VAR['clientSettingCache'][settingKey][localeName]['bodyPlain']); |
| 126 | + PWM_MAIN.addEventHandler('button-bodyPlain-' + idPrefix,'click',function(){ EmailTableHandler.editor(settingKey,localeName,true,'bodyPlain'); }); |
| 127 | + PWM_MAIN.addEventHandler('panel-bodyPlain-' + idPrefix,'click',function(){ EmailTableHandler.editor(settingKey,localeName,true,'bodyPlain'); }); |
| 128 | + |
| 129 | + UILibrary.addTextValueToElement('panel-bodyHtml-' + idPrefix,PWM_VAR['clientSettingCache'][settingKey][localeName]['bodyHtml']); |
| 130 | + PWM_MAIN.addEventHandler('button-bodyHtml-' + idPrefix,'click',function(){ EmailTableHandler.htmlEditorChoice(settingKey,localeName,'bodyHtml'); }); |
| 131 | + PWM_MAIN.addEventHandler('panel-bodyHtml-' + idPrefix,'click',function(){ EmailTableHandler.htmlEditorChoice(settingKey,localeName,'bodyHtml'); }); |
| 132 | + |
| 133 | + PWM_MAIN.addEventHandler("button-deleteRow-" + idPrefix,"click",function(){ |
| 134 | + PWM_MAIN.showConfirmDialog({okAction:function(){ |
| 135 | + delete PWM_VAR['clientSettingCache'][settingKey][localeName]; |
| 136 | + EmailTableHandler.writeSetting(settingKey,true); |
| 137 | + }}); |
| 138 | + }); |
| 139 | +}; |
| 140 | + |
| 141 | +EmailTableHandler.htmlEditorChoice = function(settingKey,localeName,type) { |
| 142 | + var dialogBody = ''; |
| 143 | + dialogBody += '<div>You can use either the HTML or plaintext editor to modify the HTML email body.</div>'; |
| 144 | + dialogBody += '<div class="buttonbar"><button class="btn" id="btn-editor-plain">Plain</button>'; |
| 145 | + dialogBody += '<button class="btn" id="btn-editor-html">HTML</button></div>'; |
| 146 | + |
| 147 | + var addEventHandlers = function(){ |
| 148 | + PWM_MAIN.addEventHandler('btn-editor-plain','click',function(){ EmailTableHandler.editor(settingKey,localeName,true,type); }); |
| 149 | + PWM_MAIN.addEventHandler('btn-editor-html','click',function(){ EmailTableHandler.htmlBodyEditor(settingKey,localeName); }); |
| 150 | + }; |
| 151 | + |
| 152 | + PWM_MAIN.showDialog({ |
| 153 | + title: "HTML Editor Choice", |
| 154 | + text: dialogBody, |
| 155 | + showClose: true, |
| 156 | + showOk: false, |
| 157 | + loadFunction: addEventHandlers |
| 158 | + }); |
| 159 | +}; |
| 160 | + |
| 161 | + |
| 162 | +EmailTableHandler.editor = function(settingKey, localeName, drawTextArea, type, instructions){ |
| 163 | + var settingData = PWM_SETTINGS['settings'][settingKey]; |
| 164 | + UILibrary.stringEditorDialog({ |
| 165 | + title:'Edit Value - ' + settingData['label'], |
| 166 | + instructions: instructions ? instructions : '', |
| 167 | + textarea:drawTextArea, |
| 168 | + value:PWM_VAR['clientSettingCache'][settingKey][localeName][type], |
| 169 | + completeFunction:function(value){ |
| 170 | + PWM_VAR['clientSettingCache'][settingKey][localeName][type] = value; |
| 171 | + PWM_CFGEDIT.writeSetting(settingKey,PWM_VAR['clientSettingCache'][settingKey],function(){ |
| 172 | + EmailTableHandler.init(settingKey); |
| 173 | + }); |
| 174 | + } |
| 175 | + }); |
| 176 | +}; |
| 177 | + |
| 178 | + |
| 179 | +EmailTableHandler.htmlBodyEditor = function(keyName, localeName) { |
| 180 | + // Grab the scope from the angular controller we created on the div element with ID: centerbody-config |
| 181 | + var $scope = angular.element(document.getElementById("centerbody-config")).scope(); |
| 182 | + var idValue = keyName + "_" + localeName + "_htmlEditor"; |
| 183 | + var toolbarButtons = |
| 184 | + "[" + |
| 185 | + "['h1','h2','h3','h4','h5','h6','p','pre','quote']," + |
| 186 | + "['bold','italics','underline','strikeThrough','ul','ol','undo','redo','clear']," + |
| 187 | + "['justifyLeft','justifyCenter','justifyRight','justifyFull','indent','outdent']," + |
| 188 | + "['html','insertImage','insertLink','insertVideo']" + |
| 189 | + "]"; |
| 190 | + |
| 191 | + PWM_MAIN.showDialog({ |
| 192 | + title: "HTML Editor", |
| 193 | + text: '<div id="' + idValue + '" text-angular ng-model="htmlText" ta-toolbar="' + toolbarButtons + '" class="html-editor"></div>', |
| 194 | + showClose:true, |
| 195 | + showCancel:true, |
| 196 | + dialogClass: 'wide', |
| 197 | + loadFunction: function(){ |
| 198 | + // Put the existing value into the scope, and tell the controller to process the element with ID: idValue |
| 199 | + $scope.htmlText = PWM_VAR['clientSettingCache'][keyName][localeName]['bodyHtml']; |
| 200 | + $scope.$broadcast("content-added", idValue); |
| 201 | + }, |
| 202 | + okAction:function(){ |
| 203 | + PWM_VAR['clientSettingCache'][keyName][localeName]['bodyHtml'] = $scope.htmlText; |
| 204 | + EmailTableHandler.writeSetting(keyName,true); |
| 205 | + } |
| 206 | + }); |
| 207 | +}; |
| 208 | + |
| 209 | + |
| 210 | +EmailTableHandler.writeSetting = function(settingKey, redraw) { |
| 211 | + var currentValues = PWM_VAR['clientSettingCache'][settingKey]; |
| 212 | + PWM_CFGEDIT.writeSetting(settingKey, currentValues, function(){ |
| 213 | + if (redraw) { |
| 214 | + EmailTableHandler.init(settingKey); |
| 215 | + } |
| 216 | + }); |
| 217 | +}; |
0 commit comments