11'use strict' ;
22
3- angular . module ( 'angularify.semantic.checkbox' , [ ] ) ;
3+ angular . module ( 'angularify.semantic.checkbox' , [ ] )
4+
5+ . directive ( 'checkbox' , function ( ) {
6+ return {
7+ restrict : 'E' ,
8+ replace : true ,
9+ transclude : true ,
10+ require :"ngModel" ,
11+ scope :{
12+ type : "@" ,
13+ size : "@" ,
14+ checked : "@" ,
15+ model : '=ngModel'
16+ } ,
17+ template : "<div class=\"{{checkbox_class}}\">" +
18+ "<input type=\"checkbox\">" +
19+ "<label ng-click=\"click_on_checkbox()\" ng-transclude></label>" +
20+ "</div>" ,
21+ link : function ( scope , element , attrs , ngModel ) {
22+
23+ //
24+ // set up checkbox class and type
25+ //
26+ if ( scope . type == 'standard' || scope . type == undefined ) {
27+ scope . type = 'standard' ;
28+ scope . checkbox_class = 'ui checkbox' ;
29+ } else if ( scope . type == 'slider' ) {
30+ scope . type = 'slider' ;
31+ scope . checkbox_class = 'ui slider checkbox' ;
32+ } else if ( scope . type == 'toggle' ) {
33+ scope . type = 'toggle' ;
34+ scope . checkbox_class = 'ui toggle checkbox' ;
35+ } else {
36+ scope . type = 'standard' ;
37+ scope . checkbox_class = 'ui checkbox' ;
38+ }
39+
40+ //
41+ // set checkbox size
42+ //
43+ if ( scope . size == 'large' ) {
44+ scope . checkbox_class = scope . checkbox_class + ' large' ;
45+ } else if ( scope . size == 'huge' ) {
46+ scope . checkbox_class = scope . checkbox_class + ' huge' ;
47+ }
48+
49+ //
50+ // set checked/unchecked
51+ //
52+ if ( scope . checked == 'false' || scope . checked == undefined ) {
53+ scope . checked = false ;
54+ } else {
55+ scope . checked = true ;
56+ element . children ( ) [ 0 ] . setAttribute ( 'checked' , '' ) ;
57+ }
58+
59+ //
60+ // Click handler
61+ //
62+ element . bind ( 'click' , function ( ) {
63+ if ( scope . checked == true ) {
64+ scope . checked = true ;
65+ scope . model = false ;
66+ element . children ( ) [ 0 ] . removeAttribute ( 'checked' ) ;
67+ } else {
68+ scope . checked = true ;
69+ scope . model = true ;
70+ element . children ( ) [ 0 ] . setAttribute ( 'checked' , 'true' ) ;
71+ }
72+ } ) ;
73+
74+ //
75+ // Watch for ng-model
76+ //
77+ scope . $watch ( 'model' , function ( val ) {
78+ if ( val == undefined )
79+ return ;
80+
81+ if ( val == true ) {
82+ scope . checked = true ;
83+ element . children ( ) [ 0 ] . setAttribute ( 'checked' , 'true' ) ;
84+ } else {
85+ scope . checked = false ;
86+ element . children ( ) [ 0 ] . removeAttribute ( 'checked' ) ;
87+ }
88+ } ) ;
89+ }
90+ }
91+ } ) ;
0 commit comments