Wiki source code of LiveTableEditSheet
Version 1.1 by superadmin on 2018/07/31 17:43
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity output="false"}} | ||
| 2 | #macro(listAvailableColumns) | ||
| 3 | {{html}} | ||
| 4 | <select id="availableColumns"> | ||
| 5 | #set($className = "$!liveTableObj.getValue('class')") | ||
| 6 | #set($classFields = $xwiki.getDocument($className).getxWikiClass().properties) | ||
| 7 | #if($classFields.size() > 0) | ||
| 8 | <optgroup label="$escapetool.xml($services.localization.render('platform.appwithinminutes.liveTableEditorClassFieldColumnGroupLabel'))" id="classFields"> | ||
| 9 | #foreach($field in $classFields) | ||
| 10 | <option value="$field.name">$escapetool.xml($field.translatedPrettyName)</option> | ||
| 11 | #end | ||
| 12 | </optgroup> | ||
| 13 | #end | ||
| 14 | <optgroup label="$escapetool.xml($services.localization.render('platform.appwithinminutes.liveTableEditorGenericColumnGroupLabel'))"> | ||
| 15 | #foreach($entry in $genericColumns.entrySet()) | ||
| 16 | <option value="$entry.key" title="$escapetool.xml($entry.value.get(1))">$escapetool.xml($entry.value.get(0))</option> | ||
| 17 | #end | ||
| 18 | </optgroup> | ||
| 19 | </select> | ||
| 20 | {{/html}} | ||
| 21 | #end | ||
| 22 | |||
| 23 | #macro(displayHelpPanel) | ||
| 24 | #set($genericColumns = { | ||
| 25 | 'doc.title': [$services.localization.render('platform.appwithinminutes.liveTableEditorDocTitleColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorDocTitleColumnDescription')], | ||
| 26 | 'doc.name': [$services.localization.render('platform.appwithinminutes.liveTableEditorDocNameColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorDocNameColumnDescription')], | ||
| 27 | 'doc.location': [$services.localization.render('platform.appwithinminutes.liveTableEditorDocLocationColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorDocLocationColumnDescription')], | ||
| 28 | 'doc.author': [$services.localization.render('platform.appwithinminutes.liveTableEditorDocAuthorColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorDocAuthorColumnDescription')], | ||
| 29 | 'doc.creator': [$services.localization.render('platform.appwithinminutes.liveTableEditorDocCreatorColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorDocCreatorColumnDescription')], | ||
| 30 | 'doc.date': [$services.localization.render('platform.appwithinminutes.liveTableEditorDocDateColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorDocDateColumnDescription')], | ||
| 31 | 'doc.creationDate': [$services.localization.render('platform.appwithinminutes.liveTableEditorDocCreationDateColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorDocCreationDateColumnDescription')], | ||
| 32 | '_actions': [$services.localization.render('platform.appwithinminutes.liveTableEditorActionsColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorActionsColumnDescription')], | ||
| 33 | '_attachments': [$services.localization.render('platform.appwithinminutes.liveTableEditorAttachmentsColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorAttachmentsColumnDescription')], | ||
| 34 | '_images': [$services.localization.render('platform.appwithinminutes.liveTableEditorImagesColumnName'), $services.localization.render('platform.appwithinminutes.liveTableEditorImagesColumnDescription')] | ||
| 35 | }) | ||
| 36 | (% class="wizard-help" %) | ||
| 37 | ((( | ||
| 38 | **$services.localization.render('platform.appwithinminutes.liveTableEditorHelpTitle')** $services.localization.render('platform.appwithinminutes.liveTableEditorHelpDescription') | ||
| 39 | |||
| 40 | #foreach($columnId in ['doc.title', 'doc.location', 'doc.date', 'doc.author', '_actions']) | ||
| 41 | ; {{{$genericColumns.get($columnId).get(0)}}} | ||
| 42 | : {{{$genericColumns.get($columnId).get(1)}}} | ||
| 43 | #end | ||
| 44 | ))) | ||
| 45 | #end | ||
| 46 | |||
| 47 | #macro(displayFormFields) | ||
| 48 | {{html wiki="true"}} | ||
| 49 | (% class="hidden" %) | ||
| 50 | ((( | ||
| 51 | ## Make sure that only the sheet content is rendered when the class is saved using AJAX. | ||
| 52 | <input type="hidden" name="xpage" value="plain" /> | ||
| 53 | ## We prevent the class name and the data space from being edited directly. They can be specified in the URL. | ||
| 54 | $doc.display('class', 'hidden') | ||
| 55 | $doc.display('dataSpace', 'hidden') | ||
| 56 | ## We have to pass the list of available columns to the JavaScript code. | ||
| 57 | #listAvailableColumns() | ||
| 58 | ))) | ||
| 59 | ; <label for="AppWithinMinutes.LiveTableClass_0_description">$doc.displayPrettyName('description', false, false)</label>## | ||
| 60 | (% class="xHint" %)$services.localization.render('platform.appwithinminutes.liveTableEditorDescriptionHint') | ||
| 61 | : $doc.display('description') | ||
| 62 | ; <label for="AppWithinMinutes.LiveTableClass_0_columns">$doc.displayPrettyName('columns', false, false)</label>## | ||
| 63 | (% class="xHint" %)$services.localization.render('platform.appwithinminutes.liveTableEditorColumnsHint') | ||
| 64 | : $doc.display('columns') | ||
| 65 | {{/html}} | ||
| 66 | #end | ||
| 67 | |||
| 68 | #macro(displayIcon) | ||
| 69 | #if($services.uix) | ||
| 70 | #set ($icon = 'icon:application') | ||
| 71 | #set ($templateProviderReference = "$stringtool.removeEnd($className, 'Class')TemplateProvider") | ||
| 72 | #set ($entryIcon = $xwiki.getDocument($templateProviderReference).getValue('icon')) | ||
| 73 | #if ("$!entryIcon" != '') | ||
| 74 | ## Use the entry icon by default, if set. | ||
| 75 | #set ($icon = "icon:$entryIcon") | ||
| 76 | #end | ||
| 77 | #foreach ($uix in $services.uix.getExtensions('org.xwiki.platform.panels.Applications')) | ||
| 78 | #if ($uix.id == "platform.panels.${doc.space}Application" && "$!uix.parameters.icon" != '') | ||
| 79 | #set ($icon = $uix.parameters.icon) | ||
| 80 | #end | ||
| 81 | #end | ||
| 82 | {{html wiki="true"}} | ||
| 83 | ; <label for="applicationIcon">$services.localization.render('platform.appwithinminutes.liveTableEditorIcon')</label> | ||
| 84 | (% class="xHint" %)$services.localization.render('platform.appwithinminutes.liveTableEditorIconHintWithPicker', | ||
| 85 | [$xwiki.getSkinFile('icons/silk/index_abc.png')]) | ||
| 86 | : <input id="applicationIcon" name="applicationIcon" type="text" size="10" value="$!escapetool.xml($icon)" /> | ||
| 87 | {{/html}} | ||
| 88 | |||
| 89 | {{iconPicker id="applicationIcon" prefix="icon:" /}} | ||
| 90 | #end | ||
| 91 | #end | ||
| 92 | |||
| 93 | #macro(doEdit) | ||
| 94 | #set($liveTableObj = $doc.getObject('AppWithinMinutes.LiveTableClass', true)) | ||
| 95 | #set($discard = $doc.use($liveTableObj)) | ||
| 96 | #set($discard = $xwiki.jsfx.use('js/scriptaculous/dragdrop.js')) | ||
| 97 | #set($discard = $xwiki.ssx.use('AppWithinMinutes.LiveTableEditSheet')) | ||
| 98 | #set($discard = $xwiki.jsx.use('AppWithinMinutes.LiveTableEditSheet')) | ||
| 99 | #if("$!request.wizard" == 'true') | ||
| 100 | #appWizardHeader('presentation') | ||
| 101 | |||
| 102 | #end | ||
| 103 | #displayHelpPanel() | ||
| 104 | (% class="form-body" %) | ||
| 105 | ((( | ||
| 106 | #displayFormFields() | ||
| 107 | |||
| 108 | #displayIcon() | ||
| 109 | #if("$!request.wizard" == 'true') | ||
| 110 | |||
| 111 | #appWizardFooter('presentation') | ||
| 112 | #end | ||
| 113 | ))) | ||
| 114 | {{html}}<div class="clearfloats"></div>{{/html}} | ||
| 115 | #end | ||
| 116 | |||
| 117 | #macro (maybeGrantSpaceAdminRight $spaceRef) | ||
| 118 | ## Grant space administration rights only if the space is new. Don't overwrite existing space preferences. | ||
| 119 | #set ($spaceHomeRef = $services.model.resolveDocument('', 'default', $spaceRef)) | ||
| 120 | #set ($spacePrefsRef = $services.model.resolveDocument('WebPreferences', 'explicit', $spaceRef)) | ||
| 121 | #if (!$xwiki.exists($spaceHomeRef) && !$xwiki.exists($spacePrefsRef)) | ||
| 122 | #set ($spacePrefsDoc = $xwiki.getDocument($spacePrefsRef)) | ||
| 123 | #set ($discard = $spacePrefsDoc.use($spacePrefsDoc.getObject('XWiki.XWikiGlobalRights', true))) | ||
| 124 | #set ($discard = $spacePrefsDoc.set('allow', 1)) | ||
| 125 | #set ($discard = $spacePrefsDoc.set('levels', 'admin')) | ||
| 126 | #set ($discard = $spacePrefsDoc.set('users', $services.model.serialize($xcontext.userReference, 'default'))) | ||
| 127 | #set ($discard = $spacePrefsDoc.setTitle('$services.localization.render(''admin.preferences.title'')')) | ||
| 128 | #set ($discard = $spacePrefsDoc.setParent($services.model.serialize($spaceHomeRef, 'default'))) | ||
| 129 | #set ($discard = $spacePrefsDoc.setHidden(true)) | ||
| 130 | #set ($discard = $spacePrefsDoc.saveWithProgrammingRights($services.localization.render( | ||
| 131 | 'platform.appwithinminutes.grantSpaceAdminRightsSaveComment'))) | ||
| 132 | #end | ||
| 133 | #end | ||
| 134 | |||
| 135 | #macro (maybeGrantSpaceAdminRights) | ||
| 136 | ## Application space | ||
| 137 | #set ($appReference = $doc.documentReference.parent) | ||
| 138 | #maybeGrantSpaceAdminRight($appReference) | ||
| 139 | ## Code space (if it's not nested inside the application space) | ||
| 140 | #set ($className = $request.get('AppWithinMinutes.LiveTableClass_0_class')) | ||
| 141 | #set ($classReference = $services.model.resolveDocument($className)) | ||
| 142 | #if (!$classReference.hasParent($appReference)) | ||
| 143 | #maybeGrantSpaceAdminRight($classReference.parent) | ||
| 144 | #end | ||
| 145 | #end | ||
| 146 | |||
| 147 | #macro(updateAndSaveLiveTable) | ||
| 148 | #set($discard = $doc.updateObjectFromRequest('AppWithinMinutes.LiveTableClass')) | ||
| 149 | #set($liveTableGeneratorDoc = $xwiki.getDocument('AppWithinMinutes.LiveTableGenerator')) | ||
| 150 | |||
| 151 | ## Generate the LiveTable by displaying the LiveTableGenerator document in the context of the current document. | ||
| 152 | #set ($displayParameters = $services.display.createDocumentDisplayerParameters()) | ||
| 153 | #set ($discard = $displayParameters.setExecutionContextIsolated(false)) | ||
| 154 | #set ($discard = $displayParameters.setContentTranslated(true)) | ||
| 155 | #set ($generatedLiveTableContent = $services.display.content($liveTableGeneratorDoc, { 'outputSyntaxId' : 'plain/1.0', 'displayerParameters' : $displayParameters })) | ||
| 156 | |||
| 157 | ## Use the generated LiveTable content for the data home page. | ||
| 158 | #set ($dataSpaceReference = $services.model.resolveSpace($doc.getValue('dataSpace'))) | ||
| 159 | #set ($dataSpaceHomeReference = $services.model.resolveDocument('', 'default', $dataSpaceReference)) | ||
| 160 | #if ($dataSpaceHomeReference.equals($doc.documentReference) || !$xwiki.exists($dataSpaceHomeReference)) | ||
| 161 | ## Either the application space and the data space are one and the same or this is a new application and we don't | ||
| 162 | ## want to promote the data space anymore. | ||
| 163 | #set ($dataHomePage = $doc) | ||
| 164 | #else | ||
| 165 | ## The application data is stored in a different space. | ||
| 166 | #set ($dataHomePage = $xwiki.getDocument($dataSpaceHomeReference)) | ||
| 167 | #set ($discard = $dataHomePage.setHidden(true)) | ||
| 168 | #set ($escapedAppName = $doc.documentReference.parent.name.toLowerCase().replace("'", "''")) | ||
| 169 | #set ($discard = $dataHomePage.setTitle("${escapetool.d}services.localization.render('${escapedAppName}.dataSpace.title')")) | ||
| 170 | |||
| 171 | ## Update the home page content. | ||
| 172 | #set ($homePageContent = '') | ||
| 173 | #if ("$!generatedLiveTableContent" != '') | ||
| 174 | ## Include the entries live table in the application home page. | ||
| 175 | #set ($escapedReference = $services.model.serialize($dataSpaceHomeReference).replaceAll('([~"])', '~$1')) | ||
| 176 | #set ($homePageContent = "{{include reference=""$escapedReference"" /}}") | ||
| 177 | #end | ||
| 178 | #set ($discard = $doc.setContent($homePageContent)) | ||
| 179 | #end | ||
| 180 | #set ($discard = $dataHomePage.setContent($generatedLiveTableContent)) | ||
| 181 | |||
| 182 | #set($minorEdit = "$!request.minorEdit" != '') | ||
| 183 | #set($comment = $request.comment) | ||
| 184 | #if("$!comment" == '') | ||
| 185 | #set($comment = $services.localization.render('platform.appwithinminutes.liveTableEditorSaveComment')) | ||
| 186 | #end | ||
| 187 | |||
| 188 | #if ($dataHomePage != $doc) | ||
| 189 | ## Save the data home page. | ||
| 190 | #set ($discard = $dataHomePage.save($comment, $minorEdit)) | ||
| 191 | #end | ||
| 192 | |||
| 193 | ## Save the application home page. | ||
| 194 | #set($discard = $doc.save($comment, $minorEdit)) | ||
| 195 | #end | ||
| 196 | |||
| 197 | #macro(updateAndSaveIcon) | ||
| 198 | #set($uix = $doc.getObject('XWiki.UIExtensionClass', true)) | ||
| 199 | #set($appName = $doc.documentReference.parent.name) | ||
| 200 | #set($discard = $uix.set('name', "platform.panels.${appName}Application")) | ||
| 201 | #set($discard = $uix.set('extensionPointId', 'org.xwiki.platform.panels.Applications')) | ||
| 202 | #set($discard = $uix.set('parameters', "label=${appName}${util.newline}target=${doc.fullName}${util.newline}icon=${request.applicationIcon}")) | ||
| 203 | #set ($hasWikiAdminRights = $services.security.authorization.hasAccess('admin', $doc.documentReference.wikiReference)) | ||
| 204 | #set($discard = $uix.set('scope', "#if ($hasWikiAdminRights)wiki#{else}user#end")) | ||
| 205 | #set($discard = $doc.save('Updated application icon', true)) | ||
| 206 | #end | ||
| 207 | |||
| 208 | #macro(doSave) | ||
| 209 | #maybeGrantSpaceAdminRights() | ||
| 210 | #updateAndSaveLiveTable() | ||
| 211 | #updateAndSaveIcon() | ||
| 212 | #if($action == 'save') | ||
| 213 | #if($errorMessage) | ||
| 214 | {{error}}{{html}}$errorMessage{{/html}}{{/error}} | ||
| 215 | #else | ||
| 216 | ## Redirect to view mode. | ||
| 217 | $response.sendRedirect($doc.getURL()) | ||
| 218 | #end | ||
| 219 | #else | ||
| 220 | #if($errorMessage) | ||
| 221 | $response.sendError(400, $errorMessage) | ||
| 222 | #else | ||
| 223 | $response.setStatus(204) | ||
| 224 | #end | ||
| 225 | #end | ||
| 226 | #end | ||
| 227 | {{/velocity}} | ||
| 228 | |||
| 229 | {{velocity}} | ||
| 230 | #if("$!request.wizard" == 'true') | ||
| 231 | {{include reference="AppWithinMinutes.WizardStep" /}} | ||
| 232 | #end | ||
| 233 | {{/velocity}} | ||
| 234 | |||
| 235 | {{velocity}} | ||
| 236 | ## Determine the action button that triggered the request | ||
| 237 | #set($action = $xcontext.action) | ||
| 238 | #foreach($paramName in $request.getParameterMap().keySet()) | ||
| 239 | #if($paramName.startsWith('xaction_')) | ||
| 240 | #set($action = $paramName.substring(8)) | ||
| 241 | #break | ||
| 242 | #end | ||
| 243 | #end | ||
| 244 | #if($action == 'edit') | ||
| 245 | #doEdit() | ||
| 246 | #elseif($action == 'save' || $action == 'saveandcontinue') | ||
| 247 | #if($services.csrf.isTokenValid($request.form_token)) | ||
| 248 | #doSave() | ||
| 249 | #else | ||
| 250 | $response.sendRedirect($services.csrf.getResubmissionURL()); | ||
| 251 | #end | ||
| 252 | #end | ||
| 253 | {{/velocity}} |