// /** // * Copyright (C) 2013 Crossing Hippos - Babs Gösgens. All rights reserved. // * Licensed under GNU General Public License version 2 or later !default; see LICENSE.txt // * // * Uses Hugo Giraudel's awesome Lists functions: https://github.com/HugoGiraudel // * // */ @if $trace-imports { @debug 'imported'; } // Dependencies @import "pg/helpers/library/lists"; // Returns a new element // ========================================================================== // @example $button: new('button', 'default'); // @example $button: new($button, 'active'); // -------------------------------------------------------------------------- // @param $properties [Literal] an arbitrary string, or an existing $element // @param $name [String] a namespace for the new element // -------------------------------------------------------------------------- // @return [List] @function new($properties, $name, $is-state) { @if type-of($properties)!=list { @return ($name, (), $is-state); } @else if type-of($properties)==list and length($properties)==1 { @return ($name, $properties, $is-state); } @else { $old_name: nth($properties, 1); @return replace($properties, $old_name, $name); } } // Placeholder function for new() // ========================================================================== // @example $button: clone($button, 'new-name'); // // @param $element [List] an existing $element // @param $name [String] a namespace for the new element // -------------------------------------------------------------------------- // @return [List] @function clone($element, $element-name, $is-state) { @return new($element, $element-name, $is-state); } // Returns the $element with adjusted properties // ========================================================================== // @example $button: change($button, (color, #fff)); // // @param $element [List] an existing $element // @param $property [List] a list consisting of property-name and value // -------------------------------------------------------------------------- // @return [List] @function change($element, $property) { $key: nth($property, 1); $properties: nth($element,2); $new_properties: (); $item: find-list-item($properties, $key); @if $item==false { $new_properties: append($properties, ($property)); } @else { $new_properties: replace($properties, $item, $property); } $element: replace($element, $properties, $new_properties); @return $element; } // Returns a matching element from a nested list // ========================================================================== // @example $button: find-list-item( ((color, blue) (border: none)), border ) => (border: none) // @example $button: find-list-item( ((color, blue) (border: none)), border, true ) => 2 // // @param $list [List] a nested list // @param $key [String] the key to match // @param $index [Boolean] return the match's index (true) or the match (false) // -------------------------------------------------------------------------- // @return [Literal] @function find-list-item($list, $key, $index: false) { $l: length($list); @for $i from 1 through $l { $item: nth($list,$i); @if nth($item,1) == $key { @return $item; } } @return false; }