PageSourceSearch

https://cobiss.si/js/services/news-viewer.js?v=20190426

js cobiss.si collected 2026-10-02 06:06:09 UTC 5,723 bytes, 237 lines download raw bytes

1/**
2 * news-viewer v 0.0.3.12
3 *
4 */
5
6(function() {
7	'use strict';
8
9	var SERVICE_URI = '/homerest/';
10
11	if (!jQuery){
12		throw new Error('jQuery required!');
13	}
14
15	var scrpt = document.currentScript || document.scripts[document.scripts.length - 1];
16	if (!scrpt.dataset.cnid){
17		throw new Error('Unable to load api. Mising parameter cnid!');
18	}
19
20	var site = {
21			key : scrpt.dataset.cnid,
22			lang : scrpt.dataset.lang.toUpperCase() || 'SLV',
23			script : scrpt.dataset.script.toUpperCase() || 'LATIN',
24			culture : scrpt.dataset.culture || 'sl'
25		};
26
27	function resolveText(news, options){
28		try {
29			var text = news.translations[site.lang][site.script];
30
31			if (options.stripHTML){
32				var div = document.createElement('div');
33				div.innerHTML = text;
34
35				text = div.textContent; // samo golo besedilo
36				if ((options.textLength > 0) && (text.length > options.textLength)){
37					text = text.substr(0, options.textLength) + ' ...';
38				}
39			}
40		}
41		catch (e) {
42			 console.error('No text in news object id = ' + news.newsId + ' for language ' + site.lang + ' - ' + site.script);
43		}
44
45		return text;
46	}
47
48	function convertData(options, data){
49		if (!options.format){
50			throw new Error('No format to return!');
51		}
52
53		var format = options.format;
54
55		if (!data){
56			throw new Error('No data to format');
57		}
58
59		var htmlData = '';
60		for (var idx in data){
61			try {
62				var item = data[idx],
63					text = resolveText(item, options),
64					date = new Date(item.published);
65
66				var s = format;
67				s = s.replace('[text]', text);
68				s = s.replace('[date]', date.toLocaleDateString(site.culture));
69				s = s.replace('[id]', item.newsId);
70
71				htmlData += s;
72			}
73			catch (e) { console.log(e); }
74		}
75
76		return htmlData;
77	}
78
79	function fetchNews(options) {
80		if (!options.max){
81			throw new Error('Latest news requires max option which is missing!');
82		}
83		if (!options.format){
84			throw new Error('Latest news requires format option which is missing!');
85		}
86		if (!options.success  || typeof options.success !== 'function'){
87			throw new Error('Latest news requires success callback function which is missing!');
88		}
89		if (!options.error || typeof options.error !== 'function'){
90			throw new Error('Latest news requires error callback function which is missing!');
91		}
92		// default nastavitve
93		if (!options.stripHTML){
94			options.stripHTML = false;
95		}
96		if (!options.textLength){
97			options.textLength = 0;
98		}
99
100		var storageKey = 'news/' + site.key + '/' + (options.max || '10');
101		if (sessionStorage){
102			var data = JSON.parse(sessionStorage.getItem(storageKey));
103			if (data !== null){
104				var htmlData = convertData(options, data);
105
106				options.success(htmlData);
107				return;
108			}
109		}
110
111		var ajaxOpt = {
112				url: SERVICE_URI + storageKey,
113				type : 'GET',
114				success : function(data){
115					if (sessionStorage){
116						sessionStorage.setItem(storageKey, JSON.stringify(data));
117					}
118
119					var htmlData = convertData(options, data);
120
121					options.success(htmlData);
122				},
123				error : function(xhr){
124					options.error(xhr.statusText);
125				}
126		};
127
128		$.ajax(ajaxOpt);
129	}
130
131	function fetchFullArchive(options){
132		if (!options){
133			throw new Error('No options specified!');
134		}
135
136		if (!options.success || (typeof options.success !== 'function')){
137			throw new Error('No success callback function specified');
138		}
139
140		if (!options.error || (typeof options.error !== 'function')){
141			throw new Error('No error callback function specified');
142		}
143
144		jQuery(document).trigger('news-archive-begin-reading');
145
146		getNewsArchiveData(function(data){
147
148
149				options.success (data, site.lang, site.script, site.culture);
150
151				jQuery(document).trigger('news-archive-finish-reading');
152		});
153
154	}
155
156	function getNewsArchiveData(callback){
157		var storageKey = 'news/archive/' + site.key;
158		if (sessionStorage){
159			var data = JSON.parse(sessionStorage.getItem(storageKey));
160			if (data !== null){
161				callback(data);
162				return;
163			}
164		}
165
166		/* LOAD FULL ARCHIVE */
167		jQuery(document).on('news-archive-loaded', function(event, data){
168
169			if (sessionStorage){
170				var jsonData = JSON.stringify(data);
171				sessionStorage.setItem(storageKey, jsonData);
172			}
173
174			callback(data);
175		});
176
177		loadNewsArchive();
178	}
179
180	var fullArchive = {};
181
182	function loadNewsArchive(years){
183		var ServiceEndpoint = SERVICE_URI + 'news/archive/' + site.key;
184		var year = undefined;
185
186		if (years === undefined){
187			// first call for archive retrieval
188			fullArchive = {};
189			fullArchive.archive = fullArchive.archive || {};
190		}else {
191			// retrieve archive for specified year
192			year = years.shift();
193
194			if (year === undefined){
195				jQuery(document).trigger('news-archive-loaded', [fullArchive]);
196				return;
197			}
198		}
199
200		// klic servisa za določeno leto - nastavi končni klic servisa
201		if (year !== undefined){
202			ServiceEndpoint += '/' + year;
203		}
204
205		var ajaxOpt = {
206			url: ServiceEndpoint,
207			type : 'GET',
208			success : function(ajaxData){
209				if (years === undefined){
210					var _years = ajaxData.years.slice();
211					fullArchive = ajaxData;
212
213					var initLoadedYear = _years.shift(); // odstrani prvo leto, saj je ravnokar naloženo
214					years = _years;
215				}else {
216					fullArchive.archive[year] = ajaxData.archive[year];
217				}
218
219				loadNewsArchive(years);
220				return;
221			},
222			error : function(xhr){
223				console.error('failed to get ARCHIVE!');
224				return;
225			}
226		};
227
228		jQuery.ajax(ajaxOpt);
229	}
230
231	window.COBISS = window.COBISS || {};
232	window.COBISS.news = window.COBISS.news || {
233		latest : fetchNews,
234		archive : fetchFullArchive
235	};
236
237})();

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.