Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
Scroll controller
Browse files Browse the repository at this point in the history
  • Loading branch information
philcockfield committed Dec 21, 2011
1 parent 6b7239c commit c241b6c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 13 deletions.
7 changes: 4 additions & 3 deletions lib/src/open.client/controls/controllers/index.coffee
@@ -1,4 +1,5 @@
module.exports =
Focus: require './focus'
Popup: require './popup'
Size: require './size'
Focus: require './focus'
Popup: require './popup'
Scroll: require './scroll'
Size: require './size'
37 changes: 37 additions & 0 deletions lib/src/open.client/controls/controllers/scroll.coffee
@@ -0,0 +1,37 @@
core = require 'open.client/core'


###
Extends a view to make it scrollable.
###
module.exports = class ScrollController
constructor: (view) ->

# Add the scroll property.
unless view.scroll?.onChanged?
view.addProps
scroll: null # Gets or sets the scroll behavior of the content (values: null, 'x', 'y' or 'xy').

# Sycers.
sync = -> core.util.syncScroll view.el, view.scroll()

# Wire up events.
# - Handler: Validate property.
view.scroll.onChanging (e) ->
value = e.newValue
value = value?.toLowerCase()
throw "'#{value}' not valid scroll value (x,y or xy)" if not isValid value
e.newValue = value

# - Handler: Sync state on scroll changed.
view.scroll.onChanged sync

# Finish up.
sync()


# PRIVATE --------------------------------------------------------------------------


isValid = (value) -> return true if value is valid for valid in ['x', 'y', 'xy']

9 changes: 2 additions & 7 deletions lib/src/open.client/modules/harness.tabs/views/base.coffee
Expand Up @@ -7,14 +7,9 @@ module.exports = (module) ->
# Setup initial conditions.
super _.extend props, className:'th_tab'

# Syncers.
syncScroll = => module.core.util.syncScroll @el, @scroll()
# Enabled scrolling.
new module.controls.controllers.Scroll @

# Wire up events.
@scroll.onChanged syncScroll

# Finish up.
syncScroll()


###
Expand Down
76 changes: 76 additions & 0 deletions test/specs/client/controls/controllers/scroll_spec.coffee
@@ -0,0 +1,76 @@
describe 'controls/controllers/scroll', ->
View = null
Scroll = null

class MyView extends core.mvc.View
defaults:
scroll: 'y'

class MyScrollView extends MyView
constructor: (props = {}) ->
super
new Scroll @



beforeEach ->
View = core.mvc.View
Scroll = controls.controllers.Scroll

it 'exists', ->
expect(Scroll).toBeDefined()

it 'adds a [scroll] property function to a view', ->
view = new View()
expect(view.scroll).not.toBeDefined()
new Scroll(view)
expect(view.scroll.onChanged instanceof Function).toEqual true

it 'keeps a pre-defined [scroll] property', ->
view = new MyView()
new Scroll view
expect(view.scroll()).toEqual 'y'


it 'converts property to lower case', ->
view = new MyScrollView()
view.scroll 'XY'
expect(view.scroll()).toEqual 'xy'

it 'throws if a non valid valid is passed', ->
view = new MyScrollView()
view.scroll 'z'


describe 'delegate to syncScroll() method', ->
callArgs = -> core.util.syncScroll.mostRecentCall.args
beforeEach ->

it 'syncs scroll on construction', ->
spyOn(core.util, 'syncScroll')
view = new MyScrollView()
args = callArgs()

expect(core.util.syncScroll.callCount).toEqual 1
expect(args[0]).toEqual view.el
expect(args[1]).toEqual 'y'

it 'syncs on property changed', ->
view = new MyScrollView()

spyOn(core.util, 'syncScroll')
view.scroll 'x'
args = callArgs()

expect(core.util.syncScroll.callCount).toEqual 1
expect(args[0]).toEqual view.el
expect(args[1]).toEqual 'x'









3 changes: 0 additions & 3 deletions test/specs/client/core/util/util_spec.coffee
Expand Up @@ -111,9 +111,6 @@ describe 'util', ->
it 'does not set the target for [mailto:] links', ->
expect(a8.attr('target')).toEqual null





describe 'scrollClasses', ->
div = null
Expand Down

0 comments on commit c241b6c

Please sign in to comment.