github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/server/camlistored/ui/style.js (about) 1 /* 2 Copyright 2013 The Camlistore Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 goog.provide('cam.style'); 18 goog.provide('cam.style.ClassNameBuilder'); 19 20 goog.require('goog.math.Coordinate'); 21 goog.require('goog.string'); 22 goog.require('goog.style'); 23 24 // Returns |url| wrapped in url() so that it can be used as a CSS property value. 25 // @param {string} url 26 // @returns {string} 27 cam.style.getURLValue = function(url) { 28 return goog.string.subs('url(%s)', url); 29 }; 30 31 // Sets a style property to a URL value. 32 // @param {Element} elm 33 // @param {string} dashedCSSProperty The CSS property to set, formatted with dashes, in the CSS style, not camelCase. 34 // @param {string} url 35 cam.style.setURLStyle = function(elm, dashedCSSProperty, url) { 36 goog.style.setStyle(elm, dashedCSSProperty, cam.style.getURLValue(url)); 37 }; 38 39 // @param {Element} elm 40 // @param {goog.math.Coordinate} origin 41 // @param {string=} opt_unit The CSS units the origin is in. If unspecified, defaults to pixels. 42 cam.style.setTransformOrigin = function(elm, origin, opt_unit) { 43 var unit = opt_unit || 'px'; 44 goog.style.setStyle(elm, 'transform-origin', goog.string.subs('%s%s %s%s', origin.x, unit, origin.y, unit)); 45 }; 46 47 // Note that this currently clears any previous CSS transform. Currently we only 48 // needs to support rotate(). 49 // @param {Element} elm 50 // @param {number} degrees 51 cam.style.setRotation = function(elm, degrees) { 52 goog.style.setStyle(elm, 'transform', goog.string.subs('rotate(%sdeg)', degrees)); 53 }; 54 55 56 // Utility to build a space-separated className property. 57 cam.style.ClassNameBuilder = function() { 58 this.names_ = {}; 59 }; 60 61 // Maybe add the specified class. 62 // @param {?string} name Class to add. If falsey, not added. 63 // @param {boolean=} yes Whether to add. If unspecified or falsey, not added. 64 // @return {cam.style.ClassNameBuilder} 65 cam.style.ClassNameBuilder.prototype.add = function(name, yes) { 66 if (!name) { 67 return this; 68 } 69 70 if (!goog.isDef(yes)) { 71 yes = true; 72 } 73 74 if (yes) { 75 this.names_[name] = true; 76 } else { 77 delete this.names_[name]; 78 } 79 80 return this; 81 }; 82 83 // Return the space-separated className. 84 // @return {string} 85 cam.style.ClassNameBuilder.prototype.build = function() { 86 return goog.object.getKeys(this.names_).join(' '); 87 };