scroller.js 8.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Wrapper for RequireJS. It will make the standard requirejs(), require(), and
// define() functions from Require JS available inside the anonymous function.
//
// See https://edx-wiki.atlassian.net/wiki/display/LMS/Integration+of+Require+JS+into+the+system
(function (requirejs, require, define) {

define(['logme'], function (logme) {
    return Scroller;

    function Scroller(state) {
11
        var parentEl, moveLeftEl, showEl, moveRightEl, showElLeftMargin;
12 13 14 15

        parentEl = $(
            '<div ' +
                'style=" ' +
16
                    'width: 665px; ' +
17
                    'height: 102px; ' +
18 19 20 21 22 23 24 25 26
                    'margin-left: auto; ' +
                    'margin-right: auto; ' +
                '" ' +
            '></div>'
        );

        moveLeftEl = $(
            '<div ' +
                'style=" ' +
27
                    'width: 40px; ' +
28
                    'height: 102px; ' +
29 30 31
                    'display: inline; ' +
                    'float: left; ' +
                '" ' +
32 33 34
            '>' +
                '<div ' +
                    'style=" ' +
35 36
                        'width: 38px; ' +
                        'height: 100px; '+
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

                        '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; ' +

                        'background-image: url(\'/static/images/arrow-left.png\'); ' +
                        'background-position: center center; ' +
                        'background-repeat: no-repeat; ' +
                    '" ' +
                '></div>' +
            '</div>'
54 55 56
        );
        moveLeftEl.appendTo(parentEl);

57 58 59 60 61 62 63
        // 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(); });

64 65
        // This event will be responsible for moving the scroller left.
        // Hidden draggables will be shown.
66 67 68
        moveLeftEl.mouseup(function (event) {
            event.preventDefault();

69 70
            // When there are no more hidden draggables, prevent from
            // scrolling infinitely.
71 72 73 74 75
            if (showElLeftMargin > -102) {
                return;
            }

            showElLeftMargin += 102;
76 77

            // We scroll by changing the 'margin-left' CSS property smoothly.
78
            state.sliderEl.animate({
79
                'margin-left': showElLeftMargin + 'px'
80
            }, 100, function () {
81
                updateArrowOpacity();
82
            });
83 84 85 86 87
        });

        showEl = $(
            '<div ' +
                'style=" ' +
88
                    'width: 585px; ' +
89
                    'height: 102px; ' +
90 91 92 93 94 95 96 97
                    'overflow: hidden; ' +
                    'display: inline; ' +
                    'float: left; ' +
                '" ' +
            '></div>'
        );
        showEl.appendTo(parentEl);

98
        showElLeftMargin = 0;
99

100 101 102 103
        // 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.
104 105 106
        state.sliderEl = $(
            '<div ' +
                'style=" ' +
107
                    'width: 20000px; ' +
108 109 110
                    'height: 100px; ' +
                    'border-top: 1px solid #CCC; ' +
                    'border-bottom: 1px solid #CCC; ' +
111 112 113 114
                '" ' +
            '></div>'
        );
        state.sliderEl.appendTo(showEl);
115 116 117 118

        state.sliderEl.mousedown(function (event) {
            event.preventDefault();
        });
119 120 121 122

        moveRightEl = $(
            '<div ' +
                'style=" ' +
123
                    'width: 40px; ' +
124
                    'height: 102px; ' +
125 126 127
                    'display: inline; ' +
                    'float: left; ' +
                '" ' +
128 129 130
            '>' +
                '<div ' +
                    'style=" ' +
131 132
                        'width: 38px; ' +
                        'height: 100px; '+
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

                        '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; ' +

                        'background-image: url(\'/static/images/arrow-right.png\'); ' +
                        'background-position: center center; ' +
                        'background-repeat: no-repeat; ' +
                    '" ' +
                '></div>' +
            '</div>'
150 151 152
        );
        moveRightEl.appendTo(parentEl);

153 154 155 156 157 158 159
        // 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(); });

160 161
        // This event will be responsible for moving the scroller right.
        // Hidden draggables will be shown.
162 163 164
        moveRightEl.mouseup(function (event) {
            event.preventDefault();

165 166
            // When there are no more hidden draggables, prevent from
            // scrolling infinitely.
167
            if (showElLeftMargin < -102 * (state.numDraggablesInSlider - 6)) {
168 169 170 171
                return;
            }

            showElLeftMargin -= 102;
172

173
            // We scroll by changing the 'margin-left' CSS property smoothly.
174
            state.sliderEl.animate({
175
                'margin-left': showElLeftMargin + 'px'
176
            }, 100, function () {
177
                updateArrowOpacity();
178
            });
179 180 181
        });

        parentEl.appendTo(state.containerEl);
182

183 184 185 186 187 188 189 190 191 192
        // 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.
193 194 195 196 197 198 199 200
        state.updateArrowOpacity = updateArrowOpacity;

        return;

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

201
            if (showElLeftMargin < -102 * (state.numDraggablesInSlider - 6)) {
202 203 204 205 206 207 208
                moveRightEl.children('div').css('opacity', '.4');
            }
            if (showElLeftMargin > -102) {
                moveLeftEl.children('div').css('opacity', '.4');
            }
        }
    } // End-of: function Scroller(state)
209 210 211 212 213 214 215
});

// End of wrapper for RequireJS. As you can see, we are passing
// namespaced Require JS variables to an anonymous function. Within
// it, you can use the standard requirejs(), require(), and define()
// functions as if they were in the global namespace.
}(RequireJS.requirejs, RequireJS.require, RequireJS.define)); // End-of: (function (requirejs, require, define)