From 1099ebc7735338bec32bda4a445fd78444d8e3d9 Mon Sep 17 00:00:00 2001 From: Salvatore Iovene Date: Mon, 29 Apr 2013 10:57:16 +0300 Subject: [PATCH] Add simple-scrollable back. --- grunt.js | 1 - src/javascripts/cowhide-simple-scrollable.js | 81 ++++++++++++++++++++++++++++ tests/index.html | 2 + tests/unit/cowhide-simple-scrollable.js | 14 +++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/javascripts/cowhide-simple-scrollable.js create mode 100644 tests/unit/cowhide-simple-scrollable.js diff --git a/grunt.js b/grunt.js index 27aa92f..fe8175b 100644 --- a/grunt.js +++ b/grunt.js @@ -122,7 +122,6 @@ module.exports = function(grunt) { 'src/javascripts/cowhide-select.js', 'src/javascripts/cowhide-page.js', 'src/javascripts/cowhide-header.js', - 'src/javascripts/cowhide-scrollable.js', 'src/javascripts/cowhide-simple-scrollable.js' ], dest: 'dist/cowhide.js' }, diff --git a/src/javascripts/cowhide-simple-scrollable.js b/src/javascripts/cowhide-simple-scrollable.js new file mode 100644 index 0000000..fe02a6d --- /dev/null +++ b/src/javascripts/cowhide-simple-scrollable.js @@ -0,0 +1,81 @@ +/* vi: set et sw=4 ts=4 si: */ +(function($, undefined) { + 'use strict'; + + var ChSimpleScrollable = function(element, options) { + $.fn.ch_widget.Constructor(element, options); + this.$element = $(element); + this.options = $.extend( + options, + $.fn.ch_widget.defaults, + { + }); + }; + + ChSimpleScrollable.prototype = $.extend( + {}, + $.fn.ch_widget.Constructor.prototype, + { + constructor: ChSimpleScrollable, + + enable: function() { + var self = this, + $this = self.$element, + $up = $('
').addClass('ch-simple-scrollable-up'), + $dn = $('
').addClass('ch-simple-scrollable-dn'), + $child = $this.find('ul, ol, div, p'), + scrollAmount; + + $child.addClass('ch-simple-scrollable-content'); + $child.height($child.parent().height() - 160); + scrollAmount = $child.height() - 40; + + + $up.css({top: $child.offset().top}); + + $up.html(''); + $dn.html(''); + + $dn.click(function() { + $child.animate({ + scrollTop: $child.scrollTop() + scrollAmount + }, 200); + }); + + $up.click(function() { + $child.animate({ + scrollTop: $child.scrollTop() - scrollAmount + }, 200); + }); + + + $up.insertBefore($child); + $dn.insertAfter($child); + } + } + ); + + $.fn.ch_simple_scrollable = function(option) { + return this.each(function() { + var $this = $(this), + data = $this.data('ch_simple_scrollable'), + options = typeof option == 'object' && option; + + if (!data) { + $this.data('ch_simple_scrollable', (data = new ChSimpleScrollable(this, options))); + data.register(); + } + + if(typeof option == 'string') + data[option](); + }); + }; + + $.fn.ch_simple_scrollable.Constructor = ChSimpleScrollable; + + /* CHSIMPLESCROLLABLE DATA-API + * ================= */ + $(function() { + $('div.ch-simple-scrollable').ch_simple_scrollable('enable'); + }) +})(window.jQuery); diff --git a/tests/index.html b/tests/index.html index 276baca..4be06fe 100644 --- a/tests/index.html +++ b/tests/index.html @@ -40,6 +40,7 @@ + @@ -50,6 +51,7 @@ +
diff --git a/tests/unit/cowhide-simple-scrollable.js b/tests/unit/cowhide-simple-scrollable.js new file mode 100644 index 0000000..8acdbe4 --- /dev/null +++ b/tests/unit/cowhide-simple-scrollable.js @@ -0,0 +1,14 @@ +$(function () { + + module("cowhide-simple-scrollable") + + test("widget has been made scrollable", function () { + var page = $('
') + var scrollable = $('

Test

') + + scrollable.appendTo(page) + scrollable.ch_simple_scrollable('enable') + + ok(scrollable.find('.ch-simple-scrollable-content').length > 0, "element has ch-simple-scrollable-content child") + }) +}) -- 2.7.4