History

Tweak sidebar show/hide

Thibaut Courouble committed 10 years ago

Commit 4fc11261 parent dd4b9bf4 files · +45 −25Browse files

Unified · Split

4 files
  1. assets/javascripts/app/settings.coffee +2 −3
  2. assets/javascripts/views/layout/document.coffee +27 −8
  3. assets/javascripts/views/layout/resizer.coffee +16 −12
  4. assets/javascripts/views/search/search.coffee +0 −2
assets/javascripts/app/settings.coffee+2−3
@@ -82,10 +82,9 @@ class app.Settings · 81 unmodified lines
8282 hasLayout: (name) ->
8383 try
8484 layout = (Cookies.get(LAYOUT_KEY) || '').split(' ')
85− $.arrayDelete(layout, '')
86− return layout.indexOf(name) isnt -1
85+ layout.indexOf(name) isnt -1
8786 catch
88− return false
87+ false
8988
9089 setSize: (value) ->
9190 try
assets/javascripts/views/layout/document.coffee+27−8
@@ -20,6 +20,10 @@ class app.views.Document extends app.View · 19 unmodified lines
2020 @addSubview @content = new app.views.Content
2121 @addSubview @path = new app.views.Path unless app.isSingleDoc() or app.isMobile()
2222
23+ @sidebar.search
24+ .on 'searching', @onSearching
25+ .on 'clear', @onSearchClear
26+
2327 @activate()
2428 return
2529
@@ -39,17 +43,32 @@ class app.views.Document extends app.View · 13 unmodified lines
3943 app.appCache?.updateInBackground()
4044 return
4145
42− toggleSidebar: (saveLayout = true) ->
43− hasHiddenClass = app.el.classList.contains(HIDE_SIDEBAR_CLASS)
44− forceShow = (!hasHiddenClass || !@hasSidebar()) && !saveLayout
45− app.el.classList[if hasHiddenClass or forceShow then 'remove' else 'add'](HIDE_SIDEBAR_CLASS)
46− return unless saveLayout
47− app.settings.setLayout(HIDE_SIDEBAR_CLASS, !hasHiddenClass)
48− app.appCache?.updateInBackground()
46+ showSidebar: (options = {}) ->
47+ @toggleSidebar(options, true)
48+ return
49+
50+ hideSidebar: (options = {}) ->
51+ @toggleSidebar(options, false)
52+ return
53+
54+ toggleSidebar: (options = {}, shouldShow) ->
55+ shouldShow ?= if options.save then !@hasSidebar() else app.el.classList.contains(HIDE_SIDEBAR_CLASS)
56+ app.el.classList[if shouldShow then 'remove' else 'add'](HIDE_SIDEBAR_CLASS)
57+ app.settings.setLayout(HIDE_SIDEBAR_CLASS, !shouldShow) if options.save
4958 return
5059
5160 hasSidebar: ->
52− return !app.el.classList.contains(HIDE_SIDEBAR_CLASS) && !app.settings.hasLayout(HIDE_SIDEBAR_CLASS)
61+ !app.settings.hasLayout(HIDE_SIDEBAR_CLASS)
62+
63+ onSearching: =>
64+ unless @hasSidebar()
65+ @showSidebar()
66+ return
67+
68+ onSearchClear: =>
69+ unless @hasSidebar()
70+ @hideSidebar()
71+ return
5372
5473 setTitle: (title) ->
5574 @el.title = if title then "DevDocs - #{title}" else 'DevDocs API Documentation'
assets/javascripts/views/layout/resizer.coffee+16−12
@@ -14,7 +14,7 @@ class app.views.Resizer extends app.View · 13 unmodified lines
1414 @appendTo $('._app')
1515
1616 @style = $('style[data-resizer]')
17− @size = @style.getAttribute('data-size')
17+ @size = @savedSize = @style.getAttribute('data-size')
1818 return
1919
2020 MIN = 260
@@ -22,18 +22,27 @@ class app.views.Resizer extends app.View · 1 unmodified line
2222
2323 resize: (value, save) ->
2424 value -= app.el.offsetLeft
25− return unless value > 0
26− value = Math.min(Math.max(Math.round(value), MIN), MAX)
27− newSize = "#{value}px"
25+ return unless value >= 0
26+
27+ if value <= 5
28+ app.document.hideSidebar({save})
29+ newSize = @savedSize
30+ else
31+ app.document.showSidebar({save})
32+ value = Math.min(Math.max(Math.round(value), MIN), MAX)
33+ newSize = "#{value}px"
34+
2835 @style.innerHTML = @style.innerHTML.replace(new RegExp(@size, 'g'), newSize)
2936 @size = newSize
37+
3038 if save
31− app.settings.setSize(value)
39+ @savedSize = @size
40+ app.settings.setSize(parseInt(@savedSize))
3241 app.appCache?.updateInBackground()
3342 return
3443
3544 onDblClick: (event) ->
36− app.document.toggleSidebar()
45+ app.document.toggleSidebar(save: true)
3746 return
3847
3948 onDragStart: (event) =>
@@ -55,12 +64,7 @@ class app.views.Resizer extends app.View · 15 unmodified lines
5564 onDragEnd: (event) =>
5665 $.off(window, 'dragover', @onDrag)
5766 value = event.pageX or (event.screenX - window.screenX)
58− if value <= 5
59− app.document.toggleSidebar()
60− return
61− else if !app.document.hasSidebar()
62− app.document.toggleSidebar(true)
63− if @lastDragValue and not (@lastDragValue - 5 < value < @lastDragValue + 5) # https://github.com/Thibaut/devdocs/issues/265
67+ if @lastDragValue and value > 0 and not (@lastDragValue - 5 < value < @lastDragValue + 5) # https://github.com/Thibaut/devdocs/issues/265
6468 value = @lastDragValue
6569 @resize(value, true)
6670 return
assets/javascripts/views/search/search.coffee+0−2
@@ -68,10 +68,8 @@ class app.views.Search extends app.View · 67 unmodified lines
6868 @value = @input.value
6969
7070 if @value.length
71− app.document.toggleSidebar(false)
7271 @search()
7372 else
74− app.document.toggleSidebar() unless app.document.hasSidebar()
7573 @clear()
7674 return
7775