From 73f7ff21cc26c46acebb84bfdd5f8aa026b70471 Mon Sep 17 00:00:00 2001 From: Ansh Shukla Date: Wed, 7 Aug 2013 17:36:53 -0700 Subject: [PATCH] videolink: url time parsing logic working --- coffee/src/shells/youtube.shell.coffee | 35 +++++++++++++++++++++++++- coffee/src/util/util.coffee | 19 ++++++++++++++ coffee/src/views/index.coffee | 1 - 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/coffee/src/shells/youtube.shell.coffee b/coffee/src/shells/youtube.shell.coffee index 488f242..33c2cb5 100644 --- a/coffee/src/shells/youtube.shell.coffee +++ b/coffee/src/shells/youtube.shell.coffee @@ -21,7 +21,6 @@ YouTubeShell = acorn.shells.YouTubeShell = ] - class YouTubeShell.Model extends VideoLinkShell.Model @@ -57,6 +56,19 @@ class YouTubeShell.Model extends VideoLinkShell.Model pattern.exec(link)[3] + parseTime: (time) => + validTimePatterns = [ + /(\d+)/ + /(\d+)m(\d+)s/ + ] + + # the second one is more general, work backwards + if validTimePatterns[1].test time + return 60 * parseInt(validTimePatterns[1].exec(time)[1]) + parseInt(validTimePatterns[1].exec(time)[2]) + else if validTimePatterns[0].test time + return parseInt(validTimePatterns[0].exec(time)[0]) + else + return undefined embedLink: (options) => # see https://developers.google.com/youtube/player_parameters for options @@ -102,6 +114,27 @@ class YouTubeShell.RemixView extends VideoLinkShell.RemixView @model.timeTotal data.data.duration @model._updateAttributesWithDefaults() + start = acorn.util.fetchParameters this.model.link(), ["t", "start"] + # take the first possible valid parameter + + start = @model.timeStart() + unless _.isNumber start + start = acorn.util.fetchParameters this.model.link(), ["t", "start"] + start = @model.parseTime _.values(start)[0] + unless _.isNumber start + start = 0 + + end = @model.timeEnd() + unless _.isNumber end + end = acorn.util.fetchParameters this.model.link(), ["end"] + end = @model.parseTime _.values(end)[0] + unless _.isNumber end + end = @model.timeTotal() + + @model.timeStart(start) + @model.timeEnd(end) + + @_setTimeInputMin() @_setTimeInputMax() diff --git a/coffee/src/util/util.coffee b/coffee/src/util/util.coffee index 86c8980..6d2724e 100644 --- a/coffee/src/util/util.coffee +++ b/coffee/src/util/util.coffee @@ -274,7 +274,26 @@ util.parseUrl = (url) -> result +# function takes a URL and an array of parameters +# returns JSON object with matching parameters and values, if any +# otherwise, returns empty object +# http://stackoverflow.com/a/8649003 +util.fetchParameters = (url, params) -> + url = $.trim url + url = "http://#{url}" unless /^([a-z0-9]+:)?\/\//i.test url + anchor = document.createElement 'a' + anchor.href = url + + search = anchor.search.substring(1) + search = '{"' + search.replace(/&/g, '","').replace(/\=/g, '":"') + '"}' + + parameters = if search is "" then {} else JSON.parse search, (key, value) -> + if key is "" then value else decodeURIComponent value + + console.log(parameters) + _.pick parameters, params + # track mouse location at all times util.mouseLocationTracker = (-> diff --git a/coffee/src/views/index.coffee b/coffee/src/views/index.coffee index 5145292..9c2647e 100644 --- a/coffee/src/views/index.coffee +++ b/coffee/src/views/index.coffee @@ -21,7 +21,6 @@ `import "shell_editor_view"` `import "shell_options_view"` `import "shell_selector_view"` -`import "slider_handle_view"` `import "sliding_bar_view"` `import "sliding_object_view"` `import "sources_view"`