|
1 | 1 | /* |
2 | | - * Cloudinary's JavaScript library - Version 2.0.0 |
| 2 | + * Cloudinary's JavaScript library - Version 2.0.1 |
3 | 3 | * Copyright Cloudinary |
4 | 4 | * see https://github.com/cloudinary/cloudinary_js |
5 | 5 | */ |
|
2213 | 2213 | */ |
2214 | 2214 | var Cloudinary; |
2215 | 2215 | return Cloudinary = (function() { |
2216 | | - var AKAMAI_SHARED_CDN, CF_SHARED_CDN, DEFAULT_POSTER_OPTIONS, DEFAULT_VIDEO_SOURCE_TYPES, OLD_AKAMAI_SHARED_CDN, SHARED_CDN, VERSION, absolutize, cdnSubdomainNumber, closestAbove, cloudinaryUrlPrefix, defaultBreakpoints, devicePixelRatioCache, finalizeResourceType, responsiveConfig, responsiveResizeInitialized; |
| 2216 | + var AKAMAI_SHARED_CDN, CF_SHARED_CDN, DEFAULT_POSTER_OPTIONS, DEFAULT_VIDEO_SOURCE_TYPES, OLD_AKAMAI_SHARED_CDN, SHARED_CDN, VERSION, absolutize, applyBreakpoints, cdnSubdomainNumber, closestAbove, cloudinaryUrlPrefix, defaultBreakpoints, finalizeResourceType, parentWidth; |
2217 | 2217 |
|
2218 | | - VERSION = "2.0.0"; |
| 2218 | + VERSION = "2.0.1"; |
2219 | 2219 |
|
2220 | 2220 | CF_SHARED_CDN = "d3jpl91pxevbkh.cloudfront.net"; |
2221 | 2221 |
|
|
2232 | 2232 |
|
2233 | 2233 | DEFAULT_VIDEO_SOURCE_TYPES = ['webm', 'mp4', 'ogv']; |
2234 | 2234 |
|
2235 | | - devicePixelRatioCache = {}; |
2236 | | - |
2237 | | - responsiveConfig = {}; |
2238 | | - |
2239 | | - responsiveResizeInitialized = false; |
2240 | | - |
2241 | 2235 |
|
2242 | 2236 | /** |
2243 | 2237 | * @const {Object} Cloudinary.DEFAULT_IMAGE_PARAMS |
|
2281 | 2275 |
|
2282 | 2276 | function Cloudinary(options) { |
2283 | 2277 | var configuration; |
| 2278 | + this.devicePixelRatioCache = {}; |
| 2279 | + this.responsiveConfig = {}; |
| 2280 | + this.responsiveResizeInitialized = false; |
2284 | 2281 | configuration = new cloudinary.Configuration(options); |
2285 | 2282 | this.config = function(newConfig, newValue) { |
2286 | 2283 | return configuration.config(newConfig, newValue); |
|
2680 | 2677 |
|
2681 | 2678 | Cloudinary.prototype.responsive = function(options) { |
2682 | 2679 | var ref, ref1, responsiveResize, timeout; |
2683 | | - responsiveConfig = Util.merge(responsiveConfig || {}, options); |
2684 | | - this.cloudinary_update('img.cld-responsive, img.cld-hidpi', responsiveConfig); |
2685 | | - responsiveResize = (ref = (ref1 = responsiveConfig['responsive_resize']) != null ? ref1 : this.config('responsive_resize')) != null ? ref : true; |
2686 | | - if (responsiveResize && !responsiveResizeInitialized) { |
2687 | | - responsiveConfig.resizing = responsiveResizeInitialized = true; |
| 2680 | + this.responsiveConfig = Util.merge(this.responsiveConfig || {}, options); |
| 2681 | + this.cloudinary_update('img.cld-responsive, img.cld-hidpi', this.responsiveConfig); |
| 2682 | + responsiveResize = (ref = (ref1 = this.responsiveConfig['responsive_resize']) != null ? ref1 : this.config('responsive_resize')) != null ? ref : true; |
| 2683 | + if (responsiveResize && !this.responsiveResizeInitialized) { |
| 2684 | + this.responsiveConfig.resizing = this.responsiveResizeInitialized = true; |
2688 | 2685 | timeout = null; |
2689 | 2686 | return window.addEventListener('resize', (function(_this) { |
2690 | 2687 | return function() { |
2691 | 2688 | var debounce, ref2, ref3, reset, run, wait; |
2692 | | - debounce = (ref2 = (ref3 = responsiveConfig['responsive_debounce']) != null ? ref3 : _this.config('responsive_debounce')) != null ? ref2 : 100; |
| 2689 | + debounce = (ref2 = (ref3 = _this.responsiveConfig['responsive_debounce']) != null ? ref3 : _this.config('responsive_debounce')) != null ? ref2 : 100; |
2693 | 2690 | reset = function() { |
2694 | 2691 | if (timeout) { |
2695 | 2692 | clearTimeout(timeout); |
2696 | 2693 | return timeout = null; |
2697 | 2694 | } |
2698 | 2695 | }; |
2699 | 2696 | run = function() { |
2700 | | - return _this.cloudinary_update('img.cld-responsive', responsiveConfig); |
| 2697 | + return _this.cloudinary_update('img.cld-responsive', _this.responsiveConfig); |
2701 | 2698 | }; |
2702 | 2699 | wait = function() { |
2703 | 2700 | reset(); |
|
2765 | 2762 | Cloudinary.prototype.device_pixel_ratio = function() { |
2766 | 2763 | var dpr, dprString, dprUsed; |
2767 | 2764 | dpr = (typeof window !== "undefined" && window !== null ? window.devicePixelRatio : void 0) || 1; |
2768 | | - dprString = devicePixelRatioCache[dpr]; |
| 2765 | + dprString = this.devicePixelRatioCache[dpr]; |
2769 | 2766 | if (!dprString) { |
2770 | 2767 | dprUsed = closestAbove(this.supported_dpr_values, dpr); |
2771 | 2768 | dprString = dprUsed.toString(); |
2772 | 2769 | if (dprString.match(/^\d+$/)) { |
2773 | 2770 | dprString += '.0'; |
2774 | 2771 | } |
2775 | | - devicePixelRatioCache[dpr] = dprString; |
| 2772 | + this.devicePixelRatioCache[dpr] = dprString; |
2776 | 2773 | } |
2777 | 2774 | return dprString; |
2778 | 2775 | }; |
|
2877 | 2874 | return this; |
2878 | 2875 | }; |
2879 | 2876 |
|
| 2877 | + applyBreakpoints = function(tag, width, options) { |
| 2878 | + var ref, ref1, ref2, ref3, responsive_use_breakpoints; |
| 2879 | + responsive_use_breakpoints = (ref = (ref1 = (ref2 = (ref3 = options['responsive_use_breakpoints']) != null ? ref3 : options['responsive_use_stoppoints']) != null ? ref2 : this.config('responsive_use_breakpoints')) != null ? ref1 : this.config('responsive_use_stoppoints')) != null ? ref : 'resize'; |
| 2880 | + if ((!responsive_use_breakpoints) || (responsive_use_breakpoints === 'resize' && !options.resizing)) { |
| 2881 | + return width; |
| 2882 | + } else { |
| 2883 | + return this.calc_breakpoint(tag, width); |
| 2884 | + } |
| 2885 | + }; |
| 2886 | + |
| 2887 | + parentWidth = function(element) { |
| 2888 | + var containerWidth; |
| 2889 | + containerWidth = 0; |
| 2890 | + while (((element = element != null ? element.parentNode : void 0) instanceof Element) && !containerWidth) { |
| 2891 | + containerWidth = Util.width(element); |
| 2892 | + } |
| 2893 | + return containerWidth; |
| 2894 | + }; |
| 2895 | + |
2880 | 2896 |
|
2881 | 2897 | /** |
2882 | 2898 | * Update hidpi (dpr_auto) and responsive (w_auto) fields according to the current container size and the device pixel ratio. |
|
2894 | 2910 | */ |
2895 | 2911 |
|
2896 | 2912 | Cloudinary.prototype.cloudinary_update = function(elements, options) { |
2897 | | - var attrs, container, containerWidth, currentWidth, exact, j, len, ref, ref1, ref2, ref3, ref4, requestedWidth, responsive, responsive_use_breakpoints, src, tag; |
| 2913 | + var containerWidth, imageWidth, j, len, ref, requestedWidth, setUrl, src, tag; |
2898 | 2914 | if (options == null) { |
2899 | 2915 | options = {}; |
2900 | 2916 | } |
|
2910 | 2926 | return [elements]; |
2911 | 2927 | } |
2912 | 2928 | })(); |
2913 | | - responsive_use_breakpoints = (ref = (ref1 = (ref2 = (ref3 = options['responsive_use_breakpoints']) != null ? ref3 : options['responsive_use_stoppoints']) != null ? ref2 : this.config('responsive_use_breakpoints')) != null ? ref1 : this.config('responsive_use_stoppoints')) != null ? ref : 'resize'; |
2914 | | - exact = !responsive_use_breakpoints || responsive_use_breakpoints === 'resize' && !options.resizing; |
2915 | 2929 | for (j = 0, len = elements.length; j < len; j++) { |
2916 | 2930 | tag = elements[j]; |
2917 | | - if (!((ref4 = tag.tagName) != null ? ref4.match(/img/i) : void 0)) { |
| 2931 | + if (!((ref = tag.tagName) != null ? ref.match(/img/i) : void 0)) { |
2918 | 2932 | continue; |
2919 | 2933 | } |
| 2934 | + setUrl = true; |
2920 | 2935 | if (options.responsive) { |
2921 | 2936 | Util.addClass(tag, "cld-responsive"); |
2922 | 2937 | } |
2923 | | - attrs = {}; |
2924 | 2938 | src = Util.getData(tag, 'src-cache') || Util.getData(tag, 'src'); |
2925 | | - if (!src) { |
2926 | | - return; |
2927 | | - } |
2928 | | - responsive = Util.hasClass(tag, 'cld-responsive') && src.match(/\bw_auto\b/); |
2929 | | - if (responsive) { |
2930 | | - container = tag.parentNode; |
2931 | | - containerWidth = 0; |
2932 | | - while (container && containerWidth === 0) { |
2933 | | - containerWidth = Util.width(container); |
2934 | | - container = container.parentNode; |
2935 | | - } |
2936 | | - if (containerWidth === 0) { |
2937 | | - return; |
2938 | | - } |
2939 | | - requestedWidth = exact ? containerWidth : this.calc_breakpoint(tag, containerWidth); |
2940 | | - currentWidth = Util.getData(tag, 'width') || 0; |
2941 | | - if (requestedWidth > currentWidth) { |
2942 | | - Util.setData(tag, 'width', requestedWidth); |
2943 | | - } else { |
2944 | | - requestedWidth = currentWidth; |
| 2939 | + if (!Util.isEmpty(src)) { |
| 2940 | + src = src.replace(/\bdpr_(1\.0|auto)\b/g, 'dpr_' + this.device_pixel_ratio()); |
| 2941 | + if (Util.hasClass(tag, 'cld-responsive') && /\bw_auto\b/.exec(src)) { |
| 2942 | + containerWidth = parentWidth(tag); |
| 2943 | + if (containerWidth !== 0) { |
| 2944 | + requestedWidth = applyBreakpoints.call(this, tag, containerWidth, options); |
| 2945 | + imageWidth = Util.getData(tag, 'width') || 0; |
| 2946 | + if (requestedWidth > imageWidth) { |
| 2947 | + imageWidth = requestedWidth; |
| 2948 | + } |
| 2949 | + Util.setData(tag, 'width', requestedWidth); |
| 2950 | + src = src.replace(/\bw_auto\b/g, 'w_' + imageWidth); |
| 2951 | + Util.setAttribute(tag, 'width', null); |
| 2952 | + if (!options.responsive_preserve_height) { |
| 2953 | + Util.setAttribute(tag, 'height', null); |
| 2954 | + } |
| 2955 | + } else { |
| 2956 | + setUrl = false; |
| 2957 | + } |
2945 | 2958 | } |
2946 | | - src = src.replace(/\bw_auto\b/g, 'w_' + requestedWidth); |
2947 | | - attrs.width = null; |
2948 | | - if (!options.responsive_preserve_height) { |
2949 | | - attrs.height = null; |
| 2959 | + if (setUrl) { |
| 2960 | + Util.setAttribute(tag, 'src', src); |
2950 | 2961 | } |
2951 | 2962 | } |
2952 | | - attrs.src = src.replace(/\bdpr_(1\.0|auto)\b/g, 'dpr_' + this.device_pixel_ratio()); |
2953 | | - Util.setAttributes(tag, attrs); |
2954 | 2963 | } |
2955 | 2964 | return this; |
2956 | 2965 | }; |
|
0 commit comments