Skip to content

Instantly share code, notes, and snippets.

@ashblue
Created September 4, 2014 01:08
Show Gist options
  • Save ashblue/6a8cdb36b8229e32dd17 to your computer and use it in GitHub Desktop.
Save ashblue/6a8cdb36b8229e32dd17 to your computer and use it in GitHub Desktop.
#
# Offer Drawer Module
#
# @author
#
(($, window, document) ->
class offerDrawerModule
_data: undefined
_defaultId: '#offer-drawer'
_drawerTmpl: window.bootstrapdirectory + "/modules/offer-drawer-module/templates/offer-drawer-template.html"
constructor: ($el, options) ->
@$el = $el
@_data = options?.data
@_caret = options?.caret if options.caret?
@_layout = options?.layout if options.layout?
# Circumvent layout creation
$container = options?.$container if options.$container?
$replace = options?.$replace if options.$replace?
# Make human readable dates
if @_data
@_startsOn = new Date(@_data.StartsOn).toLocaleDateString("en-US")
@_expiresOn = new Date(@_data.ExpiresOn).toLocaleDateString("en-US")
# Handle offer drawer slightly different if the element is already generated
if !($replace and $container)
@destroy()
@renderOfferDrawer()
else
@destroy($container.children())
$container.hide().append($replace)
@renderExistingOfferDrawer($container)
# Pre-existing offer drawer element
renderExistingOfferDrawer: ($el) =>
@showDrawer($el)
@bindClick()
renderOfferDrawer: =>
caretPos = @determineCaretPosition()
self = @
$.get @_drawerTmpl, (template) =>
template = $(template).filter(@_layout).html()
@share()
rendered = Mustache.render(template, {
data: @_data,
begin: @_startsOn,
end: @_expiresOn,
shareEmailUrl: @_shareEmailUrl,
shareFacebookUrl: @_shareFacebookUrl,
shareTwitterUrl: @_shareTwitterUrl,
caretPosition: caretPos
} )
$target = $(rendered).insertAfter(@$el)
self.showDrawer($target)
@bindClick()
@
showDrawer: ($target) =>
$target.show().slideToggle(500)
# Scroll to offer when clicked
if $('html').hasClass('mobile')
@_offset = $('.filter-body').height() + @$el.height() / 2
else
@_offset = $('.filter-body').height() + @$el.height() / 3
$("html, body").animate
scrollTop: $("#offer-drawer").offset().top - @_offset, 500
determineCaretPosition: () =>
caret = ''
if @_caret is 0
caret = 'caret-left'
else if @_caret is 2
caret = 'caret-right'
caret
bindClick: =>
$('#offer-drawer-close').on 'click', (evt) =>
evt.preventDefault()
@destroy()
$(".tc-label").on "click", (e) =>
$(".offer-tc-detail").slideToggle()
@
share: =>
@_shareTitle = encodeURIComponent(@_data.Title)
@_shareShortTitle = encodeURIComponent(@_data.ShortTitle)
@_shareSummary = encodeURIComponent(@_data.Summary)
@_shareThumb = 'http://usa.visa.com/img/visa-signature/facebook-share.png'
@_shareOfferId = encodeURIComponent(@_data.Id)
@_offerUrl = window.location + "?offerid=" + @_shareOfferId
@_shareIntro = 'I%20just%20found%20out%20I%20get%20great%20benefits%20just%20for%20being%20a%20Visa%20Signature%20cardholder.%20Don%27t%20miss%20out.%20www.visasignature.com:%20'
@_shareEmailUrl = "?subject=" + @_shareShortTitle + ":%20" + @_shareTitle + "&body=" + @_shareIntro + @_shareSummary + "%20:%0D%0A%0D%0A" + @_offerUrl
@_shareTwitterUrl = "https://twitter.com/share?text=Visa%20Signature:%20" + @_shareSummary + "&url=" + @_offerUrl
if $('html').hasClass('touch')
@_shareFacebookUrl = 'https://www.facebook.com/dialog/feed?app_id=431908216897124&link=' + @_offerUrl + '&picture=' + @_shareThumb + '&name=' + @_shareTitle + '&description=' + @_shareSummary + '&redirect_uri=' + @_offerUrl
else
@_shareFacebookUrl = 'http://www.facebook.com/sharer/sharer.php?s=100&p[url]=' + @_offerUrl + '&p[images][0]=' + @_shareThumb + '&p[title]=' + @_shareTitle + '&p[summary]=' + @_shareSummary
@
destroy: (target) =>
$(target || @_defaultId).slideUp(300).remove()
@
$.fn.offerDrawerModule = (args) ->
@offerDrawerModule = new offerDrawerModule(@,args)
) jQuery, window, document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment