33 if ( typeof define === 'function' && define . amd ) {
44 // Register as an anonymous AMD module:
55 define ( [
6- 'jquery. cloudinary' ,
6+ 'cloudinary' ,
77 'angular'
88 ] , factory ) ;
99 } else {
1010 // Browser globals:
11- factory ( window . jQuery , angular ) ;
11+ factory ( cloudinary , angular ) ;
1212 }
13- } ( function ( $ , angular ) {
13+ } ) ( function ( cloudinary , angular ) {
1414
15- var angularModule = angular . module ( 'cloudinary' , [ ] ) ;
15+ var cloudinaryModule = angular . module ( 'cloudinary' , [ ] ) ;
1616
1717 var cloudinaryAttr = function ( attr ) {
1818 if ( attr . match ( / c l [ A - Z ] / ) ) attr = attr . substring ( 2 ) ;
1919 return attr . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, '$1_$2' ) . toLowerCase ( ) ;
2020 } ;
2121
22+ /**
23+ * Returns an array of attributes for cloudinary.
24+ * @function toCloudinaryAttributes
25+ * @param {Object } source - an object containing attributes
26+ * @param {(RegExp|string) } [filter] - copy only attributes whose name matches the filter
27+ * @return {Object } attributes for cloudinary functions
28+ */
29+ var toCloudinaryAttributes = function ( source , filter ) {
30+ var attributes = { } ;
31+ var isNamedNodeMap = source && ( source . constructor . name === "NamedNodeMap" || source instanceof NamedNodeMap ) ;
32+ angular . forEach ( source , function ( value , name ) {
33+ if ( isNamedNodeMap ) {
34+ name = value . name ;
35+ value = value . value ;
36+ }
37+ if ( ! filter || filter . exec ( name ) ) {
38+ attributes [ cloudinaryAttr ( name ) ] = value ;
39+ }
40+ } ) ;
41+ return attributes ;
42+ } ;
2243
2344 [ 'Src' , 'Srcset' , 'Href' ] . forEach ( function ( attrName ) {
2445 var normalized = 'cl' + attrName ;
2546 attrName = attrName . toLowerCase ( ) ;
26- angularModule . directive ( normalized , [ '$sniffer' , function ( $sniffer ) {
47+ cloudinaryModule . directive ( normalized , [ '$sniffer' , 'cloudinary' , function ( $sniffer , cloudinary ) {
2748 return {
2849 priority : 99 , // it needs to run after the attributes are interpolated
2950 link : function ( scope , element , attr ) {
4162 if ( ! value )
4263 return ;
4364
44- var attributes = { } ;
45- $ . each ( element [ 0 ] . attributes , function ( ) { attributes [ cloudinaryAttr ( this . name ) ] = this . value } ) ;
46- value = $ . cloudinary . url ( value , attributes ) ;
65+ value = cloudinary . url ( value , toCloudinaryAttributes ( element [ 0 ] . attributes ) ) ;
4766 attr . $set ( name , value ) ;
4867
4968 // on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
5776 } ] ) ;
5877 } ) ;
5978
60- angularModule . directive ( 'clTransformation' , [ function ( ) {
79+ cloudinaryModule . directive ( 'clTransformation' , [ function ( ) {
6180 return {
6281 restrict : 'E' ,
6382 transclude : false ,
6483 require : '^clImage' ,
6584 link : function ( scope , element , attrs , clImageCtrl ) {
66- var attributes = { } ;
67- $ . each ( attrs , function ( name , value ) {
68- if ( name [ 0 ] !== '$' ) {
69- attributes [ cloudinaryAttr ( name ) ] = value ;
70- }
71- } ) ;
72- clImageCtrl . addTransformation ( attributes ) ;
85+ clImageCtrl . addTransformation ( toCloudinaryAttributes ( attrs , / ^ [ ^ $ ] / ) ) ;
7386 }
7487 }
7588 } ] ) ;
7689
77- angularModule . directive ( 'clImage' , [ function ( ) {
90+ cloudinaryModule . directive ( 'clImage' , [ 'cloudinary' , function ( cloudinary ) {
7891 var Controller = function ( $scope ) {
7992 this . addTransformation = function ( ts ) {
8093 $scope . transformations = $scope . transformations || [ ] ;
92105 controller : Controller ,
93106 // The linking function will add behavior to the template
94107 link : function ( scope , element , attrs ) {
95- var attributes = { } ;
108+ var attributes = toCloudinaryAttributes ( attrs ) ;
96109 var publicId = null ;
97110
98- $ . each ( attrs , function ( name , value ) { attributes [ cloudinaryAttr ( name ) ] = value } ) ;
99-
100111 if ( scope . transformations ) {
101112 attributes . transformation = scope . transformations ;
102113 }
127138 }
128139
129140 var loadImage = function ( ) {
130- var url = $ . cloudinary . url ( publicId , attributes ) ;
141+ var url = cloudinary . url ( publicId , attributes ) ;
131142 element . attr ( 'src' , url ) ;
132143 }
133144
134145 }
135146 } ;
136147 } ] ) ;
137- } ) ) ;
148+
149+ cloudinaryModule . provider ( 'cloudinary' , function ( ) {
150+ var configuration = new cloudinary . Configuration ( ) ;
151+ this . set = function ( name , value ) {
152+ configuration . set ( name , value ) ;
153+ return this ;
154+ } ;
155+ this . get = function ( name ) {
156+ return configuration . get ( name ) ;
157+ } ;
158+ this . $get = [ function cloudinaryFactory ( ) {
159+ if ( cloudinary . CloudinaryJQuery && jQuery ) {
160+ // cloudinary is attached to the global `jQuery` object
161+ jQuery . cloudinary . config ( configuration . config ( ) ) ;
162+ return jQuery . cloudinary ;
163+ } else {
164+ return new cloudinary . Cloudinary ( configuration . config ( ) ) ;
165+ }
166+ } ]
167+ } ) ;
168+ } ) ;
0 commit comments