Skip to content

Instantly share code, notes, and snippets.

@ivankravchenko
Created February 8, 2013 11:16
Show Gist options
  • Save ivankravchenko/4738224 to your computer and use it in GitHub Desktop.
Save ivankravchenko/4738224 to your computer and use it in GitHub Desktop.
$ ->
$.fn.iphoneMenu = ->
$.each @, -> initiateIphoneMenu @
@ # chaining
$.fn.iphoneBreadcrumbs = (pageMenuSelector) ->
$.each @, -> initiateIphoneBreadcrumbs @, pageMenuSelector
@ # chaining
initiateIphoneBreadcrumbs = (el, pageMenuSelector) ->
$pageMenu = $ """<select class="page-menu-select"></select>"""
addMenu = no
$(pageMenuSelector).find("a").each ->
$option = $("<option></option>")
href = $(@).attr("href")
if href is window.location.href
addMenu = yes
$option.attr "value", href
if $(@).parent().is(".active")
$option.attr "selected", "selected"
$option.text $(@).text()
$pageMenu.append $option
$pageMenu.change ->
window.location.href = $(@).val()
if addMenu
$(el).addClass 'page-menu-select-added'
$(el).after $pageMenu
initiateIphoneMenu = (el) ->
$iphoneMenu = $ """<select id="iphone-menu"></select>"""
hasCurrent = no
$(el).find("a").each ->
$option = $("<option></option>")
$option.attr "value", $(@).attr("href")
if $(@).parent().parent().parent().is("li")
$option.attr "short", " –– " + $(@).text()
$option.attr "long", $(@).parent().parent().parent().find(">a").text() + " –– " + $(@).text()
else
$option.attr "short", $(@).text()
$option.attr "long", $option.attr("short")
if $(@).parent().is(".current-menu-item")
$option.attr "selected", "selected"
hasCurrent = yes
$option.text $option.attr("long")
$iphoneMenu.append $option
unless hasCurrent
$currentOption = $ "<option></option>"
$currentOption.attr "value", window.location.href
$currentOption.attr "selected", "selected"
$currentOption.text window.document.title
$iphoneMenu.prepend $currentOption
$iphoneMenu.change ->
window.location.href = $(@).val()
$(el).after $iphoneMenu
# fnLongItems = ->
# $iphoneMenu.find("option").each ->
# $(@).text $(@).attr "long"
# fnShortItems = ->
# $iphoneMenu.find("option").each ->
# $(@).text $(@).attr "short"
#
#$iphoneMenu.change(function(){ fnLongItems() });
#$iphoneMenu.mousedown(function(){ fnShortItems() });
#$iphoneMenu.mouseout(function(){ fnLongItems() });
#
#console.log($iphoneMenu);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment