PageSourceSearch

https://docu.ilias.de/assets/js/counter.js?_v=11_4

js ilias.de collected 2026-10-02 02:36:32 UTC 13,573 bytes, 382 lines download raw bytes

1(function () {
2    'use strict';
3
4    var counterFactory = function($){
5        /**
6         * Name of the counter class in the DOM
7         * @private
8         */
9        var _cls_counter = 'il-counter';
10
11        /**
12         * See Interface description
13         */
14        var getCounterObject = function($object_containing_counter){
15            let $counter;
16            $counter = getCounterJquery($object_containing_counter);
17            console.assert($counter.length > 0, "Passed jQuery Object does not contain a counter");
18            return bindCounterJquery($counter);
19        };
20
21        /**
22         * See Interface description
23         */
24        var getCounterObjectOrNull = function($object_containing_counter){
25            let $counter;
26            $counter = getCounterJquery($object_containing_counter);
27            if($counter.length === 0){
28                return null;
29            }
30            return bindCounterJquery($counter);
31        };
32
33        /**
34         * Interface returned by this function for public use (see return statement bellow)
35         * The contained functions are implemented bellow
36         */
37        var public_interface = {
38            /**
39             * Function grasping the $ version of the counter in the DOM,
40             * an generating the public interface of the counter object.
41             *
42             * Note that the return object might contain more than one
43             * counter representation in the DOM. Mutators and queries
44             * will be applied to all the contained representations.
45             */
46            getCounterObject: getCounterObject,
47
48            /**
49             * Same as getCounterObject but allows returning null if
50             * no counter object is present.
51             */
52            getCounterObjectOrNull: getCounterObjectOrNull
53        };
54
55        /**
56         * get the Jquery Object of the counte
57         * @param $object_containing_counter
58         */
59        var getCounterJquery = function($object_containing_counter){
60            console.assert($object_containing_counter instanceof $,
61                "$object_containing_counter is not a jQuery Object, param: "+$object_containing_counter);
62
63            var $counter = $object_containing_counter;
64            if(!$object_containing_counter.hasClass(_cls_counter)){
65                $counter = $object_containing_counter.find("."+_cls_counter);
66            }
67            return $counter;
68        };
69
70        /**
71         * Make sure *this* in generateCounterObject is properly bound.
72         * @param $counter
73         */
74        var bindCounterJquery = function($counter){
75            var CounterObjectConstructor = counterObject.bind({});
76            return CounterObjectConstructor($counter,$);
77        };
78
79        return public_interface;
80    };
81
82    /**
83     * Declaration and implementation of the counter object
84     */
85    var counterObject = function($counter,$){
86        /**
87         * Storing jQuery instance of the DOM of the Counter
88         * in the current scope for internal access.
89         */
90        var $counter = $counter;
91
92        /**
93         * Name of the novelty class in the DOM
94         * @private
95         */
96        var _cls_counter_novelty = 'il-counter-novelty';
97
98        /**
99         * Name of the counter class in the DOM
100         * @private
101         */
102        var _cls_counter_status = 'il-counter-status';
103
104        /**
105         * Constants used to explain failed assertions
106         */
107        const MISSING_COUNTERTYPE_EXCEPTION_MSG = " Counter does not exist in the DOM. " +
108            "Make sure the respective Counter type has been rendered before applying this operations.";
109        const NOT_A_NUMBER_MSG = " is not a number";
110
111        /**
112         * Note, since the following functions will be offered on the public
113         * interface and the returned scope will be bound to a jQuery instance,
114         * we have to bind the functions to this, to set the scope right here.
115         */
116
117        /**
118         * See Interface description
119         */
120        this.getStatusCount = function() {
121            return getCount(getStatusObject($counter))
122        };
123
124        /**
125         * See Interface description
126         */
127        this.getNoveltyCount = function() {
128            return getCount(getNoveltyObject($counter))
129        };
130
131        /**
132         * See Interface description
133         */
134        this.hasNoveltyObject = function(){
135            return getNoveltyObject($counter).length>0;
136        };
137
138        /**
139         * See Interface description
140         */
141        this.hasStatusObject = function(){
142            return getStatusObject($counter).length>0;
143        };
144
145        /**
146         * See Interface description
147         */
148        this.setNoveltyTo = function(value) {
149            console.assert(this.hasNoveltyObject(),"Novelty "+MISSING_COUNTERTYPE_EXCEPTION_MSG);
150            console.assert(typeof value == 'number',value+NOT_A_NUMBER_MSG);
151
152            var $novelty = getNoveltyObject($counter);
153
154            $novelty.html(value);
155            if(value === 0){
156                $novelty.hide();
157            }else {
158                $novelty.show();
159            }
160            return this;
161        };
162
163        /**
164         * See Interface description
165         */
166        this.setStatusTo = function(value) {
167            console.assert(this.hasStatusObject(),"Status "+MISSING_COUNTERTYPE_EXCEPTION_MSG);
168            console.assert(typeof value == 'number',value+NOT_A_NUMBER_MSG);
169
170            var $status = getStatusObject($counter);
171
172            $status.html(value);
173            if(value === 0){
174                $status.hide();
175            }else {
176                $status.show();
177            }
178            return this;
179        };
180
181        /**
182         * See Interface description
183         */
184        this.incrementNoveltyCount = function(value) {
185            console.assert(this.hasNoveltyObject(),"Novelty "+MISSING_COUNTERTYPE_EXCEPTION_MSG);
186            console.assert(typeof value == 'number',value+NOT_A_NUMBER_MSG);
187
188            performWorkOnEachSingleCounter(function(single_counter){
189                if(single_counter.hasNoveltyObject()){
190                    single_counter.setNoveltyTo(single_counter.getNoveltyCount()+value);
191                }
192            },$counter);
193            return this;
194        };
195
196        /**
197         * See Interface description
198         */
199        this.decrementNoveltyCount = function(value) {
200            console.assert(this.hasNoveltyObject(),"Novelty "+MISSING_COUNTERTYPE_EXCEPTION_MSG);
201            console.assert(typeof value == 'number',value+NOT_A_NUMBER_MSG);
202
203            performWorkOnEachSingleCounter(function(single_counter){
204                if(single_counter.hasNoveltyObject()) {
205                    single_counter.setNoveltyTo(single_counter.getNoveltyCount() - value);
206                }
207            },$counter);
208            return this;
209        };
210
211        /**
212         * See Interface description
213         */
214        this.incrementStatusCount = function(value) {
215            console.assert(this.hasStatusObject(),"Status "+MISSING_COUNTERTYPE_EXCEPTION_MSG);
216            console.assert(typeof value == 'number',value+NOT_A_NUMBER_MSG);
217
218            performWorkOnEachSingleCounter(function(single_counter){
219                if(single_counter.hasStatusObject()) {
220                    single_counter.setStatusTo(single_counter.getStatusCount() + value);
221                }
222            },$counter);
223            return this;
224        };
225
226        /**
227         * See Interface description
228         */
229        this.decrementStatusCount = function(value) {
230            console.assert(this.hasStatusObject(),"Status "+MISSING_COUNTERTYPE_EXCEPTION_MSG);
231            console.assert(typeof value == 'number',value+NOT_A_NUMBER_MSG);
232
233            performWorkOnEachSingleCounter(function(single_counter){
234                if(single_counter.hasStatusObject()) {
235                    single_counter.setStatusTo(single_counter.getStatusCount() - value);
236                }
237            },$counter);
238            return this;
239        };
240
241        /**
242         * See Interface description
243         */
244        this.setTotalNoveltyToStatusCount = function() {
245            console.assert(this.hasStatusObject(),"Status "+MISSING_COUNTERTYPE_EXCEPTION_MSG);
246            console.assert(this.hasNoveltyObject(),"Novelty "+MISSING_COUNTERTYPE_EXCEPTION_MSG);
247
248            return this.incrementStatusCount(this.getNoveltyCount()).setNoveltyTo(0);
249        };
250
251        /**
252         * Interface returned by this function for public use (see return statement bellow)
253         * The contained functions are implemented bellow
254         */
255        var public_object_interace = {
256            /**
257             * Gets sum of the count of all Novelty Counter inside the given counter object
258             */
259            getNoveltyCount: this.getNoveltyCount,
260            /**
261             * Gets sum of count of all Status Counter inside the given counter object
262             */
263            getStatusCount: this.getStatusCount,
264            /**
265             * Checks if there is any Novelty Counter inside the counter the given object
266             */
267            hasNoveltyObject: this.hasNoveltyObject,
268            /**
269             * Checks if there is any Status Counter inside the counter the given object
270             */
271            hasStatusObject: this.hasStatusObject,
272            /**
273             * Sets all Novelty Counters inside a given counter object to a set value
274             */
275            setNoveltyTo: this.setNoveltyTo,
276            /**
277             * Sets all Status Counters inside a given counter object to a set value
278             */
279            setStatusTo: this.setStatusTo,
280            /**
281             * Increments all Novelty Counters inside a counter Object by some given value
282             */
283            incrementNoveltyCount: this.incrementNoveltyCount,
284            /**
285             * Decrements all Novelty Counters inside a counter Object by some given value
286             */
287            decrementNoveltyCount: this.decrementNoveltyCount,
288            /**
289             * Increments all Status Counters inside a counter Object by some given value
290             */
291            incrementStatusCount: this.incrementStatusCount,
292            /**
293             * Decrements all Status Counters inside a counter Object by some given value
294             */
295            decrementStatusCount: this.decrementStatusCount,
296            /**
297             * Collects to total sum of all Novelity Counters and increments all Status Counters
298             * by this value
299             */
300            setTotalNoveltyToStatusCount: this.setTotalNoveltyToStatusCount
301        };
302
303        /**
304         * Gets the novelty part of the counter object
305         * @private
306         */
307        var getNoveltyObject = function($parent){
308            return $parent.find("."+_cls_counter_novelty);
309        };
310
311        /**
312         * Gets the status part of the counter object
313         *
314         * @private
315         */
316        var getStatusObject = function($parent){
317            return $parent.find("."+_cls_counter_status);
318        };
319
320        /**
321         * Gets the count of either the status or the novelty part of the counter object.
322         *
323         * @private
324         */
325        var getCount = function($novelty_or_status_object){
326            var sum = 0;
327            $novelty_or_status_object.each(function() {
328                //Note that this in this anonymous function points to the
329                //object in $novelty_or_status_object currently looped
330                var count = $(this).text();
331                sum += parseInt(count);
332            });
333
334            return sum;
335        };
336
337        /**
338         * Helper used for syntactically sugering looping over jQuery elements in the
339         * jQuery representation of the counter. Needed if an operations (workload)
340         * needs to be applied to each element individually.
341         *
342         * @param workload
343         */
344        var performWorkOnEachSingleCounter = function(workload,$parent){
345            //Note that we have to change each DOM counter instance individually.
346            //Therefore we generate a separate counter instance
347            $parent.each(function() {
348                //Note that *this* in this anonymous function points to the
349                //object in counter jquery object currently looped
350                var single_counter = counterFactory($).getCounterObject($(this));
351                workload(single_counter,$(this));
352            });
353        };
354
355        return public_object_interace;
356    };
357
358    il.UI = il.UI || {};
359    /**
360     * Declaration and implementation of the il.UI.counter scope.
361     *
362     * Note that this scope provides a public interface through which counters can
363     * be accessed and manipulated by the client side.
364     *
365     * This scope contains only the getCounterObject through which a counter object can
366     * be accessed.
367     *
368     * See the public_object_interace for a list of functions of this object offered
369     * to the public.
370     *
371     * Example Usage:
372     *
373     * //Step 1: Get the counter Object
374     * var counter = il.UI.counter.getCounterObject($some_jquery_object);
375     *
376     * //Step 2: Do stuff with the counter Object
377     * var novelty_count = counter.setNoveltyCount(3).getNoveltyCount(); //novelty count should be 3
378     * novelty_count = counter.setNoveltyToStatus().getNoveltyCount(); //novelty count should be 0, status count 3
379     */
380    il.UI.counter = counterFactory($);
381
382})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.