state.js 2.88 KB
Newer Older
1 2 3
(function(requirejs, require, define) {
    define([], function() {
        return State;
4

5 6
        function State(problemId) {
            var state;
7

8 9
            state = {
                'config': null,
10

11 12
                'baseImageEl': null,
                'baseImageLoaded': false,
13

14
                'containerEl': null,
15

16
                'sliderEl': null,
17

18
                'problemId': problemId,
19

20 21 22
                'draggables': [],
                'numDraggablesInSlider': 0,
                'currentMovingDraggable': null,
23

24
                'targets': [],
25

26
                'updateArrowOpacity': null,
27

28 29
                'uniqueId': 0,
                'salt': makeSalt(),
30

31 32
                'getUniqueId': getUniqueId
            };
33

34 35 36
            $(document).mousemove(function(event) {
                documentMouseMove(state, event);
            });
37

38 39
            return state;
        }
40

41 42
        function getUniqueId() {
            this.uniqueId += 1;
43

44 45
            return this.salt + '_' + this.uniqueId.toFixed(0);
        }
46

47 48
        function makeSalt() {
            var text, possible, i;
49

50 51
            text = '';
            possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
52

53 54 55
            for (i = 0; i < 5; i += 1) {
                text += possible.charAt(Math.floor(Math.random() * possible.length));
            }
56

57 58
            return text;
        }
59

60 61 62
        function documentMouseMove(state, event) {
            if (state.currentMovingDraggable !== null) {
                state.currentMovingDraggable.iconEl.css(
63 64 65 66 67 68
                'left',
                event.pageX -
                    state.baseImageEl.offset().left -
                    state.currentMovingDraggable.iconWidth * 0.5
                    - state.currentMovingDraggable.iconElLeftOffset
            );
69
                state.currentMovingDraggable.iconEl.css(
70 71 72 73 74 75
                'top',
                event.pageY -
                    state.baseImageEl.offset().top -
                    state.currentMovingDraggable.iconHeight * 0.5
            );

76 77
                if (state.currentMovingDraggable.labelEl !== null) {
                    state.currentMovingDraggable.labelEl.css(
78 79 80 81 82 83
                    'left',
                    event.pageX -
                        state.baseImageEl.offset().left -
                        state.currentMovingDraggable.labelWidth * 0.5
                        - 9 // Account for padding, border.
                );
84
                    state.currentMovingDraggable.labelEl.css(
85 86 87 88 89 90
                    'top',
                    event.pageY -
                        state.baseImageEl.offset().top +
                        state.currentMovingDraggable.iconHeight * 0.5 +
                        5
                );
91
                }
92 93
            }
        }
94
    }); // End-of: define([], function () {
95
}(RequireJS.requirejs, RequireJS.require, RequireJS.define)); // End-of: (function (requirejs, require, define) {