001/* 002 * Copyright 2017 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.resources.css.sass; 017 018import org.apache.avalon.framework.service.ServiceException; 019import org.apache.avalon.framework.service.ServiceManager; 020import org.apache.avalon.framework.service.Serviceable; 021import org.apache.cocoon.components.ContextHelper; 022import org.apache.cocoon.environment.Request; 023import org.apache.commons.lang3.StringUtils; 024import org.slf4j.Logger; 025 026import org.ametys.plugins.core.ui.resources.css.sass.AbstractAmetysSASSFunctionsProvider; 027import org.ametys.runtime.plugin.component.LogEnabled; 028import org.ametys.web.WebHelper; 029import org.ametys.web.repository.site.Site; 030import org.ametys.web.repository.site.SiteManager; 031 032import io.bit3.jsass.annotation.Name; 033import io.bit3.jsass.type.SassString; 034 035/** 036 * This helper provides function that will be directly injected into Sass file compilation. 037 * Inherited methods are NOT injected. For this reason, method that must not be accessed from Sass have to be declared in the parent class AbstractAmetysSASSHelper 038 */ 039public class AmetysSassFunctionsProvider extends AbstractAmetysSASSFunctionsProvider implements Serviceable, LogEnabled 040{ 041 private SiteManager _siteManager; 042 private Logger _logger; 043 044 public void service(ServiceManager manager) throws ServiceException 045 { 046 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 047 } 048 049 public void setLogger(Logger logger) 050 { 051 _logger = logger; 052 } 053 054 /** 055 * Resolve url path inside the current skin 056 * @param path the path relative to the current skin 057 * @return the absolute path 058 */ 059 public SassString skinUrl(@Name("path") String path) 060 { 061 Request request = ContextHelper.getRequest(_context); 062 String skinName = (String) request.getAttribute("skin"); 063 064 if (StringUtils.isBlank(skinName)) 065 { 066 String siteName = WebHelper.getSiteName(request); 067 Site site = _siteManager.getSite(siteName); 068 skinName = site.getSkinId(); 069 } 070 071 StringBuilder sb = new StringBuilder(request.getContextPath()); 072 sb.append("/skins/") 073 .append(skinName) 074 .append("/resources") 075 .append(path.startsWith("/") ? path : "/" + path); 076 077 SassString url = new SassString("url(\"" + sb.toString() + "\")", false); 078 return url; 079 } 080}