diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-06-12 18:01:20 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-06-12 18:01:20 -0400 |
| commit | f9a05332e51b586b42cffcf144dae6f4b3abe436 (patch) | |
| tree | ed9f30f241bbdca6c6c3717afd76d1356179f257 /public/assets/javascripts/ui/lib/Parser.js | |
| parent | e37702d371b459847a43f7c6f953dff31684d823 (diff) | |
client side of uploading process
Diffstat (limited to 'public/assets/javascripts/ui/lib/Parser.js')
| -rw-r--r-- | public/assets/javascripts/ui/lib/Parser.js | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/public/assets/javascripts/ui/lib/Parser.js b/public/assets/javascripts/ui/lib/Parser.js new file mode 100644 index 0000000..545ed41 --- /dev/null +++ b/public/assets/javascripts/ui/lib/Parser.js @@ -0,0 +1,96 @@ +var Parser = { + integrations: [{ + type: 'image', + regex: /\.(jpeg|jpg|gif|png|svg)(\?.*)?$/i, + async: false, + fetch: function(url, done) { + done("", "") + }, + tag: function (media) { + return '<img src="' + media.url + '" onerror="imgError(this);">'; + } + }, { + type: 'youtube', + regex: /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i, + async: false, + fetch: function(url, done) { + var id = (url.match(/v=([-_a-zA-Z0-9]{11})/i) || url.match(/youtu.be\/([-_a-zA-Z0-9]{11})/i) || url.match(/embed\/([-_a-zA-Z0-9]{11})/i))[1].split('&')[0]; + var thumb = "http://i.ytimg.com/vi/" + id + "/hqdefault.jpg" + done(id, thumb); + }, + tag: function (media) { + return '<img class="video" type="youtube" vid="'+media.token+'" src="'+media.thumbnail+'"><span class="playvid">▶</span>'; + } + }, { + type: 'vimeo', + regex: /vimeo.com\/\d+$/i, + async: true, + fetch: function(url, done) { + var id = url.match(/\d+$/i)[0]; + $.ajax({ + type: 'GET', + url: 'http://vimeo.com/api/v2/video/' + id + '.json', + success: function(result){ + if (result.length == 0) { return done(id, "") } + done(id, result[0].thumbnail_large) + } + }) + }, + tag: function (media) { + return '<img class="video" type="vimeo" vid="'+media.token+'" src="'+media.thumbnail+'"><span class="playvid">▶</span>'; + } + }, + /* + { + type: 'soundcloud', + regex: /soundcloud.com\/[-a-zA-Z0-9]+\/[-a-zA-Z0-9]+\/?$/i, + async: true, + fetch: function (url, done) { + $.ajax({ + type: 'GET', + url: 'http://api.soundcloud.com/resolve.json?url=' + + url + + '&client_id=' + + '0673fbe6fc794a7750f680747e863b10', + success: function(result) { + done(result.id, ""); + } + }); + }, + tag: function (media) { + return '<iframe width="400" height="166" scrolling="no" frameborder="no"' + + 'src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/' + media.token + + '&color=ff6600&auto_play=false&show_artwork=true"></iframe>' + } + }, { + type: 'link', + regex: /^http.+/i, + async: false, + fetch: function(url, done) { + done("", "") + }, + tag: function (media) { + return '<a href="' + media.url + '" target="_blank">' + media.url + '</a>' + } + } + */ + ], + + parse: function (url, cb) { + Parser.integrations.some(function(integration){ + if (integration.regex.test(url)) { + integration.fetch(url, function(token, thumbnail){ + cb({ + token: token, + thumbnail: thumbnail, + type: integration.type, + url: url, + }) + }) + return true + } + return false + }) + cb(null) + } +}
\ No newline at end of file |
