UNPKG

10.6 kBJavaScriptView Raw
1"use strict";
2
3function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
4
5var _ = require('lodash');
6exports.config = require("./config");
7exports.utils = require("./utils");
8exports.uploader = require("./uploader");
9exports.api = require("./api");
10exports.PreloadedFile = require("./preloaded_file");
11exports.Cache = require('./cache');
12
13var cloudinary = module.exports;
14
15var optionConsume = cloudinary.utils.option_consume;
16
17exports.url = function url(public_id, options) {
18 options = _.extend({}, options);
19 return cloudinary.utils.url(public_id, options);
20};
21
22var _require = require('./utils/srcsetUtils'),
23 generateImageResponsiveAttributes = _require.generateImageResponsiveAttributes,
24 generateMediaAttr = _require.generateMediaAttr;
25
26/**
27 * Helper function, allows chaining transformation to the end of transformation list
28 *
29 * @private
30 * @param {object} options Original options
31 * @param {object|object[]} transformation Transformations to chain at the end
32 *
33 * @return {object} Resulting options
34 */
35
36
37function chainTransformations(options) {
38 var transformation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
39
40 // preserve url options
41 var urlOptions = cloudinary.utils.extractUrlParams(options);
42 var currentTransformation = cloudinary.utils.extractTransformationParams(options);
43 transformation = cloudinary.utils.build_array(transformation);
44 urlOptions.transformation = [currentTransformation].concat(_toConsumableArray(transformation));
45 return urlOptions;
46}
47
48/**
49 * Generate an HTML img tag with a Cloudinary URL
50 * @param {string} source A Public ID or a URL
51 * @param {object} options Configuration options
52 * @param {srcset} options.srcset srcset options
53 * @param {object} options.attributes HTML attributes
54 * @param {number} options.html_width (deprecated) The HTML tag width
55 * @param {number} options.html_height (deprecated) The HTML tag height
56 * @param {boolean} options.client_hints Don't implement the client side responsive function.
57 * This argument can override the the same option in the global configuration.
58 * @param {boolean} options.responsive Setup the tag for the client side responsive function.
59 * @param {boolean} options.hidpi Setup the tag for the client side auto dpr function.
60 * @param {boolean} options.responsive_placeholder A place holder image URL to use with.
61 * the client side responsive function
62 * @return {string} An HTML img tag
63 */
64exports.image = function image(source, options) {
65 var localOptions = _.extend({}, options);
66 var srcsetParam = optionConsume(localOptions, 'srcset');
67 var attributes = optionConsume(localOptions, 'attributes', {});
68 var src = cloudinary.utils.url(source, localOptions);
69 if ("html_width" in localOptions) localOptions.width = optionConsume(localOptions, "html_width");
70 if ("html_height" in localOptions) localOptions.height = optionConsume(localOptions, "html_height");
71
72 var client_hints = optionConsume(localOptions, "client_hints", cloudinary.config().client_hints);
73 var responsive = optionConsume(localOptions, "responsive");
74 var hidpi = optionConsume(localOptions, "hidpi");
75
76 if ((responsive || hidpi) && !client_hints) {
77 localOptions["data-src"] = src;
78 var classes = [responsive ? "cld-responsive" : "cld-hidpi"];
79 var current_class = optionConsume(localOptions, "class");
80 if (current_class) classes.push(current_class);
81 localOptions.class = classes.join(" ");
82 src = optionConsume(localOptions, "responsive_placeholder", cloudinary.config().responsive_placeholder);
83 if (src === "blank") {
84 src = cloudinary.BLANK;
85 }
86 }
87 var html = "<img ";
88 if (src) html += "src='" + src + "' ";
89 var responsiveAttributes = {};
90 if (cloudinary.utils.isString(srcsetParam)) {
91 responsiveAttributes.srcset = srcsetParam;
92 } else {
93 responsiveAttributes = generateImageResponsiveAttributes(source, attributes, srcsetParam, options);
94 }
95 if (!cloudinary.utils.isEmpty(responsiveAttributes)) {
96 delete localOptions.width;
97 delete localOptions.height;
98 }
99 html += cloudinary.utils.html_attrs(_.extend(localOptions, responsiveAttributes, attributes)) + "/>";
100 return html;
101};
102
103/**
104 * Creates an HTML video tag for the provided public_id
105 * @param {String} public_id the resource public ID
106 * @param {Object} [options] options for the resource and HTML tag
107 * @param {(String|Array<String>)} [options.source_types] Specify which
108 * source type the tag should include. defaults to webm, mp4 and ogv.
109 * @param {String} [options.source_transformation] specific transformations
110 * to use for a specific source type.
111 * @param {(String|Object)} [options.poster] image URL or
112 * poster options that may include a <tt>public_id</tt> key and
113 * poster-specific transformations
114 * @example <caption>Example of generating a video tag:</caption>
115 * cloudinary.video("mymovie.mp4");
116 * cloudinary.video("mymovie.mp4", {source_types: 'webm'});
117 * cloudinary.video("mymovie.ogv", {poster: "myspecialplaceholder.jpg"});
118 * cloudinary.video("mymovie.webm", {source_types: ['webm', 'mp4'], poster: {effect: 'sepia'}});
119 * @return {string} HTML video tag
120 */
121exports.video = function video(public_id, options) {
122 options = _.extend({}, options);
123 public_id = public_id.replace(/\.(mp4|ogv|webm)$/, '');
124 var source_types = optionConsume(options, 'source_types', []);
125 var source_transformation = optionConsume(options, 'source_transformation', {});
126 var fallback = optionConsume(options, 'fallback_content', '');
127
128 if (source_types.length === 0) source_types = cloudinary.utils.DEFAULT_VIDEO_SOURCE_TYPES;
129 var video_options = _.cloneDeep(options);
130
131 if (video_options.hasOwnProperty('poster')) {
132 if (_.isPlainObject(video_options.poster)) {
133 if (video_options.poster.hasOwnProperty('public_id')) {
134 video_options.poster = cloudinary.utils.url(video_options.poster.public_id, video_options.poster);
135 } else {
136 video_options.poster = cloudinary.utils.url(public_id, _.extend({}, cloudinary.utils.DEFAULT_POSTER_OPTIONS, video_options.poster));
137 }
138 }
139 } else {
140 video_options.poster = cloudinary.utils.url(public_id, _.extend({}, cloudinary.utils.DEFAULT_POSTER_OPTIONS, options));
141 }
142
143 if (!video_options.poster) delete video_options.poster;
144
145 var html = '<video ';
146
147 if (!video_options.hasOwnProperty('resource_type')) video_options.resource_type = 'video';
148 var multi_source = _.isArray(source_types) && source_types.length > 1;
149 var source = public_id;
150 if (!multi_source) {
151 source = source + '.' + cloudinary.utils.build_array(source_types)[0];
152 }
153 var src = cloudinary.utils.url(source, video_options); // calculate src and reduce video_options
154 if (!multi_source) {
155 video_options.src = src;
156 }
157 if (video_options.hasOwnProperty("html_width")) video_options.width = optionConsume(video_options, 'html_width');
158 if (video_options.hasOwnProperty("html_height")) video_options.height = optionConsume(video_options, 'html_height');
159 html = html + cloudinary.utils.html_attrs(video_options) + '>';
160 if (multi_source) {
161 html += source_types.map(function (source_type) {
162 var transformation = source_transformation[source_type] || {};
163 var sourceSrc = cloudinary.utils.url(source + "." + source_type, _.extend({ resource_type: 'video' }, _.cloneDeep(options), _.cloneDeep(transformation)));
164 var video_type = source_type === 'ogv' ? 'ogg' : source_type;
165 var type = "video/" + video_type;
166 return `<source ${cloudinary.utils.html_attrs({ src: sourceSrc, type })}>`;
167 }).join('');
168 }
169
170 html = html + fallback;
171 html = html + '</video>';
172 return html;
173};
174
175/**
176 * Generate a <code>source</code> tag.
177 * @param {string} public_id
178 * @param {object} options
179 * @param {srcset} options.srcset arguments required to generate the srcset attribute.
180 * @param {object} options.attributes HTML tag attributes
181 * @return {string}
182 */
183exports.source = function source(public_id) {
184 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
185
186 var srcsetParam = cloudinary.utils.extend({}, options.srcset, cloudinary.config().srcset);
187 var attributes = options.attributes || {};
188
189 cloudinary.utils.extend(attributes, generateImageResponsiveAttributes(public_id, attributes, srcsetParam, options));
190 if (!attributes.srcset) {
191 attributes.srcset = cloudinary.url(public_id, options);
192 }
193 if (!attributes.media && options.media) {
194 attributes.media = generateMediaAttr(options.media);
195 }
196 return `<source ${cloudinary.utils.html_attrs(attributes)}>`;
197};
198
199/**
200 * Generate a <code>picture</code> HTML tag.<br>
201 * The sources argument defines different transformations to apply for each
202 * media query.
203 * @param {string}public_id
204 * @param {object} options
205 * @param {object[]} options.sources a list of source arguments. A source tag will be rendered for each item
206 * @param {number} options.sources.min_width a minimum width query
207 * @param {number} options.sources.max_width a maximum width query
208 * @param {number} options.sources.transformation the transformation to apply to the source tag.
209 * @return {string} A picture HTML tag
210 * @example
211 *
212 * cloudinary.picture("sample", {
213 * sources: [
214 * {min_width: 1600, transformation: {crop: 'fill', width: 800, aspect_ratio: 2}},
215 * {min_width: 500, transformation: {crop: 'fill', width: 600, aspect_ratio: 2.3}},
216 * {transformation: {crop: 'crop', width: 400, gravity: 'auto'}},
217 * ]}
218 * );
219 */
220exports.picture = function picture(public_id) {
221 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
222
223 var sources = options.sources || [];
224 options = cloudinary.utils.clone(options);
225 delete options.sources;
226 cloudinary.utils.patchFetchFormat(options);
227 return "<picture>" + sources.map(function (source) {
228 var sourceOptions = chainTransformations(options, source.transformation);
229 sourceOptions.media = source;
230 return cloudinary.source(public_id, sourceOptions);
231 }).join('') + cloudinary.image(public_id, options) + "</picture>";
232};
233
234exports.cloudinary_js_config = cloudinary.utils.cloudinary_js_config;
235exports.CF_SHARED_CDN = cloudinary.utils.CF_SHARED_CDN;
236exports.AKAMAI_SHARED_CDN = cloudinary.utils.AKAMAI_SHARED_CDN;
237exports.SHARED_CDN = cloudinary.utils.SHARED_CDN;
238exports.BLANK = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
239exports.v2 = require('./v2');
\No newline at end of file