Skip to content

Instantly share code, notes, and snippets.

@bndynet
Last active April 4, 2018 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bndynet/c5b92bd5b2837fba59d9aa2f9756d8d8 to your computer and use it in GitHub Desktop.
Save bndynet/c5b92bd5b2837fba59d9aa2f9756d8d8 to your computer and use it in GitHub Desktop.
Angular Directive - Color Picker

Color Picker Directive for AngularJS

Requires

  • bootstrap

Code

"use strict"
###!
# @param {object} ng-colors - e.x. [{key: 1, value: "#ff0000", description: "", css: "flag"}, ...]
# @param {object} ng-model - type of key
# @param {function} ng-change - fn(color)
# 
# @example
#   <bn-ui-colorpicker ng-colors="[{}, {}, ...]" ng-model="model" ng-change="changeColor(color)"></bn-ui-colorpicker>
###
angular.module "bn.ui.colorpicker", []
    .directive "bnUiColorpicker", 
    ->
        restrict: "E"
        scope: ngColors: "=ngColors", ngModel: "=ngModel", onChange: "&ngChange"
        template: '''
<div class="bn-ui-colorpicker dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
        <span class="empty" style="width: 1em; height: 1em; display: inline-block; vertical-align: text-top; border: solid 1px #dedede;" ng-show="ngModel == null || typeof(ngModel) == 'undefined'"></span>
        <span style="width: 1em; height: 1em; display: inline-block; vertical-align: text-top;" ng-style="{'background-color': color.value}" ng-class="color.css" ng-show="color.key == ngModel" ng-repeat="color in ngColors" title="{{color.description}}"></span>
    </a>
    <ul class="dropdown-menu">
        <li ng-repeat="color in ngColors">
            <a role="button" ng-click="onChange({color: color})">
                <span style="width: 1em; height: 1em; display: inline-block; vertical-align: text-top;" ng-class="color.css" ng-style="{'background-color': color.value}"></span>
                <span ng-bind="color.description"></span>
            </a>
        </li>
    </ul>
</div>
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment