@@ -2,7 +2,7 @@ import regeneratorRuntime from './vendor/regeneratorRuntime.js'
22
33import { ifEmbed , createText , deepExtend , cloneObject } from './modules/utils.es6'
44
5- import Template from './modules/template .es6'
5+ import Renderer from './modules/renderer .es6'
66
77import Emoji from './modules/emoticons/emoji.es6'
88import Smiley from './modules/emoticons/smiley.es6'
@@ -128,7 +128,8 @@ var defaultOptions = {
128128} ;
129129
130130let instances = [ ] ;
131- let allInstances = [ ]
131+ let allInstances = [ ] ;
132+ let promises = [ ] ;
132133
133134export 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
0 commit comments