File size: 6,943 Bytes
2fcec7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
define(['jquery', './constants', './pubsub'], function ($, constants, pubSub) {
    'use strict';

    /* Private variables */
    var _dialogQueue = [];
    var _isDialogShown = false;

    var UiManager = function () {};
    UiManager.prototype = {
        init: function () {
            constants.JQ_TERMINAL.resizable({
                handles: 'n, e, w, ne, nw',
                start: function () {
                    constants.JQ_TERMINAL.addClass('resizing');
                },
                stop: function () {
                    constants.JQ_TERMINAL.removeClass('resizing');
                    constants.JQ_TERMINAL.css('top', '');
                    constants.JQ_TERMINAL.css('left', '');
                    constants.JQ_TERMINAL.css('bottom', '5px');
                    constants.JQ_TERMINAL.css('right', '1px');
                }
            });
            constants.JQ_TERMINAL_TOGGLE.button({
                icons: {
                    primary: 'ui-icon-power'
                },
                text: false
            });
            constants.JQ_MENU_BUTTON.button({
                icons: {
                    primary: 'ui-icon-gear'
                },
                text: false
            });
            constants.JQ_TERMINAL.hide();
            constants.JQ_MENU.mCustomScrollbar();

            /* PubSub */
            var that = this;
            pubSub.subscribe('UI/alert', function (evt, text, optTitle) {
                that.showDialog(optTitle || 'Message', text, [constants.Buttons.getDefaultCloseButton()]);
            });
            pubSub.subscribe('UI/dialog', function (evt, title, text, buttons, optParams) {
                that.showDialog(title, text, buttons, optParams);
            });
            pubSub.subscribe('UI/talk', function (evt, title, personName, text, buttons, optParams) {
                that.showPersonTalking(title, personName, text, buttons, optParams);
            });
        },

        showDialog: function (title, text, buttons, optParams) {
            var thisUiManager = this;
            if(_isDialogShown) {
                _dialogQueue.push(function () {
                    thisUiManager.showDialog(title, text, buttons, optParams);
                })
                return;
            }
            _isDialogShown = true;
            pubSub.publish('Dialog/open');
            constants.JQ_MSGDIALOG.removeClass('dialogHidden');
            constants.JQ_MSGDIALOG.html(text);
            var defaultParams = {
                title: title || 'Message',
                modal: true,
                show: 'drop',
                hide: 'drop',
                width: Math.min(400, window.innerWidth - 20),
                height: Math.min(400, window.innerHeight - 20),
                maxWidth: window.innerWidth - 20,
                maxHeight: window.innerHeight - 20,
                buttons: buttons || [constants.Buttons.getDefaultCloseButton()],
                open: function () {
                    pubSub.publish('AudioManager/playSound', [constants.Sound.DIALOG_SHOW]);
                    thisUiManager.applyCustomStyle();
                },
                beforeClose: function () {
                    pubSub.publish('AudioManager/playSound', [constants.Sound.DIALOG_SHOW]);
                    pubSub.publish('Dialog/close');
                },
                close: function() {
                    _isDialogShown = false;
                    if(_dialogQueue.length > 0) {
                        var nextDialog = _dialogQueue.shift();
                        nextDialog();
                    }
                }
            };
            if(optParams && typeof optParams === 'object') {
                var appendOptFunc = function(baseFunc, optFunc) {
                    return function() {
                        baseFunc();
                        optFunc();
                    };
                };
                for(var p in optParams) {
                    if(typeof defaultParams[p] === 'function') {
                        defaultParams[p] = appendOptFunc(defaultParams[p], optParams[p]);
                    } else {
                        defaultParams[p] = optParams[p];
                    }
                }
            }
            constants.JQ_MSGDIALOG.dialog(defaultParams).mCustomScrollbar();
        },

        showPersonTalking: function (title, personName, text, buttons, optParams) {
            this.showDialog(title, '<h3 class="personTalking">' + personName + ' says:</h3><blockquote>' + text + '</blockquote>', buttons, optParams);
        },

        showMenu: function () {
            var thisUiManager = this;
            pubSub.publish('Dialog/open');
            constants.JQ_MENU.dialog({
                title: 'Menu',
                width: Math.min(500, window.innerWidth - 20),
                height: Math.min(450, window.innerHeight - 20),
                maxWidth: window.innerWidth - 20,
                maxHeight: window.innerHeight - 20,
                modal: true,
                show: 'drop',
                hide: 'drop',
                open: function () {
                    pubSub.publish('AudioManager/playSound', [constants.Sound.DIALOG_SHOW]);
                    thisUiManager.applyCustomStyle();
                },
                beforeClose: function () {
                    pubSub.publish('AudioManager/playSound', [constants.Sound.DIALOG_SHOW]);
                    pubSub.publish('Dialog/close');
                }
            });
        },

        applyCustomStyle: function () {
            constants.getJQ_HELP_BUTTONS().button({
                icons: {
                    primary: 'ui-icon-help'
                }
            }).click(function () {
                pubSub.publish('AudioManager/playSound', [constants.Sound.DIALOG_BUTTON]);
            });
            constants.getJQ_LINKS().button({
                icons: {
                    primary: 'ui-icon-extlink'
                }
            }).click(function () {
                pubSub.publish('AudioManager/playSound', [constants.Sound.DIALOG_BUTTON]);
            });
            constants.getJQ_ACCORDIONS().accordion({
                active: false,
                activate: function (evt, ui) {
                    $(evt.target).resize();
                    if(ui.newHeader.hasClass('spoiler')) {
                        pubSub.publish('AchievementManager/openSpoiler', [10]);
                    }
                },
                beforeActivate: function () {
                    pubSub.publish('AudioManager/playSound', [constants.Sound.ACCORDION]);
                },
                collapsible: true,
                heightStyle: 'content',
                icons: {
                    'header': 'ui-icon-circle-triangle-e',
                    'headerSelected': 'ui-icon-circle-triangle-s'
                }
            });
        }
    };
    return UiManager;
});