aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/oer.js
blob: b6eb37c4bd86ff42494ec26b116eea8329a8850d (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { $gettext } from '../lib/gettext.js';

const OER = {
    periodicalPushData: function () {
        if (jQuery(".comments").length) {
            return {
                'review_id': jQuery("[name=comment]").data("review_id")
            };
        }
    },
    update: function (output) {
        if (output.comments) {
            for (var i = 0; i < output.comments.length; i++) {
                if (jQuery("#comment_" + output.comments[i].comment_id).length === 0) {
                    jQuery(".comments").append(output.comments[i].html).find(":last-child").hide().fadeIn(300);
                }
            }
        }
    },
    requestFullscreen: function (selector) {
        var player = jQuery(selector)[0];
        if (!player) {
            window.alert($gettext('Kein passendes Element für Vollbildmodus.'));
            return;
        }
        if (player.requestFullscreen) {
            player.requestFullscreen();
        } else if (player.msRequestFullscreen) {
            player.msRequestFullscreen();
        } else if (player.mozRequestFullScreen) {
            player.mozRequestFullScreen();
        } else if (player.webkitRequestFullscreen) {
            player.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
        }
    },
    initSearch: function () {
        STUDIP.Vue.load().then(({createApp}) => {
            STUDIP.OER.Search = createApp({
                el: ".oer_search",
                data() {
                    return {
                        browseMode: false,
                        tags: $(".oer_search").data("tags"),
                        tagHistory: [],
                        searchtext: "",
                        activeFilterPanel: false,
                        difficulty: [1, 12],
                        category: null,
                        results: false,
                        material_select_url_template: $(".oer_search").data("material_select_url_template")
                    };
                },
                methods: {
                    sync_search_text: function () {
                        this.searchtext = $(".oer_search input[name=search]").val();
                    },
                    triggerFilterPanel: function () {
                        this.activeFilterPanel = !this.activeFilterPanel;
                    },
                    showFilterPanel: function () {
                        this.activeFilterPanel = true;
                    },
                    hideFilterPanel: function () {
                        this.activeFilterPanel = false;
                    },
                    clearAllFilters: function (keep_results) {
                        this.clearCategory();
                        this.clearDifficulty();
                        if (this.searchtext != '') {
                            this.searchtext = '';
                        }
                        $(".oer_search input[name=search]").val('');
                        if (keep_results !== true) {
                            this.results = false;
                        }
                    },
                    clearDifficulty: function () {
                        if ((this.difficulty[0] != 1) && (this.difficulty[1] != 12)) {
                            this.difficulty = [1, 12];
                        }
                        jQuery("#difficulty_slider").slider("values", this.difficulty);
                    },
                    clearCategory: function () {
                        if (this.category != null) {
                            this.category = null;
                        }
                    },
                    getIconShape: function (result) {
                        if (result.category === "video") {
                            return "video";
                        }
                        if (result.category === "audio") {
                            return "file-audio";
                        }
                        if (result.category === "presentation") {
                            return "file-pdf";
                        }
                        if (result.category === "elearning") {
                            return "learnmodule";
                        }
                        if (result.content_type === "application/zip") {
                            return "archive3";
                        }
                        return "file";
                    },
                    search: function () {
                        let v = this;
                        this.browseMode = false;
                        $.ajax({
                            url: STUDIP.URLHelper.getURL("dispatch.php/oer/market/search"),
                            data: {
                                type: this.category,
                                difficulty: this.difficulty.join(","),
                                search: this.searchtext
                            },
                            dataType: "json",
                            success: function (output) {
                                $("#new_ones").hide();
                                v.results = output.materials;
                                v.activeFilterPanel = false;
                                $(".material_navigation").toggle(v.results.length == 0);
                                $(".mainlist").toggle(v.results.length == 0);
                                $(".new_ones").toggle(v.results.length == 0);
                            }
                        });
                        return false;
                    },
                    browseTag: function (tag_hash, name) {
                        let v = this;
                        this.clearAllFilters(true);
                        let tags = [];
                        for (let i in this.tagHistory) {
                            tags.push(this.tagHistory[i].tag_hash);
                        }
                        if (tag_hash && (tags.indexOf(tag_hash) === -1)) {
                            tags.push(tag_hash);
                        }
                        let p = new Promise(function (resolve, reject) {
                            $.ajax({
                                url: STUDIP.URLHelper.getURL("dispatch.php/oer/market/get_tags"),
                                data: {
                                    tags: tags
                                },
                                dataType: "json",
                                success: function (output) {
                                    v.results = output.results.materials;
                                    v.tags = output.tags;
                                    if (tag_hash) {
                                        v.tagHistory.push({
                                            tag_hash: tag_hash,
                                            name: name
                                        });
                                    }
                                    if (v.tagHistory.length > 0) {
                                        $("#new_ones").hide();
                                    }
                                    resolve();
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    reject(new Error(errorThrown));
                                }
                            });
                        });
                        return p;
                    },
                    backInCloud: function () {
                        this.tagHistory.pop();
                        let tag_hash = null;
                        let tag_name = null;
                        if (this.tagHistory.length > 0) {
                            tag_hash = this.tagHistory[this.tagHistory.length - 1].tag_hash;
                            tag_name = this.tagHistory[this.tagHistory.length - 1].name;
                        }
                        let v = this;
                        this.tagHistory.pop();
                        this.browseTag(tag_hash, tag_name).then(function () {
                            if (v.tagHistory.length === 0) {
                                $("#new_ones").show();
                            }
                        });

                    },
                    getTagStyle: function (tag_hash) {
                        return "position: relative; top: " + Math.floor(Math.random() * 15 - 15) + "px";
                    },
                    capitalizeFirstLetter: function (string) {
                        return string.charAt(0).toUpperCase() + string.slice(1);
                    },
                    getMaterialURL: function (material_id) {
                        return this.material_select_url_template.replace("__material_id__", material_id);
                    },
                    shortenName: function (name) {
                        if (name.length > 55) {
                            return name.substring(0, 50) + ' ...';
                        } else {
                            return name;
                        }
                    }
                },
                mounted: function () {
                    this.results = $(this.$el).data('searchresults');
                    if (this.results !== false) {
                        $("#new_ones").hide();
                    }
                    if ($(this.$el).data('filteredcategory')) {
                        this.category = $(this.$el).data('filteredcategory');
                    }
                },
                updated: function () {
                    this.$nextTick(function () {
                        if (!jQuery("#difficulty_slider.ui-slider").length) { //to prevent an endless loop
                            let v = this;
                            jQuery("#difficulty_slider").slider({
                                range: true,
                                min: 1,
                                max: 12,
                                values: [v.difficulty[0], v.difficulty[1]],
                                change: function (event, ui) {
                                    v.difficulty = ui.values;
                                }
                            });
                        }
                    });
                }
            });
        });


        jQuery(document).on("click", function (evnt) {
            if (!jQuery(evnt.target).is(".searchform *") && STUDIP.OER.Search) {
                STUDIP.OER.Search.hideFilterPanel();
            }
        });

    }
};


export default OER;