Dotclear

Changeset 399:16dff9759f10 for admin/js


Ignore:
Timestamp:
06/21/11 13:56:59 (14 years ago)
Author:
Tomtom33 <tbouron@…>
Branch:
wysiwyg
Message:

Finished web media integration with oembed

Location:
admin/js
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • admin/js/jquery/jquery.oembed.js

    r381 r399  
    1 /*! 
    2  * jquery oembed plugin 
    3  * 
    4  * Copyright (c) 2009 Richard Chamorro 
    5  * Licensed under the MIT license 
    6  *  
    7  * Author: Richard Chamorro  
    8  */ 
    9   
    10 (function ($) { 
    11     $.fn.oembed = function (url, options, embedAction) { 
    12  
    13         settings = $.extend(true, $.fn.oembed.defaults, options); 
    14  
    15         initializeProviders(); 
    16  
    17         return this.each(function () { 
    18  
    19             var container = $(this), 
    20                     resourceURL = (url != null) ? url : container.attr("href"), 
    21                     provider; 
    22  
    23             if (embedAction) { 
    24                 settings.onEmbed = embedAction; 
    25             } else { 
    26                 settings.onEmbed = function (oembedData) { 
    27                     $.fn.oembed.insertCode(this, settings.embedMethod, oembedData); 
    28                 }; 
    29             } 
    30  
    31             if (resourceURL != null) { 
    32                 provider = $.fn.oembed.getOEmbedProvider(resourceURL); 
    33  
    34                 if (provider != null) { 
    35                     provider.params = getNormalizedParams(settings[provider.name]) || {}; 
    36                     provider.maxWidth = settings.maxWidth; 
    37                     provider.maxHeight = settings.maxHeight; 
    38                     embedCode(container, resourceURL, provider); 
    39                 } else { 
    40                     settings.onProviderNotFound.call(container, resourceURL); 
    41                 } 
    42             } 
    43  
    44             return container; 
    45         }); 
    46  
    47  
    48     }; 
    49  
    50     var settings, activeProviders = []; 
    51  
    52     // Plugin defaults 
    53     $.fn.oembed.defaults = { 
    54         maxWidth: null, 
    55         maxHeight: null, 
    56         embedMethod: "replace",    // "auto", "append", "fill"         
    57         defaultOEmbedProvider: "oohembed",   // "oohembed", "embed.ly", "none" 
    58         allowedProviders: null, 
    59         disallowedProviders: null, 
    60         customProviders: null, // [ new $.fn.oembed.OEmbedProvider("customprovider", null, ["customprovider\\.com/watch.+v=[\\w-]+&?"]) ]    
    61         defaultProvider: null, 
    62         greedy: true, 
    63         onProviderNotFound: function () { }, 
    64         beforeEmbed: function () { }, 
    65         afterEmbed: function () { }, 
    66         onEmbed: function () { }, 
     1(function() { 
     2     var settings = { 
     3          maxWidth: null, 
     4          maxHeight: null, 
     5          embedMethod: 'auto',               // auto|append|replace 
     6          defaultOEmbedProvider: 'embed.ly', // oohembed|embed.ly|none 
     7          allowedProviders: null, 
     8          disallowedProviders: null, 
     9          customProviders: null,             // [ new $.oembed('OEmbedProvider','customprovider', null, ['customprovider\\.com/watch.+v=[\\w-]+&?']) ] 
     10          greedy: false, 
     11          onProviderNotFound: function() {}, 
     12          beforeEmbed: function() {}, 
     13          afterEmbed: function() {}, 
     14          onEmbed: function() {}, 
    6715          onError: function() {}, 
    6816          ajaxOptions: {} 
    69     }; 
    70  
    71     /* Private functions */ 
    72     function getRequestUrl(provider, externalUrl) { 
    73  
    74         var url = provider.apiendpoint, qs = "", callbackparameter = provider.callbackparameter || "callback", i; 
    75  
    76         if (url.indexOf("?") <= 0) 
    77             url = url + "?"; 
    78         else 
    79             url = url + "&"; 
    80  
    81         if (provider.maxWidth != null && provider.params["maxwidth"] == null) 
    82             provider.params["maxwidth"] = provider.maxWidth; 
    83  
    84         if (provider.maxHeight != null && provider.params["maxheight"] == null) 
    85             provider.params["maxheight"] = provider.maxHeight; 
    86  
    87         for (i in provider.params) { 
    88             // We don't want them to jack everything up by changing the callback parameter 
    89             if (i == provider.callbackparameter) 
    90                 continue; 
    91  
    92             // allows the options to be set to null, don't send null values to the server as parameters 
    93             if (provider.params[i] != null) 
    94                 qs += "&" + escape(i) + "=" + provider.params[i]; 
    95         } 
    96  
    97         url += "format=json&url=" + escape(externalUrl) + 
    98                          qs + 
    99                          "&" + callbackparameter + "=?"; 
    100  
    101         return url; 
    102     }; 
    103  
    104     function embedCode(container, externalUrl, embedProvider) { 
    105  
    106         var requestUrl = getRequestUrl(embedProvider, externalUrl),         
    107                ajaxopts = $.extend({ 
    108                     url: requestUrl, 
     17     }; 
     18      
     19     var publicMethods = { 
     20          init: function(url,options) { 
     21               $.extend(settings,options); 
     22                
     23               internalMethods._init(); 
     24                
     25               return this.each(function(){ 
     26                    var container = $(this); 
     27                    var resourceURL = (url != null) ? url : container.attr('href'); 
     28                    var provider = null; 
     29                     
     30                    if (!options.onEmbed) { 
     31                         settings.onEmbed = function (data) { 
     32                              internalMethods._insertCode(this,settings.embedMethod,data); 
     33                         }; 
     34                    } 
     35                     
     36                    if (resourceURL != null) { 
     37                         provider = publicMethods.getOEmbedProvider(resourceURL); 
     38                          
     39                         if (provider != null) { 
     40                              provider.params = internalMethods._getNormalizedParams(settings[provider.name]) || {}; 
     41                              provider.maxWidth = settings.maxWidth; 
     42                              provider.maxHeight = settings.maxHeight; 
     43                              internalMethods._doRequest(container,resourceURL,provider); 
     44                         } else { 
     45                              settings.onProviderNotFound.call(container, resourceURL); 
     46                         } 
     47                    } 
     48                     
     49                    return container; 
     50               }); 
     51          }, 
     52           
     53          OEmbedProvider: function(name,type,urlschemes,apiendpoint,callbackparameter) { 
     54               this.name = name; 
     55               this.type = type; // 'photo', 'video', 'link', 'rich', null 
     56               this.urlschemes = getUrlSchemes(urlschemes); 
     57               this.apiendpoint = apiendpoint; 
     58               this.callbackparameter = callbackparameter; 
     59               this.maxWidth = 500; 
     60               this.maxHeight = 400; 
     61               this.enable = true; 
     62               var i, property, regExp; 
     63                
     64               this.matches = function (externalUrl) { 
     65                    for (i = 0; i < this.urlschemes.length; i++) { 
     66                         regExp = new RegExp(this.urlschemes[i], 'i'); 
     67                         if (externalUrl.match(regExp) != null) { 
     68                              return true; 
     69                         } 
     70                    } 
     71                    return false; 
     72               }; 
     73                
     74               this.fromJSON = function (json) { 
     75                    for (property in json) { 
     76                         if (property != 'urlschemes') { 
     77                              this[property] = json[property]; 
     78                         } else { 
     79                              this[property] = getUrlSchemes(json[property]); 
     80                         } 
     81                    } 
     82                    return true; 
     83               }; 
     84                
     85               function getUrlSchemes(urls) { 
     86                    if (internalMethods._isNullOrEmpty(urls)) { 
     87                         return ['.']; 
     88                    } 
     89                    if ($.isArray(urls)) { 
     90                         return urls; 
     91                    } 
     92                    return urls.split(';'); 
     93               } 
     94          }, 
     95           
     96          getOEmbedProvider: function(url) { 
     97               for (var i = 0; i < providers.length; i++) { 
     98                    if (providers[i].matches(url)) { 
     99                         return providers[i]; 
     100                    } 
     101               } 
     102               return null; 
     103          } 
     104     }; 
     105      
     106     var internalMethods = { 
     107          _doRequest: function(container,url,embedProvider) { 
     108               $.ajax($.extend({ 
     109                    url: internalMethods._getRequestUrl(embedProvider,url), 
    109110                    type: 'get', 
    110111                    dataType: 'json', 
    111                     // error: jsonp request doesnt' support error handling 
     112                    cache: false, 
     113                    timeout: 10000, 
    112114                    success:  function (data) { 
    113                          var oembedData = $.extend({}, data); 
    114                          switch (oembedData.type) { 
    115                               case "photo": 
    116                                    oembedData.code = $.fn.oembed.getPhotoCode(externalUrl, oembedData); 
     115                         var data = $.extend({ 
     116                              type: null, 
     117                              version: null, 
     118                              title: null, 
     119                              author_name: null, 
     120                              author_url: null, 
     121                              provider_name: null, 
     122                              provider_url: null, 
     123                              cache_age: null, 
     124                              thumbnail_url: null, 
     125                              thumbnail_width: null, 
     126                              thumbnail_height: null, 
     127                              url: null, 
     128                              width: null, 
     129                              height: null, 
     130                              html: null, 
     131                              code: null 
     132                         }, data); 
     133                         switch (data.type) { 
     134                              case 'photo': 
     135                                   data.code = internalMethods._getPhotoCode(data); 
    117136                                   break; 
    118                               case "video": 
    119                                    oembedData.code = $.fn.oembed.getVideoCode(externalUrl, oembedData); 
     137                              case 'video': 
     138                                   data.code = internalMethods._getVideoCode(data); 
    120139                                   break; 
    121                               case "rich": 
    122                                    oembedData.code = $.fn.oembed.getRichCode(externalUrl, oembedData); 
     140                              case 'rich': 
     141                                   data.code = internalMethods._getRichCode(data); 
    123142                                   break; 
    124                               default: 
    125                                    oembedData.code = $.fn.oembed.getGenericCode(externalUrl, oembedData); 
     143                              case 'link': 
     144                                   data.code = internalMethods._getLinkCode(data); 
    126145                                   break; 
    127                          } 
    128                          settings.beforeEmbed.call(container, oembedData); 
    129                          settings.onEmbed.call(container, oembedData); 
    130                          settings.afterEmbed.call(container, oembedData); 
     146                              case 'error': 
     147                                   settings.onError.call(null,data.error_code,data.error_message); 
     148                                   break; 
     149                         } 
     150                         settings.beforeEmbed.call(container,data); 
     151                         settings.onEmbed.call(container,data); 
     152                         settings.afterEmbed.call(container,data); 
    131153                    }, 
    132154                    error: settings.onError 
    133                }, settings.ajaxOptions || { } ); 
    134            
    135           $.ajax( ajaxopts );         
    136     }; 
    137  
    138     function initializeProviders() { 
    139  
    140         activeProviders = []; 
    141  
    142         var defaultProvider, restrictedProviders = [], i, provider; 
    143  
    144         if (!isNullOrEmpty(settings.allowedProviders)) { 
    145             for (i = 0; i < $.fn.oembed.providers.length; i++) { 
    146                 if ($.inArray($.fn.oembed.providers[i].name, settings.allowedProviders) >= 0) 
    147                     activeProviders.push($.fn.oembed.providers[i]); 
    148             } 
    149             // If there are allowed providers, jquery-oembed cannot be greedy 
    150             settings.greedy = false; 
    151  
    152         } else { 
    153             activeProviders = $.fn.oembed.providers; 
    154         } 
    155  
    156         if (!isNullOrEmpty(settings.disallowedProviders)) { 
    157             for (i = 0; i < activeProviders.length; i++) { 
    158                 if ($.inArray(activeProviders[i].name, settings.disallowedProviders) < 0) 
    159                     restrictedProviders.push(activeProviders[i]); 
    160             } 
    161             activeProviders = restrictedProviders; 
    162             // If there are allowed providers, jquery-oembed cannot be greedy 
    163             settings.greedy = false; 
    164         } 
    165  
    166         if (!isNullOrEmpty(settings.customProviders)) { 
    167             $.each(settings.customProviders, function (n, customProvider) { 
    168                 if (customProvider instanceof $.fn.oembed.OEmbedProvider) { 
    169                     activeProviders.push(provider); 
    170                 } else { 
    171                     provider = new $.fn.oembed.OEmbedProvider(); 
    172                     if (provider.fromJSON(customProvider)) 
    173                         activeProviders.push(provider); 
    174                 } 
    175             }); 
    176         } 
    177  
    178         // If in greedy mode, we add the default provider 
    179         defaultProvider = getDefaultOEmbedProvider(settings.defaultOEmbedProvider); 
    180         if (settings.greedy == true) { 
    181             activeProviders.push(defaultProvider); 
     155               },settings.ajaxOptions || {})); 
     156          }, 
     157           
     158          _insertCode: function(container,method,data) { 
     159               if (data.type == null && data.code == null) { 
     160                    return; 
     161               } 
     162                
     163               switch (method) { 
     164                    case 'auto': 
     165                         if (container.attr('href') == null) { 
     166                              internalMethods._insertCode(container,'append',data); 
     167                         } 
     168                         else { 
     169                              internalMethods._insertCode(container,'replace',data); 
     170                         }; 
     171                         break; 
     172                    case 'replace': 
     173                         container.replaceWith(data.code); 
     174                         break; 
     175                    case 'append': 
     176                         container.html(data.code); 
     177                         break; 
     178               } 
     179          }, 
     180           
     181          _getPhotoCode: function(data) { 
     182               var href = null; 
     183               var title = null; 
     184               var alt = new Array(); 
     185                
     186               href = data.thumbnail_url ? data.thumbnail_url : data.url; 
     187               title = data.title ? data.title : ''; 
     188                
     189               if (data.provider_name) alt.push(data.provider_name); 
     190               if (data.author_name) alt.push(data.author_name); 
     191               if (data.title) alt.push(data.title); 
     192                
     193               return $('<a>').attr({ 
     194                    'href': data.url, 
     195                    'title': title 
     196               }).append($('<img>').attr({ 
     197                    'src': href, 
     198                    'title': title, 
     199                    'alt': alt.join(' - ') 
     200               })); 
     201          }, 
     202           
     203          _getVideoCode: function(data) { 
     204               return internalMethods._getValidHXHTMLCode(data.html); 
     205          }, 
     206           
     207          _getRichCode: function(data) { 
     208               return internalMethods._getValidHXHTMLCode(data.html); 
     209          }, 
     210           
     211          _getLinkCode: function(data) { 
     212               var title = null; 
     213               var alt = new Array(); 
     214                
     215               title = data.title ? data.title : ''; 
     216                
     217               if (data.provider_name) alt.push(data.provider_name); 
     218               if (data.author_name) alt.push(data.author_name); 
     219               if (data.title) alt.push(data.title); 
     220                
     221               return $('<a>').attr({ 
     222                    'href': data.url, 
     223                    'title': title 
     224               }).append(title); 
     225          }, 
     226           
     227          _getValidHXHTMLCode: function(html) { 
     228               var xhtml = ''; 
     229                
     230               $(html).each(function() { 
     231                    if (this.tagName == 'IFRAME') { 
     232                         var attr = { 
     233                              'src': 'data', 
     234                              'width': 'width', 
     235                              'height': 'height' 
     236                         }; 
     237                         var attributes = this.attributes; 
     238                         object = $('<object>'); 
     239                         object.attr('type','text/html'); 
     240                         for (i in attributes) { 
     241                              if (attr.hasOwnProperty(attributes[i].name)) { 
     242                                   object.attr(attr[attributes[i].name],attributes[i].value); 
     243                              } 
     244                         } 
     245                         xhtml += object.get(0).outerHTML; 
     246                    }  
     247                    else if (this.tagName == 'OBJECT') { 
     248                         if ($(this).find('embed').size() > 0) { 
     249                              var embed = $(this).find('embed').get(0); 
     250                              if ($.inArray('src',embed.attributes)) { 
     251                                   $(this).attr('data',embed.attributes.src.nodeValue); 
     252                              } 
     253                              if ($.inArray('type',embed.attributes)) { 
     254                                   $(this).attr('type',embed.attributes.type.nodeValue); 
     255                              } 
     256                              $(this).find('embed').remove(); 
     257                         } 
     258                         xhtml += this.outerHTML; 
     259                    } else { 
     260                         xhtml += internalMethods._getValidHXHTMLCode($(html).find('iframe,object').get(0).outerHTML); 
     261                    } 
     262               }); 
     263                
     264               return xhtml; 
     265          }, 
     266           
     267          _init: function() { 
     268               var provider; 
     269               var i; 
     270                
     271               // If there are allowed providers, jQuery oembed can not be greedy 
     272               if (!internalMethods._isNullOrEmpty(settings.allowedProviders)) { 
     273                    settings.greedy = false; 
     274               } 
     275                
     276               // If there are allowed providers, jQuery oembed can not be greedy 
     277               // Disabled also providers 
     278               if (!internalMethods._isNullOrEmpty(settings.disallowedProviders)) { 
     279                    for (i = 0; i < providers.length; i++) { 
     280                         if ($.inArray(providers[i].name,settings.disallowedProviders)) { 
     281                              provider.enable = false; 
     282                         } 
     283                    } 
     284                    settings.greedy = false; 
     285               } 
     286                
     287               if (!internalMethods._isNullOrEmpty(settings.customProviders)) { 
     288                    $.each(settings.customProviders, function(n,customProvider) { 
     289                         if (customProvider instanceof publicMethods.OEmbedProvider) { 
     290                              providers.push(provider); 
     291                         } else { 
     292                              provider = new publicMethods.OEmbedProvider(); 
     293                              if (provider.fromJSON(customProvider)) { 
     294                                   providers.push(provider); 
     295                              } 
     296                         } 
     297                    }); 
     298               } 
     299                
     300               // If in greedy mode, we add the default provider 
     301               defaultProvider = internalMethods._getDefaultOEmbedProvider(settings.defaultOEmbedProvider); 
     302               if (settings.greedy) { 
     303                    providers.push(defaultProvider); 
     304               } 
     305                
     306               // If any provider has no apiendpoint, we use the default provider endpoint 
     307               for (i = 0; i < providers.length; i++) { 
     308                    if (providers[i].enable && providers[i].apiendpoint == null) { 
     309                         providers[i].apiendpoint = defaultProvider.apiendpoint; 
     310                    } 
     311               } 
     312          }, 
     313           
     314          _getDefaultOEmbedProvider: function(defaultOEmbedProvider) { 
     315               var url = 'http://oohembed.com/oohembed/'; 
     316               if (defaultOEmbedProvider == 'embed.ly') { 
     317                    url = 'http://api.embed.ly/v1/api/oembed?'; 
     318               } 
     319               return new publicMethods.OEmbedProvider(defaultOEmbedProvider,null,null,url,'callback'); 
     320          }, 
     321           
     322          _getRequestUrl: function(provider,externalUrl) { 
     323               var url = provider.apiendpoint, qs = "", callbackparameter = provider.callbackparameter || 'callback', i; 
     324                
     325               if (url.indexOf('?') <= 0) { 
     326                    url = url + '?'; 
     327               } else { 
     328                    url = url + '&'; 
     329               } 
     330                
     331               if (provider.maxWidth != null && provider.params['maxwidth'] == null) { 
     332                    provider.params['maxwidth'] = provider.maxWidth; 
     333               } 
     334                
     335               if (provider.maxHeight != null && provider.params['maxheight'] == null) { 
     336                    provider.params['maxheight'] = provider.maxHeight; 
     337               } 
     338                
     339               for (i in provider.params) { 
     340                    // We don't want them to jack everything up by changing the callback parameter 
     341                    if (i == provider.callbackparameter) { 
     342                         continue; 
     343                    } 
     344                     
     345                    // allows the options to be set to null, don't send null values to the server as parameters 
     346                    if (provider.params[i] != null) { 
     347                         qs += '&' + escape(i) + '=' + provider.params[i]; 
     348                    } 
     349               } 
     350                
     351               url += 'format=json&url=' + escape(externalUrl) + 
     352               qs + 
     353               '&' + callbackparameter + '=?'; 
     354                
     355               return url; 
     356          }, 
     357           
     358          _getNormalizedParams: function(params) { 
     359               if (params == null) { 
     360                    return null; 
     361               } 
     362                
     363               var key; 
     364               var normalizedParams = {}; 
     365                
     366               for (key in params) { 
     367                    if (key != null) { 
     368                         normalizedParams[key.toLowerCase()] = params[key]; 
     369                    } 
     370               } 
     371               return normalizedParams; 
     372          }, 
     373           
     374          _isNullOrEmpty: function(object) { 
     375               if (typeof object == 'undefined') { 
     376                    return true; 
     377               } else if (object == null) { 
     378                    return true; 
     379               } else if ($.isArray(object) && object.length == 0) { 
     380                    return true; 
     381               } else { 
     382                    return false; 
     383               } 
    182384          } 
    183         // If any provider has no apiendpoint, we use the default provider endpoint 
    184         for (i = 0; i < activeProviders.length; i++) { 
    185             if (activeProviders[i].apiendpoint == null) 
    186                 activeProviders[i].apiendpoint = defaultProvider.apiendpoint; 
    187         } 
    188     } 
    189  
    190     function getDefaultOEmbedProvider(defaultOEmbedProvider) { 
    191         var url = "http://oohembed.com/oohembed/"; 
    192         if (defaultOEmbedProvider == "embed.ly") 
    193             url = "http://api.embed.ly/v1/api/oembed?"; 
    194         return new $.fn.oembed.OEmbedProvider(defaultOEmbedProvider, null, null, url, "callback"); 
    195     } 
    196  
    197     function getNormalizedParams(params) { 
    198         if (params == null) 
    199             return null; 
    200         var key, normalizedParams = {}; 
    201         for (key in params) { 
    202             if (key != null) 
    203                 normalizedParams[key.toLowerCase()] = params[key]; 
    204         } 
    205         return normalizedParams; 
    206     } 
    207  
    208     function isNullOrEmpty(object) { 
    209         if (typeof object == "undefined") 
    210             return true; 
    211         if (object == null) 
    212             return true; 
    213         if ($.isArray(object) && object.length == 0) 
    214             return true; 
    215         return false; 
    216     } 
    217  
    218     /* Public functions */ 
    219     $.fn.oembed.insertCode = function (container, embedMethod, oembedData) { 
    220         if (oembedData == null) 
    221             return; 
    222  
    223         switch (embedMethod) { 
    224             case "auto": 
    225                 if (container.attr("href") != null) { 
    226                     $.fn.oembed.insertCode(container, "append", oembedData); 
    227                 } 
    228                 else { 
    229                     $.fn.oembed.insertCode(container, "replace", oembedData); 
    230                 }; 
    231                 break; 
    232             case "replace": 
    233                 container.replaceWith(oembedData.code); 
    234                 break; 
    235             case "fill": 
    236                 container.html(oembedData.code); 
    237                 break; 
    238             case "append": 
    239                 var oembedContainer = container.next(); 
    240                 if (oembedContainer == null || !oembedContainer.hasClass("oembed-container")) { 
    241                     oembedContainer = container 
    242                               .after('<div class="oembed-container"></div>') 
    243                               .next(".oembed-container"); 
    244                     if (oembedData != null && oembedData.provider_name != null) 
    245                         oembedContainer.toggleClass("oembed-container-" + oembedData.provider_name); 
    246                 } 
    247                 oembedContainer.html(oembedData.code); 
    248                 break; 
    249         } 
    250     }; 
    251  
    252     $.fn.oembed.getPhotoCode = function (url, oembedData) { 
    253         var code, alt = oembedData.title ? oembedData.title : ''; 
    254         alt += oembedData.author_name ? ' - ' + oembedData.author_name : ''; 
    255         alt += oembedData.provider_name ? ' - ' + oembedData.provider_name : ''; 
    256         code = '<div><a href="' + url + '" target=\'_blank\'><img src="' + oembedData.url + '" alt="' + alt + '"/></a></div>'; 
    257         if (oembedData.html) 
    258             code += "<div>" + oembedData.html + "</div>"; 
    259         return code; 
    260     }; 
    261  
    262     $.fn.oembed.getVideoCode = function (url, oembedData) { 
    263         var code = oembedData.html; 
    264  
    265         return code; 
    266     }; 
    267  
    268     $.fn.oembed.getRichCode = function (url, oembedData) { 
    269         var code = oembedData.html; 
    270         return code; 
    271     }; 
    272  
    273     $.fn.oembed.getGenericCode = function (url, oembedData) { 
    274         var title = (oembedData.title != null) ? oembedData.title : url, 
    275                code = '<a href="' + url + '">' + title + '</a>'; 
    276         if (oembedData.html) 
    277             code += "<div>" + oembedData.html + "</div>"; 
    278         return code; 
    279     }; 
    280  
    281     $.fn.oembed.isProviderAvailable = function (url) { 
    282         var provider = getOEmbedProvider(url); 
    283         return (provider != null); 
    284     }; 
    285  
    286     $.fn.oembed.getOEmbedProvider = function (url) { 
    287         for (var i = 0; i < activeProviders.length; i++) { 
    288             if (activeProviders[i].matches(url)) 
    289                 return activeProviders[i]; 
    290         } 
    291         return null; 
    292     }; 
    293  
    294     $.fn.oembed.OEmbedProvider = function (name, type, urlschemesarray, apiendpoint, callbackparameter) { 
    295         this.name = name; 
    296         this.type = type; // "photo", "video", "link", "rich", null 
    297         this.urlschemes = getUrlSchemes(urlschemesarray); 
    298         this.apiendpoint = apiendpoint; 
    299         this.callbackparameter = callbackparameter; 
    300         this.maxWidth = 500; 
    301         this.maxHeight = 400; 
    302         var i, property, regExp; 
    303  
    304         this.matches = function (externalUrl) { 
    305             for (i = 0; i < this.urlschemes.length; i++) { 
    306                 regExp = new RegExp(this.urlschemes[i], "i"); 
    307                 if (externalUrl.match(regExp) != null) 
    308                     return true; 
    309             } 
    310             return false; 
    311         }; 
    312  
    313         this.fromJSON = function (json) { 
    314             for (property in json) { 
    315                 if (property != "urlschemes") 
    316                     this[property] = json[property]; 
    317                 else 
    318                     this[property] = getUrlSchemes(json[property]); 
    319             } 
    320             return true; 
    321         }; 
    322  
    323         function getUrlSchemes(urls) { 
    324             if (isNullOrEmpty(urls)) 
    325                 return ["."]; 
    326             if ($.isArray(urls)) 
    327                 return urls; 
    328             return urls.split(";"); 
    329         } 
    330     }; 
    331  
    332     /* Native & common providers */ 
    333     $.fn.oembed.providers = [ 
    334           new $.fn.oembed.OEmbedProvider("youtube", "video", ["youtube\\.com/watch.+v=[\\w-]+&?"]), // "http://www.youtube.com/oembed" (no jsonp) 
    335           new $.fn.oembed.OEmbedProvider("flickr", "photo", ["flickr\\.com/photos/[-.\\w@]+/\\d+/?"], "http://flickr.com/services/oembed", "jsoncallback"), 
    336           new $.fn.oembed.OEmbedProvider("viddler", "video", ["viddler\.com"]), // "http://lab.viddler.com/services/oembed/" (no jsonp) 
    337           new $.fn.oembed.OEmbedProvider("blip", "video", ["blip\\.tv/.+"], "http://blip.tv/oembed/"), 
    338           new $.fn.oembed.OEmbedProvider("hulu", "video", ["hulu\\.com/watch/.*"], "http://www.hulu.com/api/oembed.json"), 
    339           new $.fn.oembed.OEmbedProvider("vimeo", "video", ["http:\/\/www\.vimeo\.com\/groups\/.*\/videos\/.*", "http:\/\/www\.vimeo\.com\/.*", "http:\/\/vimeo\.com\/groups\/.*\/videos\/.*", "http:\/\/vimeo\.com\/.*"], "http://vimeo.com/api/oembed.json"), 
    340           new $.fn.oembed.OEmbedProvider("dailymotion", "video", ["dailymotion\\.com/.+"]), // "http://www.dailymotion.com/api/oembed/" (callback parameter does not return jsonp) 
    341           new $.fn.oembed.OEmbedProvider("scribd", "rich", ["scribd\\.com/.+"]), // ", "http://www.scribd.com/services/oembed"" (no jsonp)        
    342           new $.fn.oembed.OEmbedProvider("slideshare", "rich", ["slideshare\.net"], "http://www.slideshare.net/api/oembed/1"), 
    343           new $.fn.oembed.OEmbedProvider("photobucket", "photo", ["photobucket\\.com/(albums|groups)/.*"], "http://photobucket.com/oembed/") 
    344           // new $.fn.oembed.OEmbedProvider("vids.myspace.com", "video", ["vids\.myspace\.com"]), // "http://vids.myspace.com/index.cfm?fuseaction=oembed" (not working) 
    345           // new $.fn.oembed.OEmbedProvider("screenr", "rich", ["screenr\.com"], "http://screenr.com/api/oembed.json") (error)          
    346           // new $.fn.oembed.OEmbedProvider("qik", "video", ["qik\\.com/\\w+"], "http://qik.com/api/oembed.json"),       
    347           // new $.fn.oembed.OEmbedProvider("revision3", "video", ["revision3\.com"], "http://revision3.com/api/oembed/") 
     385     }; 
     386      
     387     var providers = [ 
     388          new publicMethods.OEmbedProvider('youtube', 'video', ['youtube\\.com/watch.+v=[\\w-]+&?']), // 'http://www.youtube.com/oembed'    (no jsonp) 
     389          new publicMethods.OEmbedProvider('flickr', 'photo', ['flickr\\.com/photos/[-.\\w@]+/\\d+/?'], 'http://flickr.com/services/oembed', 'jsoncallback'), 
     390          new publicMethods.OEmbedProvider('viddler', 'video', ['viddler\.com']), // 'http://lab.viddler.com/services/oembed/' (no jsonp) 
     391          new publicMethods.OEmbedProvider('blip', 'video', ['blip\\.tv/.+'], 'http://blip.tv/oembed/'), 
     392          new publicMethods.OEmbedProvider('hulu', 'video', ['hulu\\.com/watch/.*'], 'http://www.hulu.com/api/oembed.json'), 
     393          new publicMethods.OEmbedProvider('vimeo', 'video', ['http:\/\/www\.vimeo\.com\/groups\/.*\/videos\/.*', 'http:\/\/www\.vimeo\.com\/.*', 'http:\/\/vimeo\.com\/groups\/.*\/videos\/.*', 'http:\/\/vimeo\.com\/.*'], 'http://vimeo.com/api/oembed.json'), 
     394          new publicMethods.OEmbedProvider('dailymotion', 'video', ['dailymotion\\.com/.+']), // 'http://www.dailymotion.com/api/oembed/' (callback parameter does not return jsonp) 
     395          new publicMethods.OEmbedProvider('scribd', 'rich', ['scribd\\.com/.+']), // ', 'http://www.scribd.com/services/oembed'' (no jsonp)           
     396          new publicMethods.OEmbedProvider('slideshare', 'rich', ['slideshare\.net'], 'http://www.slideshare.net/api/oembed/1'), 
     397          new publicMethods.OEmbedProvider('photobucket', 'photo', ['photobucket\\.com/(albums|groups)/.*'], 'http://photobucket.com/oembed/') 
    348398     ]; 
    349 })(jQuery); 
     399      
     400     $.fn.oembed = function(method) { 
     401          if (publicMethods[method]) { 
     402               return publicMethods[method].apply(this,Array.prototype.slice.call(arguments,1)); 
     403          } else if (typeof method === 'string' || !method) { 
     404               return publicMethods.init.apply(this,arguments); 
     405          } else { 
     406               $.error('Method ' + method + ' does not exist on jQuery.oembed'); 
     407          } 
     408     }; 
     409})(jQuery) 
  • admin/js/tiny_mce/plugins/dcControls/js/popup_web_media.js

    r381 r399  
    55          maxWidth: 480, 
    66          maxHeight: 400, 
    7           defaultOEmbedProvider: 'embed.ly', 
    8           greedy: false, 
    9           embedMethod: 'fill', 
    107          onProviderNotFound: function(url) { 
    118               $('#src').removeClass().addClass('error'); 
     
    1714               } else { 
    1815                    $('#src').removeClass().addClass('success'); 
    19                     if (!data.thumbnail_url) { 
    20                          $('input[value="thumbnail"]').attr('disabled',true); 
    21                     } else { 
    22                          $('input[value="thumbnail"]').attr('disabled',false); 
    23                     } 
    24                     $('div.two-cols').slideDown(); 
    2516               } 
    2617          }, 
     
    3930                     
    4031                    if (!data.thumbnail_url) { 
    41                          $('input[name="insertion" value="thumbnail"]').attr('disabled', 'disabled'); 
     32                         $('input[value="thumbnail"]').attr('disabled',true); 
     33                    } else { 
     34                         $('input[value="thumbnail"]').attr('disabled',false); 
    4235                    } 
    4336                     
    44                     $(this).data('code',popup_web_media.getValidXHTMLCode(data.html)); 
     37                    $(this).data('data',data); 
     38                     
     39                    $('div.two-cols').slideDown(); 
    4540               } 
    46           }, 
    47           onEmbed: function(data) { 
    48                $(this).data('code',popup_web_media.getValidXHTMLCode(data.html)); 
    4941          }, 
    5042          onError: function(xhr,status,error) { 
    5143               $('#src').removeClass().addClass('error'); 
    52           }, 
    53           ajaxOptions: { 
    54                cache: false, 
    55                timeout: 5000 
    5644          } 
    5745     }, 
     
    7159               $('#src').removeClass().addClass('loading'); 
    7260                
    73                $('#alt,#title').val(''); 
     61               $('#alt,#title,#width,#height').val(''); 
    7462                
    75                $('div.preview').data('code',null); 
     63               $('div.preview').data('data',null); 
    7664               $('div.preview').oembed($('#src').val(),popup_web_media.oembed_opts); 
    7765          }); 
     
    7967          $('#webmedia-insert-ok').click(function(){ 
    8068               var ed = tinyMCEPopup.editor; 
    81                var xhtml = $('div.preview').data('code'); 
     69               var media_align_grid = { 
     70                    left: 'float: left; margin: 0 1em 1em 0;', 
     71                    right: 'float: right; margin: 0 0 1em 1em;', 
     72                    center: 'text-align: center;' 
     73               }; 
     74               var data = $('div.preview').data('data'); 
     75               var alignment = $('input[name=alignment]:checked').val(); 
     76               var insertion = $('input[name="insertion"]:checked').val(); 
     77               var src = $('input[name="src"]').val(); 
     78               var title = $('input[name="title"]').val(); 
     79               var alt = $('input[name="alt"]').val(); 
     80               var code = ''; 
    8281                
    83                if (xhtml != null) { 
    84                     ed.execCommand('mceInsertContent',false,xhtml.get(0).outerHTML) 
     82               if (data != null) { 
     83                    var a = $('<a>').attr({ 
     84                         'href': src, 
     85                         'title': title 
     86                    }); 
     87                    var img = $('<img>').attr({ 
     88                         'src': data.thumbnail_url, 
     89                         'alt': alt, 
     90                         'title': title 
     91                    }); 
     92                    switch($('input[name="insertion"]:checked').val()) { 
     93                         case 'media': 
     94                              code = $(data.code); 
     95                              break; 
     96                         case 'thumbnail': 
     97                              code = a.append(img); 
     98                              break; 
     99                         case 'link': 
     100                              code = a.append(alt); 
     101                              break; 
     102                    } 
     103                     
     104                    if (alignment != 'none') { 
     105                         code.attr('style',media_align_grid[alignment]); 
     106                    } 
     107                     
     108                    ed.execCommand('mceInsertContent',false,code.get(0).outerHTML); 
    85109                    tinyMCEPopup.close(); 
    86110               } else { 
    87                     alert('provide a valide media'); 
     111                    alert('Provide a valid media'); 
    88112               } 
    89113          }); 
Note: See TracChangeset for help on using the changeset viewer.

Sites map