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; 024 025import org.ametys.plugins.core.ui.resources.css.sass.AbstractAmetysSASSFunctionsProvider; 026import org.ametys.web.WebHelper; 027import org.ametys.web.repository.site.Site; 028import org.ametys.web.repository.site.SiteManager; 029 030import io.bit3.jsass.annotation.Name; 031import io.bit3.jsass.type.SassString; 032 033/** 034 * This helper provides function that will be directly injected into Sass file compilation. 035 * 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 036 */ 037public class AmetysSassFunctionsProvider extends AbstractAmetysSASSFunctionsProvider implements Serviceable 038{ 039 private SiteManager _siteManager; 040 041 public void service(ServiceManager manager) throws ServiceException 042 { 043 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 044 } 045 046 /** 047 * Resolve url path inside the current skin 048 * @param path the path relative to the current skin 049 * @return the absolute path 050 */ 051 public SassString skinUrl(@Name("path") String path) 052 { 053 Request request = ContextHelper.getRequest(_context); 054 String skinName = (String) request.getAttribute("skin"); 055 056 if (StringUtils.isBlank(skinName)) 057 { 058 String siteName = WebHelper.getSiteName(request); 059 Site site = _siteManager.getSite(siteName); 060 skinName = site.getSkinId(); 061 } 062 063 StringBuilder sb = new StringBuilder(request.getContextPath()); 064 sb.append("/skins/") 065 .append(skinName) 066 .append("/resources") 067 .append(path.startsWith("/") ? path : "/" + path); 068 069 SassString url = new SassString("url(\"" + sb.toString() + "\")", false); 070 return url; 071 } 072}