Wiki source code of XWiki Skin Editor
Last modified by Normann P. Nielsen on 2026/11/17 21:00
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | ## |
| 2 | ## Skin Editor Application | ||
| 3 | ## @programming (to access the engine context for storing pseudo-locks) | ||
| 4 | ## | ||
| 5 | ## TODO | ||
| 6 | ## - Relative paths for proto + xwiki.js (or check version) | ||
| 7 | ## | ||
| 8 | ## = Global vars = | ||
| 9 | ## | ||
| 10 | #set($openPRE = "{" + "pre}") ## Trick XEclipse to keep syntax coloration inside pre | ||
| 11 | #set($closePRE = "{/" + "pre}") | ||
| 12 | #set($code = "{" + "code}") | ||
| 13 | #set($sc = $context.context.engineContext.servletContext) | ||
| 14 | #if($request.xpage && $request.xpage=="plain") | ||
| 15 | ## | ||
| 16 | ## = Services = | ||
| 17 | ## | ||
| 18 | #if("$!request.filename" != "") | ||
| 19 | ## | ||
| 20 | ## @service serve skin or SX content. | ||
| 21 | ## Decode the passed filename to retrieve the proper content. (skin file or JSX/SSX) | ||
| 22 | ## | ||
| 23 | #set($filename = $request.filename) | ||
| 24 | #if($filename.startsWith("XWiki.XWikiSkins_")) ## Ah! we have a skin! | ||
| 25 | #set($prefix = "XWiki.XWikiSkins_0_") | ||
| 26 | #set($filename = $filename.substring($prefix.length())) | ||
| 27 | #set($content = $doc.getObject("XWiki.XWikiSkins").getProperty("$filename").value) | ||
| 28 | #else ## Probably a SX | ||
| 29 | #if($filename.startsWith("XWiki.StyleSheetExtension_")) ## SSX | ||
| 30 | #set($filename = $filename.replaceAll(".css$","")) ## Stript the .css, we don't need it here | ||
| 31 | #set($nb = $util.parseInt($filename.substring($mathtool.sub($filename.length(), 1)))) ## Miam :) | ||
| 32 | #set($content = $doc.getObject("XWiki.StyleSheetExtension", $nb).getProperty("code").value) | ||
| 33 | #elseif($filename.startsWith("XWiki.JavaScriptExtension_")) ## JSX | ||
| 34 | #set($filename = $filename.replaceAll(".js$","")) ## Stript the .js, we don't need it here | ||
| 35 | #set($nb = $util.parseInt($filename.substring($mathtool.sub($filename.length(), 1)))) ## Again, Miam :( | ||
| 36 | #set($content = $doc.getObject("XWiki.JavaScriptExtension", $nb).getProperty("code").value) | ||
| 37 | #end ## If not, you won't have content and that's it! | ||
| 38 | #end | ||
| 39 | ## | ||
| 40 | ## give the found content if any as ajax request response. | ||
| 41 | ## | ||
| 42 | #if("$!content" != "") | ||
| 43 | $openPRE | ||
| 44 | $content | ||
| 45 | $closePRE | ||
| 46 | #else ## in case it's empty, so that we don't get template does not exists. | ||
| 47 | Empty file. | ||
| 48 | #end | ||
| 49 | #elseif("$!request.release" != "") | ||
| 50 | ## | ||
| 51 | ## @service release a pseudo-lock (when changing file or leaving the editor). | ||
| 52 | ## | ||
| 53 | #set($docName = $request.release) | ||
| 54 | #set($file = $request.file) | ||
| 55 | #set($locks = $sc.getAttribute("pseudolocks")) | ||
| 56 | #if($locks.containsKey("${context.database}:${docName}_${file}")) | ||
| 57 | $locks.remove("${context.database}:${docName}_${file}") | ||
| 58 | OK | ||
| 59 | #end | ||
| 60 | #elseif("$!request.update" != "") | ||
| 61 | ## | ||
| 62 | ## @service update a pseudo-lock (when saving file). | ||
| 63 | ## | ||
| 64 | #set($docName = $request.update) | ||
| 65 | #set($file = $request.file) | ||
| 66 | #set($locks = $sc.getAttribute("pseudolocks")) | ||
| 67 | #if($locks.containsKey("${context.database}:${docName}_${file}")) | ||
| 68 | #set($data = $util.arrayList) | ||
| 69 | #set($ok = $data.add($mathtool.add($xwiki.date.time, 1800000))) ## 30 minutes | ||
| 70 | #set($ok = $data.add($context.user)) | ||
| 71 | #set($ok = $locks.put("${context.database}:${docName}_${file}", $data)) | ||
| 72 | OK | ||
| 73 | #end | ||
| 74 | #elseif("$!request.service" == "files") | ||
| 75 | ## | ||
| 76 | ## @service | ||
| 77 | ## give the file list for a SX or a Skin document. | ||
| 78 | ## Called by the editor to refresh list of files and locks. | ||
| 79 | ## | ||
| 80 | #set($sDoc = $xwiki.getDocument("$!request.doc")) | ||
| 81 | #if($sDoc.getObject('XWiki.XWikiSkins')) | ||
| 82 | #skinFilesList($sc $sDoc, "$!request.file", false, false) | ||
| 83 | #else | ||
| 84 | #sxFilesList($sc $sDoc, "$!request.file", false, false) | ||
| 85 | #end | ||
| 86 | #elseif($xwiki.hasAccessLevel("edit")) | ||
| 87 | ## | ||
| 88 | ## = Main = (edit mode) | ||
| 89 | ## | ||
| 90 | $openPRE | ||
| 91 | #if("$request.type" != "") ## Available types: sx, skin | ||
| 92 | #set($type=$request.type) | ||
| 93 | #else | ||
| 94 | #set($type="sx") ## default on SX | ||
| 95 | #end | ||
| 96 | #if($type == "skin") | ||
| 97 | ## | ||
| 98 | ## XWiki.XWikiSkins files edition. | ||
| 99 | ## | ||
| 100 | #if("$!request.file" != "") #set($file = $request.file) #else #set($file = "style.css") #end | ||
| 101 | #set($skin = $doc.getObject('XWiki.XWikiSkins')) | ||
| 102 | #set($content = $skin.getProperty("${file}").value) | ||
| 103 | ## Compose pseudo filename that will be passed around in bespin | ||
| 104 | #set($filename = "XWiki.XWikiSkins_0_${file}") | ||
| 105 | #elseif($type == "sx") | ||
| 106 | ## | ||
| 107 | ## XWiki.JavaScriptExtension and XWiki.StyleSheetExtension files edition. | ||
| 108 | ## | ||
| 109 | #if("$!request.ext" != "") #set($ext = $request.ext) #else #set($ext = "js") #end | ||
| 110 | #if($ext == "js") | ||
| 111 | #set($className = "XWiki.JavaScriptExtension") | ||
| 112 | #elseif($ext == "css") | ||
| 113 | #set($className = "XWiki.StyleSheetExtension") | ||
| 114 | #else | ||
| 115 | #error("Invalid SX extension $ext") #set($hasError = 1) | ||
| 116 | #end | ||
| 117 | #if(!$hasError) ## No need to go further | ||
| 118 | #if("$!request.o" != "") #set($number = $util.parseInt($request.o)) #else #set($number = 0) #end | ||
| 119 | #set($extObject = $doc.getObject("$className",$number)) | ||
| 120 | #if($extObject) | ||
| 121 | #set($content = $extObject.getProperty("code").value) | ||
| 122 | #set($filename = "${className}_${number}.${ext}") | ||
| 123 | #set($file = "${number}.${ext}") | ||
| 124 | #else | ||
| 125 | #error("Cound not find object $className number $number") | ||
| 126 | #end | ||
| 127 | #end | ||
| 128 | #else | ||
| 129 | #error("Invalid type: $type") #set($hasError = 1) | ||
| 130 | #end | ||
| 131 | #set($sc = $context.context.engineContext.servletContext) | ||
| 132 | #if($hasError) | ||
| 133 | #error("Error") | ||
| 134 | #else | ||
| 135 | ## | ||
| 136 | ## Check pseudo-locks | ||
| 137 | ## | ||
| 138 | #if(!$sc.getAttribute("pseudolocks")) ## Lazy lock map creation in memory | ||
| 139 | #set($ok = $sc.setAttribute("pseudolocks", $util.hashMap)) | ||
| 140 | #end | ||
| 141 | #set($locks = $sc.getAttribute("pseudolocks")) | ||
| 142 | #if($locks.containsKey("${context.database}:${doc.fullName}_${filename}")) | ||
| 143 | #set($lockData = $locks.get("${context.database}:${doc.fullName}_${filename}")) | ||
| 144 | #set($lockedUntil = $listtool.get($lockData, 0)) | ||
| 145 | #if($lockedUntil < $xwiki.date.time) | ||
| 146 | ## Lock is timed-out, let's remove it | ||
| 147 | #set($ok = $locks.remove("${context.database}:${doc.fullName}_${filename}")) | ||
| 148 | #else | ||
| 149 | #set($locker = $listtool.get($lockData, 1)) | ||
| 150 | #if($locker != "$context.user") ## The locker is different from the context user | ||
| 151 | #if($request.force && $request.force=="1") | ||
| 152 | #set($ok = $locks.remove("${context.database}:${doc.fullName}_${filename}")) ## Forced, let's remove the old lock | ||
| 153 | #else | ||
| 154 | #set($isLocked=1) | ||
| 155 | #end | ||
| 156 | #end | ||
| 157 | #end | ||
| 158 | #end | ||
| 159 | #includeMacros("XWiki.SkinEditorMacros") | ||
| 160 | #if(!$isLocked) ## if it's free, put our own lock here | ||
| 161 | #set($data = $util.arrayList) | ||
| 162 | #set($ok = $data.add($mathtool.add($xwiki.date.time, 1800000))) ## 30 minutes | ||
| 163 | #set($ok = $data.add($context.user)) | ||
| 164 | #set($ok = $locks.put("${context.database}:${doc.fullName}_${filename}", $data)) | ||
| 165 | #end | ||
| 166 | #template("xwikivars.vm") | ||
| 167 | <html> | ||
| 168 | <head> | ||
| 169 | <meta name="document" content="$doc.fullName"/> ## Useful metas for the JS code | ||
| 170 | <meta name="filename" content="$filename"/> | ||
| 171 | <meta name="file" content="$file"/> | ||
| 172 | #template("stylesheets.vm") ## couple of includes. TODO: replace and include just what is necessary | ||
| 173 | ## Bootstrap bespin. | ||
| 174 | #if($xwiki.jsfx) ## POST 1.8 M2 | ||
| 175 | <script type="text/javascript" src="$xwiki.getSkinFile("js/prototype/prototype.js")"></script> | ||
| 176 | <script type="text/javascript" src="$xwiki.getSkinFile("js/xwiki/xwiki.js", true)"></script> | ||
| 177 | #else ## compatibility with older versions | ||
| 178 | <script type="text/javascript" src="${request.contextPath}/prototype.js"></script> | ||
| 179 | <script type="text/javascript" src="$xwiki.getSkinFile("xwiki.js")"></script> | ||
| 180 | #end | ||
| 181 | #if(!$isLocked) | ||
| 182 | <script src=$xwiki.getDocument('XWiki.SkinEditor').getAttachmentURL('bespin.js')></script> | ||
| 183 | #end | ||
| 184 | ## Hook for inserting JavaScript skin extensions | ||
| 185 | <!-- com.xpn.xwiki.plugin.skinx.JsSkinExtensionPlugin --> | ||
| 186 | <!-- com.xpn.xwiki.plugin.skinx.JsSkinFileExtensionPlugin --> | ||
| 187 | #set($ok = $xwiki.ssx.use('XWiki.SkinEditorExtension')) | ||
| 188 | </head> | ||
| 189 | #set($sxParams = $util.hashMap) | ||
| 190 | #if(!$isLocked) | ||
| 191 | #set($void = $xwiki.jsx.use("XWiki.BespinXWikiAdapter")) | ||
| 192 | #set($void = $xwiki.jsx.use("XWiki.SkinEditorExtension")) | ||
| 193 | #end | ||
| 194 | ## | ||
| 195 | ## Some elements bespin code relies they are in the DOM. | ||
| 196 | ## Hide the things we don't want (collab, buttons, etc.) and add our stuff | ||
| 197 | ## | ||
| 198 | <body id=body> | ||
| 199 | <div id="header"> | ||
| 200 | <div id="collab_line" style="display:none"></div> | ||
| 201 | <div id="version" style="display:none"></div> | ||
| 202 | <span id="selected_browsers" style="display:none"></span> | ||
| 203 | <span id="themes"> | ||
| 204 | #set($theme = $xwiki.getUserPreferenceFromCookie("skineditortheme")) | ||
| 205 | #if("$!theme" == "") #set($theme = "coffee") #end | ||
| 206 | <select id="skineditortheme"> | ||
| 207 | <option value="coffee" #if($theme=="coffee")selected#end>Coffee</option> | ||
| 208 | <option value="white"#if($theme=="white")selected#end>White</option> | ||
| 209 | <option value="black"#if($theme=="black")selected#end>Black</option> | ||
| 210 | </select> | ||
| 211 | #set($zebra = $xwiki.getUserPreferenceFromCookie("skineditorzebra")) | ||
| 212 | #if("$!zebra" == "") #set($zebra = "0") #end | ||
| 213 | Zebra <input type="checkbox" id="skineditorzebra" #if($zebra != "0")checked#end/> | ||
| 214 | </span> | ||
| 215 | </div> | ||
| 216 | <div id="subheader" style="right:220px;"> ## Do not remove the style, bespin relies on it. | ||
| 217 | <div id="xwikiextra" class="coffee"> | ||
| 218 | <span class="buttonwrapper"><button id="bespinsave">Save</button></span> | ||
| 219 | <span class="buttonwrapper"><button id="backtoxwiki">Back to the wiki</button></span> | ||
| 220 | <img id="fontsize" title="Font Size" alt="Font Size" src="$xwiki.getDocument('XWiki.SkinEditor').getAttachmentURL('icnfontsize.png')"/> | ||
| 221 | <span id="info" class="infomessage" style="display:none"></span> | ||
| 222 | </div> | ||
| 223 | <div id="status" style="display:none"><canvas id="projectLabel"></canvas></div> | ||
| 224 | </div> | ||
| 225 | #if($isLocked) | ||
| 226 | <div id="lockmessage"> | ||
| 227 | #set($forceLink = "${doc.getURL('view')}?${request.queryString}&force=1") | ||
| 228 | #warning("Locked by $xwiki.getUserName($locker, false). <a href=${forceLink}>Click this link to force edition.</a>") | ||
| 229 | </div> | ||
| 230 | #else | ||
| 231 | <div id="editor" width="1200" height="500"></div> | ||
| 232 | #end | ||
| 233 | <div id="files" class="coffee"> | ||
| 234 | ## Look in user cookie to see wheter or not to hide empty files when firing the editor. | ||
| 235 | #set($hideemptyfiles = $xwiki.getUserPreferenceFromCookie("skineditorhideemptyfiles")) | ||
| 236 | <div id="filesheader">Files <span>hide empty<input type="checkbox" id="skinfileshide" #if(!$hideemptyfiles ||"$!hideemptyfiles"=="1")checked#end/></span></div> | ||
| 237 | <div id="filelist"> | ||
| 238 | #if($doc.getObject('XWiki.XWikiSkins')) | ||
| 239 | #skinFilesList($sc $doc, $file, false, false) | ||
| 240 | #else | ||
| 241 | #sxFilesList($sc $doc, $file, false, false) | ||
| 242 | #end | ||
| 243 | </div> | ||
| 244 | <div id="filesfooter"></div> | ||
| 245 | </div> | ||
| 246 | <div id="footer" style="height:18px;"> | ||
| 247 | <div id="prompt" style="display:none; height:18px;"><img id="promptimg" src="images/icn_command.png" alt=">" ></div> | ||
| 248 | <div id="commandline" style="height:18px;"><input id="command" style="display:none;"></div> | ||
| 249 | <div id="message" style="display:none;"></div> | ||
| 250 | </div> | ||
| 251 | </body> | ||
| 252 | </html> | ||
| 253 | #end | ||
| 254 | #else | ||
| 255 | $response.sendRedirect("$doc.getURL('view','warning=cannotedit')") | ||
| 256 | #end | ||
| 257 | #else | ||
| 258 | ## | ||
| 259 | ## = Main = (view mode) | ||
| 260 | ## | ||
| 261 | #if($xwiki.getDocument("XWiki.SkinEditor").document) ## Check we can execute the programming right. | ||
| 262 | #includeMacros("XWiki.SkinEditorMacros") | ||
| 263 | #set($code = "{" + "code}") ## Trick to expand a velocity variable inside the code block | ||
| 264 | #if("$!request.warning" == "cannotedit") | ||
| 265 | #warning("You are not allowed to edit this document.") | ||
| 266 | #end | ||
| 267 | #if("$!request.info" == "activated") | ||
| 268 | #info("The Skin Editor has been successfully activated on this wiki. Thanks!") | ||
| 269 | #end | ||
| 270 | <div id="header"></div> | ||
| 271 | |||
| 272 | 1.1 $doc.name | ||
| 273 | |||
| 274 | $xwiki.jsx.use("XWiki.SkinEditor") | ||
| 275 | $xwiki.ssx.use("XWiki.SkinEditor") | ||
| 276 | |||
| 277 | <div id="skinfilelist"> | ||
| 278 | <div style="float: left; width: 33%;"> | ||
| 279 | #if($doc.getObject("XWiki.XWikiSkins")) | ||
| 280 | #skinFilesList($sc, $doc, "", true, true) | ||
| 281 | #else | ||
| 282 | #sxFilesList($sc, $doc, "", true, true) | ||
| 283 | #end | ||
| 284 | </div> | ||
| 285 | #if($codeContainerFile && $codeContainerContent) ## Check that not all files are empty | ||
| 286 | <div id="codecontainer" style="float: left; width: 66%;"> | ||
| 287 | <div style="padding-left:20px;"> | ||
| 288 | <div id="codecontainerfile"><tt>$codeContainerFile</tt></div> | ||
| 289 | <div id="codecontainercontent"> | ||
| 290 | $code | ||
| 291 | $codeContainerContent | ||
| 292 | $code | ||
| 293 | </div> | ||
| 294 | </div> | ||
| 295 | </div> | ||
| 296 | #end | ||
| 297 | <div style="clear: both; margin-bottom: 40px;"></div> | ||
| 298 | </div> | ||
| 299 | #else | ||
| 300 | ## | ||
| 301 | ## Skin Editor not saved with programming right. | ||
| 302 | ## | ||
| 303 | #set($documentName = "XWiki.SkinEditor") | ||
| 304 | #set($warningText = "The document *${documentName}* needs to be saved with by a user with Programming Access Level for the Skin Editor to be available.") | ||
| 305 | #if($context.hasAccessLevel("programming", $documentName)) | ||
| 306 | #set($saveURL = $xwiki.getURL("${documentName}",'save',"xredirect=${doc.getURL('view')}?info=activated")) | ||
| 307 | #set($warningText = "${warningText} You ($xwiki.getUserName($context.user, false)) have the programming Access Level. <a href=${saveURL}>Click this link to save $documentName</a> and activate the Skin Editor.") | ||
| 308 | #else | ||
| 309 | #set($warningText = "${warningText} You ($xwiki.getUserName($context.user, false)) do not have the Programming Access Level. Contact your administrator to activate the Skin Editor on this wiki.") | ||
| 310 | #end | ||
| 311 | #warning("${warningText}") | ||
| 312 | #end | ||
| 313 | #end |