scroller.js 7.91 KB
Newer Older
1
(function (requirejs, require, define) {
2
define([], function () {
3 4 5
    return Scroller;

    function Scroller(state) {
6
        var parentEl, moveLeftEl, showEl, moveRightEl, showElLeftMargin;
7 8 9 10

        parentEl = $(
            '<div ' +
                'style=" ' +
11
                    'width: 665px; ' +
12
                    'height: 102px; ' +
13 14 15 16 17 18 19 20 21
                    'margin-left: auto; ' +
                    'margin-right: auto; ' +
                '" ' +
            '></div>'
        );

        moveLeftEl = $(
            '<div ' +
                'style=" ' +
22
                    'width: 40px; ' +
23
                    'height: 102px; ' +
24 25 26
                    'display: inline; ' +
                    'float: left; ' +
                '" ' +
27 28 29
            '>' +
                '<div ' +
                    'style=" ' +
30 31
                        'width: 38px; ' +
                        'height: 100px; '+
32 33 34 35 36 37 38 39 40 41 42

                        'border: 1px solid #CCC; ' +
                        'background-color: #EEE; ' +
                        'background-image: -webkit-linear-gradient(top, #EEE, #DDD); ' +
                        'background-image: -moz-linear-gradient(top, #EEE, #DDD); ' +
                        'background-image: -ms-linear-gradient(top, #EEE, #DDD); ' +
                        'background-image: -o-linear-gradient(top, #EEE, #DDD); ' +
                        'background-image: linear-gradient(top, #EEE, #DDD); ' +
                        '-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset; ' +
                        'box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset; ' +

43
                        "background-image: url('"+baseUrl+"images/arrow-left.png'); " +
44 45 46 47 48
                        'background-position: center center; ' +
                        'background-repeat: no-repeat; ' +
                    '" ' +
                '></div>' +
            '</div>'
49 50 51
        );
        moveLeftEl.appendTo(parentEl);

52 53 54 55 56 57 58
        // The below is necessary to prevent the browser thinking that we want
        // to perform a drag operation, or a highlight operation. If we don't
        // do this, the browser will then highlight with a gray shade the
        // element.
        moveLeftEl.mousemove(function (event) { event.preventDefault(); });
        moveLeftEl.mousedown(function (event) { event.preventDefault(); });

59 60
        // This event will be responsible for moving the scroller left.
        // Hidden draggables will be shown.
61 62 63
        moveLeftEl.mouseup(function (event) {
            event.preventDefault();

64 65
            // When there are no more hidden draggables, prevent from
            // scrolling infinitely.
66 67 68 69 70
            if (showElLeftMargin > -102) {
                return;
            }

            showElLeftMargin += 102;
71 72

            // We scroll by changing the 'margin-left' CSS property smoothly.
73
            state.sliderEl.animate({
74
                'margin-left': showElLeftMargin + 'px'
75
            }, 100, function () {
76
                updateArrowOpacity();
77
            });
78 79 80 81 82
        });

        showEl = $(
            '<div ' +
                'style=" ' +
83
                    'width: 585px; ' +
84
                    'height: 102px; ' +
85 86 87 88 89 90 91 92
                    'overflow: hidden; ' +
                    'display: inline; ' +
                    'float: left; ' +
                '" ' +
            '></div>'
        );
        showEl.appendTo(parentEl);

93
        showElLeftMargin = 0;
94

95 96 97 98
        // Element where the draggables will be contained. It is very long
        // so that any SANE number of draggables will fit in a single row. It
        // will be contained in a parent element whose 'overflow' CSS value
        // will be hidden, preventing the long row from fully being visible.
99 100 101
        state.sliderEl = $(
            '<div ' +
                'style=" ' +
102
                    'width: 20000px; ' +
103 104 105
                    'height: 100px; ' +
                    'border-top: 1px solid #CCC; ' +
                    'border-bottom: 1px solid #CCC; ' +
106 107 108 109
                '" ' +
            '></div>'
        );
        state.sliderEl.appendTo(showEl);
110 111 112 113

        state.sliderEl.mousedown(function (event) {
            event.preventDefault();
        });
114 115 116 117

        moveRightEl = $(
            '<div ' +
                'style=" ' +
118
                    'width: 40px; ' +
119
                    'height: 102px; ' +
120 121 122
                    'display: inline; ' +
                    'float: left; ' +
                '" ' +
123 124 125
            '>' +
                '<div ' +
                    'style=" ' +
126 127
                        'width: 38px; ' +
                        'height: 100px; '+
128 129 130 131 132 133 134 135 136 137 138

                        'border: 1px solid #CCC; ' +
                        'background-color: #EEE; ' +
                        'background-image: -webkit-linear-gradient(top, #EEE, #DDD); ' +
                        'background-image: -moz-linear-gradient(top, #EEE, #DDD); ' +
                        'background-image: -ms-linear-gradient(top, #EEE, #DDD); ' +
                        'background-image: -o-linear-gradient(top, #EEE, #DDD); ' +
                        'background-image: linear-gradient(top, #EEE, #DDD); ' +
                        '-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset; ' +
                        'box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset; ' +

139
                        "background-image: url('"+baseUrl+"images/arrow-right.png'); " +
140 141 142 143 144
                        'background-position: center center; ' +
                        'background-repeat: no-repeat; ' +
                    '" ' +
                '></div>' +
            '</div>'
145 146 147
        );
        moveRightEl.appendTo(parentEl);

148 149 150 151 152 153 154
        // The below is necessary to prevent the browser thinking that we want
        // to perform a drag operation, or a highlight operation. If we don't
        // do this, the browser will then highlight with a gray shade the
        // element.
        moveRightEl.mousemove(function (event) { event.preventDefault(); });
        moveRightEl.mousedown(function (event) { event.preventDefault(); });

155 156
        // This event will be responsible for moving the scroller right.
        // Hidden draggables will be shown.
157 158 159
        moveRightEl.mouseup(function (event) {
            event.preventDefault();

160 161
            // When there are no more hidden draggables, prevent from
            // scrolling infinitely.
162
            if (showElLeftMargin < -102 * (state.numDraggablesInSlider - 6)) {
163 164 165 166
                return;
            }

            showElLeftMargin -= 102;
167

168
            // We scroll by changing the 'margin-left' CSS property smoothly.
169
            state.sliderEl.animate({
170
                'margin-left': showElLeftMargin + 'px'
171
            }, 100, function () {
172
                updateArrowOpacity();
173
            });
174 175 176
        });

        parentEl.appendTo(state.containerEl);
177

178 179 180 181 182 183 184 185 186 187
        // Make the function available throughout the application. We need to
        // call it in several places:
        //
        // 1.) When initially reading answer from server, if draggables will be
        // positioned on the base image, the scroller's right and left arrows
        // opacity must be updated.
        //
        // 2.) When creating draggable elements, the scroller's right and left
        // arrows opacity must be updated according to the number of
        // draggables.
188 189 190 191 192 193 194 195
        state.updateArrowOpacity = updateArrowOpacity;

        return;

        function updateArrowOpacity() {
            moveLeftEl.children('div').css('opacity', '1');
            moveRightEl.children('div').css('opacity', '1');

196
            if (showElLeftMargin < -102 * (state.numDraggablesInSlider - 6)) {
197 198 199 200 201 202 203
                moveRightEl.children('div').css('opacity', '.4');
            }
            if (showElLeftMargin > -102) {
                moveLeftEl.children('div').css('opacity', '.4');
            }
        }
    } // End-of: function Scroller(state)
204
}); // End-of: define([], function () {
205
}(RequireJS.requirejs, RequireJS.require, RequireJS.define)); // End-of: (function (requirejs, require, define) {