PageSourceSearch

https://sy.co.uk/app/components/home/home.controller.js

js sy.co.uk collected 2026-10-09 23:44:32 UTC 12,406 bytes, 465 lines download raw bytes

1HomeController.$inject = ["$rootScope", "$timeout", "$scope", "$location", "gServices", "dbServices", "$http", "$state", "server", "$window", "messaging", "$sce", "homeSections"];
2
3function HomeController($rootScope, $timeout, $scope, $location, gServices, dbServices, $http, $state, server, $window, messaging, $sce, homeSections) {
4	vm = this;
5
6	var gv = gServices.gVars;
7	vm.gv = gv;
8	vm.server = server;
9
10    vm.homePageSections = homeSections;
11    
12    vm.slides = gv.homePageSlides;
13    vm.slideIndex = 0;
14	vm.spinner = false;
15
16	var slideshow = gv.homePageSlides;
17	vm.slides = angular.copy(slideshow.imagesList);
18	vm.slides.slideDuration = slideshow.slideDuration * 1000;
19	vm.slides.forEach( function(slide, index){ slide.index = index; });
20
21
22
23	$scope.$on('$viewContentLoaded', function() { 
24        $timeout( function(){ // need this for countering Angular digest cycle !!!
25			if (gv.homeOffsetY !== null) window.scroll( 0, gv.homeOffsetY );
26
27			const slider1Elem = document.getElementById("slider1");
28			var carousel =  bootstrap.Carousel.getOrCreateInstance(slider1Elem);
29			carousel.cycle()
30        }, 0);
31    });
32
33
34
35	function adjustPaddings(){
36		const slider1Elem = document.getElementById("slider1");
37		if (slider1Elem === null) return;
38		const a = document.getElementById("spaceAbove");
39		if (a === null) return;
40        slider1Elem.style.width = a.clientWidth  + "px";
41		const s = document.querySelector(".container-title");
42		if (s === null) return;
43		let o =  (s.clientWidth - a.clientWidth)  / 2;
44		o = o + 2;
45		slider1Elem.style.marginLeft = o+"px";
46	}
47	adjustPaddings();
48
49
50	
51	angular.element($window).bind('resize', function(){
52        $scope.$apply( adjustPaddings );
53    });
54
55
56    new ResizeObserver(
57        () => adjustPaddings()).observe( document.querySelector(".siteContent")
58    );
59
60	
61	vm.processRoute = function( slide ){
62		if ( slide.state ){
63			$state.transitionTo( slide.state, slide.stateParams );
64		} else if ( slide.externalLink ){
65			if ( slide.externalLink.indexOf("https") === -1) slide.externalLink = "https:" + slide.externalLink;
66			$window.open( slide.externalLink, '_blank');
67		}
68	}
69
70
71
72	let imageModal = null;
73	vm.openImageModal = function(){
74		if (!imageModal)
75			imageModal = new bootstrap.Modal(document.getElementById('imageModal'), {});
76			
77		imageModal.show();
78	};
79
80
81
82	vm.closeImageModal = function(){
83		if (imageModal) imageModal.hide();
84	}
85
86
87
88    // vm.homePageSections.forEach(function( section ) {
89    //     if (section.header == "Trending Products") {
90    //         rLoop:
91    //         for (var r=0; r < section.gridRows; r++) {
92    //             for (var c=0; c < section.gridCols; c++) {
93    //                 var index = (r * section.gridCols) + c;
94    //                 if (index >= gv.rankings.featuredProducts.list.length) break rLoop;
95
96    //                 var item = section.grid[r][c];
97    //                 var productId = gv.rankings.featuredProducts.list[index];
98    //                 var product = gv.allProducts.find( function(product){ return product._id == productId; });
99    //                 if ( product === undefined ) break rLoop;
100    //                 item.cellLink = "viewProduct";
101	// 				item.cellLinkParam = {productUrlName: product.urlName};
102    //                 var productImageUrl = "https://s3.eu-central-1.amazonaws.com/uk-sy-assets/www/assets/products/"+productId+"/mr/"+product.gallery[0];
103    //                 productImageUrl = productImageUrl.replace(/ /g, '%20');
104    //                 productImageUrl = productImageUrl.replace(/\(/g, '%28');
105    //                 productImageUrl = productImageUrl.replace(/\)/g, '%29');
106    //                 item.cellBgImg = productImageUrl;
107    //                 item.cellTitle = product.name;
108    //                 item.title = product.title;
109    //                 item.cellTitlePos = "Top";
110    //                 item.padding = true;
111    //             }
112    //         }
113    //     }  
114    // });
115
116
117
118	vm.fetchSectionsData = function (categoryName, sectionIndex) {
119		vm.server.getFilteredNews({ categories: categoryName, show: true }, { sort: {'displayDate': -1 }, limit: 8 })
120		.then(function(res){
121			var newsList = res.data.data,
122				listCount = 0;
123			if (newsList.length > 0){
124				for (var i = 0; i < vm.homePageSections[sectionIndex].gridRows;i++){
125					for (var j = 0; j < vm.homePageSections[sectionIndex].gridCols;j++){
126						vm.homePageSections[sectionIndex].grid[i][j].cellBgImg = newsList[listCount].imageUrl;
127						vm.homePageSections[sectionIndex].grid[i][j].cellLink = newsList[listCount].href;
128						vm.homePageSections[sectionIndex].grid[i][j].cellTitle = newsList[listCount].title;
129						vm.homePageSections[sectionIndex].grid[i][j].newsObj = newsList[listCount];
130						listCount++;
131						if (!newsList[listCount]) {
132							return;
133						}
134					}
135				}
136			}
137		}).catch(function(err){
138			messaging.alert('Unable to fetch section data!');
139		});
140	}
141
142
143
144	vm.populateSections = function () {
145		for (var i = 0; i < vm.homePageSections.length; i++){
146			if (vm.homePageSections[i].sectionType === 'News' && vm.homePageSections[i].displayType === 'Automatic'){
147				vm.fetchSectionsData(vm.homePageSections[i].newsCategory,i);
148			}
149		}
150	}
151
152
153
154	vm.selectSlide = function( index ) {
155		vm.resetTimer();
156		vm.slideIndex = index;
157	}
158
159
160
161	vm.prev = function() {
162		vm.slideIndex = (--vm.slideIndex < 0) ? vm.slides.length-1 : vm.slideIndex;
163	}
164
165
166
167	vm.next = function() {
168		vm.slideIndex = (++vm.slideIndex > vm.slides.length-1) ? 0 : vm.slideIndex;
169	}
170
171
172
173	vm.previousImage = function() {
174		vm.resetTimer();
175		vm.prev();
176	}
177
178
179
180	vm.nextImage = function() {
181		vm.resetTimer();
182		vm.next();
183	}
184
185
186
187	var timer;
188	var sliderFunc = function() {
189		timer = $timeout(function() {
190			vm.next();
191			timer = $timeout(sliderFunc, 2000);
192		}, 2000);
193	};
194	sliderFunc();
195
196
197
198	vm.resetTimer = function() {
199		$timeout.cancel(timer);
200		timer = $timeout(sliderFunc, 5000);
201	}
202
203
204
205	$scope.$on('$destroy', function() {
206		$timeout.cancel(timer); // when the scope is getting destroyed, cancel the timer
207	});
208	
209
210
211	vm.setCat = function( cat ) {
212		if ( cat == "mm" ) {
213			gServices.gVars.search.text = 'mm';
214			gServices.gVars.search.category ='Matrix';
215		} else {
216			gServices.gVars.search.text = 'Ethernet';
217			gServices.gVars.search.category ='Keypad';
218		}
219		$state.transitionTo('');
220		$location.path('products');
221	}
222
223
224
225	vm.getNumber = function (num) {
226		return new Array(num);
227	}
228
229
230
231	vm.tileClick = function( section, tile ) {
232		gv.homeOffsetY = window.scrollY;
233
234		if (section.sectionType === 'News'){
235			vm.news = tile.newsObj;
236			vm.newsBody = $sce.trustAsHtml( vm.news.intro );
237			var myModal = new bootstrap.Modal(document.getElementById('homePageNewsModal'), {});
238			myModal.show();
239			const deregisterUrlChangeDetect = $rootScope.$on("$locationChangeStart", function() { 
240				const modal = bootstrap.Modal.getInstance( document.getElementById('homePageNewsModal'));
241				if (modal) modal.hide();
242				const modalBackdropElem = document.getElementsByClassName("modal-backdrop")[0];
243				if ( modalBackdropElem ) modalBackdropElem.remove();
244				deregisterUrlChangeDetect();
245			});
246		} else if (section.header === 'Trending Products'){
247            $state.transitionTo( tile.cellLink, tile.cellLinkParam );
248		} else if (section.sectionType === 'Link'){
249			if (tile.linkType === 'externalPage'){
250				$window.open( tile.cellExternalUrl, '_blank');
251			} else if (tile.linkType === 'internalPage'){
252				$state.transitionTo( tile.cellLink );
253			} else if (tile.linkType === 'landingPage'){
254				gv.pagePhoto = tile.cellBgImg;
255				gv.landingPage = tile.landingPage;
256				$state.transitionTo('landingPage', {_id: tile.landingPage._id, name: tile.cellTitle.replace(/ /g,"-")});
257			}
258        }
259	}
260
261
262
263	vm.dropSection = function(s,d , context){
264		if(s !== d){
265			var des = Number(d), start = Number(s);
266			var temp = angular.copy(vm.homePageSections[des]);
267			vm.homePageSections[des] = angular.copy(vm.homePageSections[start]);
268			vm.homePageSections[start] = angular.copy(temp);
269			
270			vm.homePageSections[des].positionIndex = des;
271			vm.homePageSections[start].positionIndex = start;
272
273			vm.updateSection(vm.homePageSections[des]);
274			vm.updateSection(vm.homePageSections[start]);
275		}
276
277	};
278
279	
280
281	vm.addSection = function () {
282		var newSection = {
283			header: 'New Container',
284			height: 1,
285			gridRows: 1,
286			gridCols: 1,
287			sectionType: 'Link',
288			displayType: 'Automatic',
289			positionIndex: vm.homePageSections.length,
290			titlePos: 'Bottom',
291			grid: [
292				[
293					{
294						cellTitle: 'New Cell',
295						cellBgImg: '',
296						cellBgColor: '#ddd',
297						cellLink: '',
298						cellExternalUrl: '',
299						linkType: null
300					}, {
301						cellTitle: 'New Cell',
302						cellBgImg: '',
303						cellBgColor: '#ddd',
304						cellLink: '',
305						cellExternalUrl: '',
306						linkType: null
307					}, {
308						cellTitle: 'New Cell',
309						cellBgImg: '',
310						cellBgColor: '#ddd',
311						cellLink: '',
312						cellExternalUrl: '',
313						linkType: null
314					}, {
315						cellTitle: 'New Cell',
316						cellBgImg: '',
317						cellBgColor: '#ddd',
318						cellLink: '',
319						cellExternalUrl: '',
320						linkType: null
321					}
322				],
323				[
324					{
325						cellTitle: 'New Cell',
326						cellBgImg: '',
327						cellBgColor: '#ddd',
328						cellLink: '',
329						cellExternalUrl: '',
330						linkType: null
331					}, {
332						cellTitle: 'New Cell',
333						cellBgImg: '',
334						cellBgColor: '#ddd',
335						cellLink: '',
336						cellExternalUrl: '',
337						linkType: null
338					}, {
339						cellTitle: 'New Cell',
340						cellBgImg: '',
341						cellBgColor: '#ddd',
342						cellLink: '',
343						cellExternalUrl: '',
344						linkType: null
345					}, {
346						cellTitle: 'New Cell',
347						cellBgImg: '',
348						cellBgColor: '#ddd',
349						cellLink: '',
350						cellExternalUrl: '',
351						linkType: null
352					}
353				]
354			]
355		};
356
357		$http({
358			method: "post",
359			url: "/api/private/home/home_page_sections/setup/item",
360			data: {
361				item: {
362					candidate: newSection,
363					collectionName: "home_page_sections"
364                }
365			}
366		}).success(function (result) {
367			if (result.err) {
368				messaging.alert("contact administrator: could not add section to db");
369			} else {
370				messaging.alert('New Section Added to the home page!');
371				newSection._id = result._id;
372				vm.homePageSections.push(newSection);
373			}
374		});
375	};
376
377	
378	
379	vm.updateSection = function (homeSection) {
380		return $http({
381			method: "post",
382			url: "/api/private/home/home_page_sections/dbUpdate",
383			data: {
384				item: homeSection
385			}
386		}).success(function (result) {
387			if (result.err) {
388				messaging.alert("contact administrator: section error");
389			} else {
390				messaging.alert("Successfuly updated section settings");
391			}
392		})
393	};
394
395
396
397	vm.removeSection = function (index) {
398		messaging.confirm('Are you sure you want to permanently remove this section and its related data from the home page?\nPress "OK" to continue.')
399		.then( function( confirmed ){
400			if ( !confirmed ) return;
401
402			$http({
403				method: "delete",
404				url: "/api/private/home/removeSection/"+vm.homePageSections[index]._id,
405				data: {
406				}
407			}).success(function (result) {
408				if (result.err)
409					messaging.alert("contact administrator: section delete error");
410				else {
411					messaging.alert('successfully deleted section item-', index);
412					vm.homePageSections.splice(index, 1);
413				}
414			});
415		}); 
416	};
417
418
419	vm.populateSections();
420
421
422	vm.showLandingPage = false;
423	vm.tinyOptions = gv.tinymceOptionsFull;
424	vm.customPage = {
425		_id:	null,
426		Content: ""
427	}
428
429
430	vm.openPageEditor = function( id, photo, content ){
431		vm.customPage._id = id;
432		vm.customPage.content = content;
433		vm.customPage.photo = photo; 
434		vm.showLandingPage = true;
435	}
436
437
438	vm.savePageEditor = function( form, page ){
439		dbServices.updateHomepageDocument("page", {"_id": vm.customPage._id}, {"photo": vm.customPage.photo, "content": vm.customPage.content})
440		.then( function( result ){
441			const message = result.err ? result.error : "Page updates have been successfully saved in the database.";
442			messaging.alert( message );
443			if ( !result.err ) {
444				form.$setPristine();
445				vm.customPage.content = page.content;
446			}
447		});
448	}
449
450
451	vm.closePageEditor = function( form ){
452		if ( form.$dirty ) {
453			messaging.confirm('This action will discard all changes made to this page.\nPress "OK" to continue.')
454			.then( function( confirmed ){
455				if ( !confirmed ) return;
456
457				form.$setPristine();
458				vm.showLandingPage = false;
459			});
460		} else {
461			vm.showLandingPage = false;
462		}
463	}
464
465}

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.