Skip to content

Commit 3bf7012

Browse files
committed
feat(promises): return promiss on .render() , .text() and .applyEmbedJS()
#111
1 parent 9ba310f commit 3bf7012

6 files changed

Lines changed: 108 additions & 49 deletions

File tree

demo/index.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,23 @@ <h1>Loading embed.js in a block</h1>
9393
input: document.getElementById('block'),
9494
tweetsEmbed:true
9595
});
96-
x.render();
97-
98-
x.text(function(data){
96+
x.render().then(function(data){
9997
console.log(data);
10098
});
10199

100+
x.text().then(function(data){
101+
console.log(data)
102+
})
103+
102104

103105
// EmbedJS.template.basicAudio = function(match){
104106
// return 'kjkh'+ match
105107
// };
106108

107109

108-
EmbedJS.applyEmbedJS(".block",{});
110+
EmbedJS.applyEmbedJS(".block").then(function(data){
111+
console.log(data)
112+
})
109113

110114
</script>
111115

dist/embed.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js

Lines changed: 56 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/embed.es6

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import regeneratorRuntime from './vendor/regeneratorRuntime.js'
22

33
import { ifEmbed, createText, deepExtend, cloneObject } from './modules/utils.es6'
44

5-
import Template from './modules/template.es6'
5+
import Renderer from './modules/renderer.es6'
66

77
import Emoji from './modules/emoticons/emoji.es6'
88
import Smiley from './modules/emoticons/smiley.es6'
@@ -128,7 +128,8 @@ var defaultOptions = {
128128
};
129129

130130
let instances = [];
131-
let allInstances = []
131+
let allInstances = [];
132+
let promises = [];
132133

133134
export default class EmbedJS {
134135
/**
@@ -141,7 +142,7 @@ export default class EmbedJS {
141142
* @param {string} input [optional] The string to be processed
142143
* @return {null}
143144
*/
144-
constructor(options, renderer) {
145+
constructor(options, template) {
145146
/**
146147
* We have created a clone of the original options to make sure that the original object
147148
* isn't altered.
@@ -156,11 +157,11 @@ export default class EmbedJS {
156157
//object while creating a new instance of embed.js
157158
this.options = deepExtend(globOptions, options);
158159

159-
this.options.template = renderer || new Template();
160+
this.options.template = template || new Renderer();
160161

161162
if (!this.options.input || !(typeof this.options.input === 'string' || typeof this.options.input === 'object')) throw ReferenceError("You need to pass an element or the string that needs to be processed");
162163

163-
this.input = typeof this.options.input === 'object' ? this.options.input.innerHTML : this.options.input
164+
this.input = (typeof this.options.input === 'object') ? this.options.input.innerHTML : this.options.input
164165

165166
}
166167

@@ -277,6 +278,22 @@ export default class EmbedJS {
277278
[output, embeds] = await (this.twitter.process());
278279
}
279280

281+
this.data = {
282+
input : options.input,
283+
output : output,
284+
options : options,
285+
inputString : this.input,
286+
/**
287+
288+
TODO:
289+
- Restructure served urls structure with services name
290+
291+
*/
292+
293+
services : options.served,
294+
template : options.template
295+
}
296+
280297
return createText(output, embeds);
281298
}
282299

@@ -303,16 +320,18 @@ export default class EmbedJS {
303320
this.options.input.dispatchEvent(event);
304321

305322
this.options.afterEmbedJSApply();
323+
324+
return new Promise((resolve, reject) => (this.data.output ? resolve(this.data) : reject('error')))
306325
}
307326

308327
/**
309328
* returns the resulting string based on the input and the options passed by the user.
310329
* @param {Function} callback Function that is executed once the data is ready
311330
* @return null
312331
*/
313-
async text(callback) {
332+
async text() {
314333
let result = await this.process();
315-
callback(result, this.input);
334+
return new Promise((resolve, reject) => (result ? resolve(this.data) : reject('error')))
316335
}
317336

318337
/**
@@ -340,14 +359,18 @@ export default class EmbedJS {
340359
* @param {string} className
341360
* @return {null}
342361
*/
343-
static applyEmbedJS(selectorName, options, renderer) {
362+
static applyEmbedJS(selectorName, options = {}, template = (new Renderer())) {
344363
let elements = document.querySelectorAll(selectorName);
345-
renderer = renderer || new Template();
346364
for (let i = 0; i < elements.length; i++) {
347365
options.input = elements[i];
348-
instances[i] = new EmbedJS(options, renderer);
349-
instances[i].render()
350-
}
366+
instances[i] = new EmbedJS(options, template);
367+
promises[i] = instances[i].render()
368+
}
369+
return new Promise((resolve) => {
370+
Promise.all(promises).then(function(val){
371+
resolve(val);
372+
})
373+
})
351374
}
352375

353376
/**
@@ -376,24 +399,24 @@ export default class EmbedJS {
376399
*
377400
* The usage of the plugin is described below.
378401
*
379-
* => Create a new Instance of the template by using .Renderer() method of EmbedJS.
402+
* => Create a new Instance of the template by using .Template() method of EmbedJS.
380403
*
381-
* var renderer = EmbedJS.Renderer()
404+
* var template = EmbedJS.Template()
382405
*
383406
* => Now create different templates for different service names.
384407
*
385-
* renderer.url = function(match, options){
408+
* template.url = function(match, options){
386409
* return '<a href=" + match + "> + match + </a>'
387410
* }
388411
*
389-
* renderer.instagram = function(match, dimensions, options){
412+
* template.instagram = function(match, dimensions, options){
390413
* var config = options.soundCloudOptions;
391414
* return `<div class="ejs-embed ejs-instagram"><iframe src="${toUrl(match.split('/?')[0])}/embed/" height="${dimensions.height}"></iframe></div>`;
392415
* }
393416
*
394417
*/
395-
static Renderer() {
396-
return new Template();
418+
static Template() {
419+
return new Renderer();
397420
}
398421
}
399422

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { toUrl } from './utils.es6'
22

3-
export default class Template{
3+
export default class Renderer{
44
constructor(options){
55
this.options = options || {}
66
}

0 commit comments

Comments
 (0)