var sesfDirectives = angular.module('sesfDirectives', []); sesfDirectives.directive('embedSrc', function () { return { restrict: 'A', link: function(scope, element, attrs) { scope.$watch( function() { return attrs.embedSrc; }, function() { element.attr('src', attrs.embedSrc); } ); } }; }); sesfDirectives.directive( 'paginate', [ '$location', '$log', function($location, $log) { return { restrict: 'A', scope: { results: '=paginate' }, template: '', link: function( scope ) { var paginate = function( v ) { var results = scope.results; if(typeof(results.data) == 'undefined') return false; if ( !scope.currentPage ) scope.currentPage = 1; scope.total = results.total; scope.totalPages = results.last; scope.perPage = results.perPage; scope.maxSize = 10; if(typeof($location.search().page) != 'undefined') scope.currentPage = $location.search().page; scope.$watch( 'currentPage', pageChange ); }; var pageChange = function( newPage, lastPage ) { $log.info('changed page: ' + newPage); if ( newPage != lastPage ) { var search = $location.search(); search.page = newPage; $location.search(search); scope.results.$get(); } }; scope.$watch( 'results.data', paginate ); } } }]); sesfDirectives.directive('sesfSelect', ['$location', function($location) { return { restrict: 'E', scope: true, template: '', link: function(scope){ var selectFilter = function(filter){ if(typeof(filter) == 'undefined') return; scope.items = filter.data; scope.f = {}; scope.f.cleanItems = []; angular.forEach(scope.items, function(item){ scope.f.cleanItems.push(item); }); angular.forEach(scope.f.cleanItems, function(item){ if(item.id == scope.filter.value) { scope.f.value = item; } }); }; var change = function(newVal, oldVal){ if(typeof(newVal) == 'undefined') return false; scope.value = newVal; filter = $location.search(); filter[scope.filter.name] = newVal.id; if(!newVal.id) { delete search[scope.filter.name]; } $location.search(filter); }; scope.$watch( 'filter', selectFilter); scope.$watch( 'f.value', change); } }; }]); sesfDirectives.directive('sesfTextbox', ['$location', '$timeout', function($location, $timeout) { return { restrict: 'E', scope: true, template: '', link: function(scope){ var timer=false; scope.$watch('filter.value', function(value){ if(value == 'null' || value == null) { return false; } if(timer){ $timeout.cancel(timer) } timer= $timeout(function(){ var search = $location.search(); search[scope.filter.name] = scope.filter.value; if(!scope.filter.value) { delete search[scope.filter.name]; } $location.search(search); scope.members.$get(); },500) }); } }; }]); sesfDirectives.directive('response', ['$timeout', '$rootScope', '$log', function($timeout, $rootScope, $log){ return { scope: { response: '=' }, template: '' + '
'+ ' {{ response.response }}'+ '
'+ ' '+ ' ' + ' '+ '
'+ ' {{ items }}'+ '
'+ '
' + ' {{ items[0] }}' + '
' + '
' + '
' + '
' + '
' + '
' + ' {{response.response}}' + '
', link: function(scope){ var response = function(response){ $log.info('response ' + JSON.stringify(response)); scope.response = response; $rootScope.log('response', response); if(typeof(response) != 'undefined' && response.status == 'error') { $timeout(function(){ $('html,body').animate({scrollTop: $(document).find('.alert:visible:first').offset().top - 20}); }, 200); } }; scope.typeOf = function(value){ return typeof(value); }; scope.$watch('response', response, true); } }; }]); function Accordion($scope) { $scope.oneAtATime = false; } sesfDirectives.directive('htmlCompile', ['$compile', function($compile) { return function(scope, element, attrs) { scope.$watch( function(scope) { return scope.$eval(attrs.compile); }, function(value) { element.html(value); $compile(element.contents())(scope); }); }; }]); sesfDirectives.directive('registrationCars', ['$location', function(location){ return { restrict: 'E', scope: { registeredCars: '=registrationCars', allCars: '=allCars' }, templateUrl: 'template/event?only=register_car', link: function(scope, element, attrs) { init = function(){ scope.newCarList = []; scope.$watch('allCars', function(newVal, oldVal) { scope.newCarList = newVal; scope.newCarList.push({id: -1, name: 'Add a New Car'}); }); scope.$watch('registeredCars', function() { updateDefaultText(); angular.forEach(scope.registeredCars, function(item){ // find selected judging type key for(var i = 0; i < item.judging_types.length; i++) { if(item.judging_types[i].id == item.judging_type_id) { item.judging_type = item.judging_types[i]; } } // remove added cars from all cars list for(var i = 0; i < scope.newCarList.length; i++) { if(scope.newCarList[i].id == item.id) { scope.newCarList.splice(i, 1); } } }); }); } updateDefaultText = function() { scope.selectDefaultText = (typeof(scope.registeredCars) != 'undefined' && scope.registeredCars.length > 0 ? 'Register Another Car' : 'Select a Car'); } addCar = function(newVal) { if(newVal) { if(newVal.id == -1) { scope.newCar(); return; } scope.newCarList.splice(scope.newCarList.indexOf(scope.addCar), 1); scope.registeredCars.push(newVal); updateDefaultText(); } } scope.removeCar = function(car) { if(car) { scope.registeredCars.splice(scope.registeredCars.indexOf(car), 1); var addCar = scope.newCarList.pop(); scope.newCarList.push(car); scope.newCarList.push(addCar); scope.addCar = null; updateDefaultText(); } } scope.newCar = function(car) { location.path('/car/new'); } init(); scope.$watch( 'addCar', addCar ); } }; }]); sesfDirectives.directive('registrationExtras', ['$location', function(location){ return { restrict: 'E', scope: { registeredCars: '=', registeredGuests: '=', registeredExtras: '=', extras: '=' }, templateUrl: 'template/event?only=register_extras', link: function(scope, element, attrs) { scope.generateObjectRange = function(id, range) { items = [{show_extra_id: id, num_extras: 0}]; for(var i = 0; i < range; i++) { items.push({show_extra_id: id, num_extras: i + 1}); } return items; }; scope.countGuests = function() { var num_guests = 0; angular.forEach(scope.registeredGuests, function(item) { if(typeof(item.deleted) == 'undefined' || item.deleted === false) { num_guests ++; } }); return num_guests; }; scope.init = function() { angular.forEach(scope.extras, function(extra, key){ if(extra.type_id == 1) { scope.extras[key].num_items = scope.generateObjectRange(extra.id, scope.registeredCars.length); } else if(extra.type_id == 2) { scope.extras[key].num_items = scope.generateObjectRange(extra.id, 1 + scope.countGuests()); } else if(extra.type_id == 4) { scope.extras[key].num_items = scope.generateObjectRange(extra.id, 1 + extra.max_value); } if(scope.registeredExtras[key]) { if(scope.registeredExtras[key].num_extras > scope.extras[key].num_items.length) { scope.registeredExtras[key].num_extras = scope.extras[key].num_items.length; } angular.forEach(scope.extras[key].num_items, function(item){ if(item.num_extras == scope.registeredExtras[key].num_extras) { console.log('parse', item); scope.registeredExtras[key] = item; } }); } }); }; scope.$watch('registeredGuests', function() { scope.init(); }, true); scope.$watch('registeredExtras', function() { scope.init(); }); } }; }]); sesfDirectives.directive('validateEmailNotSameAsUser', ['$log', function($log){ return { require: 'ngModel', scope: { guest: '=guest', userData: '=userData' }, link: function(scope, elm, attrs, ctrl) { ctrl.$parsers.unshift(function(viewValue, test) { console.log(viewValue, test, scope.userData, scope.guest); if(viewValue.id && viewValue.id == 1) { console.log('here'); scope.userData.guestAdult = true; } else if(viewValue.id) { scope.userData.guestAdult = false; } else { if (viewValue == scope.userData.email) { console.log('here2'); scope.userData.emailMatch = true; } else { scope.userData.emailMatch = false; } } console.log(scope.userData.guestAdult, scope.userData.emailMatch); if(scope.userData.guestAdult == true && scope.userData.emailMatch == true) { ctrl.$setValidity('required', false); scope.guest.emailError = true; } else { ctrl.$setValidity('required', true); scope.guest.emailError = false; } return viewValue; }); } }; }]); sesfDirectives.directive('registrationGuests', ['$log', function($log){ return { restrict: 'E', scope: { guestTypes: '=guestTypesData', guests: '=guestsData', userData: '=userData', validationMessages: '=validationMessages' }, templateUrl: 'template/event?only=register_guests', link: function(scope, element, attrs) { scope.removedGuests = []; scope.hasGuests = false; scope.$watch('guests', function(){ scope.$watch('guestTypes', function(){ if(scope.guestTypes && scope.guests) { for(var guest = 0; guest < scope.guests.length; guest++) { scope.hasGuests = true; for(var type = 0; type < scope.guestTypes.length; type++) { if(scope.guests[guest].type_id == scope.guestTypes[type].id) { scope.guests[guest].type = scope.guestTypes[type]; } } } } }); }); scope.addGuest = function(){ $log.info('guest added'); scope.guests.push({}); scope.calculateHasGuests(); } scope.removeGuest = function(guest){ $log.info('guest removed'); if(guest) { if(guest.id) guest.deleted = true; else scope.guests.splice(scope.guests.indexOf(guest), 1); scope.calculateHasGuests(); } } scope.calculateHasGuests = function(){ angular.forEach(scope.guests, function(item){ if(item.deleted != true) scope.hasGuests = true; }); } } }; }]); sesfDirectives.directive('registrationArrivalTime', ['$log', function($log){ return { restrict: 'E', scope: { eventDates: '=eventDates', arrivalDate: '=arrivalDate', arrivalTime: '=arrivalTime', validationMessages: '=validationMessages', }, templateUrl: 'template/event?only=register_arrival_time', link: function(scope, element, attrs) { scope.$watch('eventDates', function(){ angular.forEach(scope.eventDates, function(item){ if(item.data == scope.arrivalDate) { scope.arrivalDate = item; } }); }); scope.$watch('arrivalDate', function(){ angular.forEach(scope.eventDates, function(item){ if(item.data == scope.arrivalDate) { scope.arrivalDate = item; } }); }); } }; }]); sesfDirectives.directive('registrationCancel', ['$log', function($log){ return { restrict: 'E', scope: { eventInfo: '=eventInfo', cancel: '=cancel' }, templateUrl: 'template/event?only=register_cancel', link: function(scope, element, attrs) { /* scope.donationList = [ {"id" : 0 , "name" : "No thanks!"}, {"id" : 5 , "name" : "$5"}, {"id" : 10 , "name" : "$10"}, {"id" : 15 , "name" : "$15"}, {"id" : 25 , "name" : "$25"}, {"id" : 35 , "name" : "$35"}, {"id" : 50 , "name" : "$50"}, {"id" : 75 , "name" : "$75"}, {"id" : 100 , "name" : "$100"}, {"id" : 150 , "name" : "$150"}, {"id" : 200 , "name" : "$200"}, {"id" : 250 , "name" : "$250"}, {"id" : 300 , "name" : "$300"}, {"id" : 400 , "name" : "$400"}, {"id" : 500 , "name" : "$500"} ]; scope.donationSelection = scope.donationList[0]; scope.$watch('donation', function(){ if(scope.donation) { for(var id = 0; id < scope.donationList.length; id++) { if(scope.donationList[id].id == scope.donation) { scope.donationSelection = scope.donationList[id]; } } } }); scope.$watch('donationSelection', function(to){ scope.donation = to.id; });*/ } }; }]); sesfDirectives.directive('registrationDonation', ['$log', function($log){ return { restrict: 'E', scope: { donation: '=donation' }, templateUrl: 'template/event?only=register_donation', link: function(scope, element, attrs) { scope.donationList = [ {"id" : 0 , "name" : "No thanks!"}, {"id" : 5 , "name" : "$5"}, {"id" : 10 , "name" : "$10"}, {"id" : 15 , "name" : "$15"}, {"id" : 25 , "name" : "$25"}, {"id" : 35 , "name" : "$35"}, {"id" : 50 , "name" : "$50"}, {"id" : 75 , "name" : "$75"}, {"id" : 100 , "name" : "$100"}, {"id" : 150 , "name" : "$150"}, {"id" : 200 , "name" : "$200"}, {"id" : 250 , "name" : "$250"}, {"id" : 300 , "name" : "$300"}, {"id" : 400 , "name" : "$400"}, {"id" : 500 , "name" : "$500"} ]; scope.donationSelection = scope.donationList[0]; scope.$watch('donation', function(){ if(scope.donation) { for(var id = 0; id < scope.donationList.length; id++) { if(scope.donationList[id].id == scope.donation) { scope.donationSelection = scope.donationList[id]; } } } }); scope.$watch('donationSelection', function(to){ scope.donation = to.id; }); } }; }]); sesfDirectives.directive('registrationShirts', ['$log', function($log){ return { restrict: 'E', scope: { shirtTypes: '=shirtTypesData', shirts: '=shirtsData', validationMessages: '=validationMessages' }, templateUrl: 'template/event?only=register_shirts', link: function(scope, element, attrs) { scope.removedShirts = []; scope.hasShirts = false; scope.$watch('shirts', function(){ scope.$watch('shirtTypes', function(){ if(scope.shirtTypes && scope.shirts) { for(var shirt = 0; shirt < scope.shirts.length; shirt++) { scope.hasShirts = true; for(var type = 0; type < scope.shirtTypes.length; type++) { if(scope.shirts[shirt].type_id == scope.shirtTypes[type].id) { scope.shirts[shirt].type = scope.shirtTypes[type]; } } } } }); }); scope.addShirt = function(){ $log.info('added shirt'); scope.shirts.push({}); scope.calculateHasShirts(); } scope.removeShirt = function(shirt){ $log.info('removed shirt'); if(shirt) { if(shirt.id) shirt.deleted = true; else scope.shirts.splice(scope.shirts.indexOf(shirt), 1); scope.calculateHasShirts(); } } scope.calculateHasShirts = function(){ angular.forEach(scope.shirts, function(item){ if(item.deleted != true) scope.hasShirts = true; }); } } }; }]); sesfDirectives.directive('registrationSummary', function(){ return { restrict: 'E', scope: { registration: '=registrationData', eventInfo: '=eventInfo', }, templateUrl: 'template/event?only=registration_summary', link: function(scope, element, attrs) { } }; }); sesfDirectives.directive('carSettingsInfo', [function() { return { restrict: 'E', scope: { carInfo: '=', bodies: '=', types: '=', carInfoResponse: '=', submitMethod: '&' }, templateUrl: 'template/car_profile?only=settings_info', link: function(scope, element, attrs) { scope.$watch('bodies', function(from, to) { try { angular.forEach(scope.bodies, function(item){ if(scope.carInfo.body.id == item.id) { scope.carInfo.body = item; } }); } catch (e) {} }); scope.$watch('types', function() { try { angular.forEach(scope.types, function(item){ if(scope.carInfo.type_id == item.id) { scope.carInfo.type = item; } }); } catch (e) {} }); } }; }]); sesfDirectives.directive('breadcrumb', [function() { return { restrict: 'E', scope: { breadcrumb: '=', currentGallery: '=' }, templateUrl: 'template/gallery?only=breadcrumb', link: function(scope, element, attrs) { } }; }]); sesfDirectives.directive('galleryUpload', ['$window', 'GalleryService', function($window, GalleryService) { return { restrict: 'E', scope: { items: '=', galleryId: '=' }, templateUrl: 'template/gallery?only=upload', link: function(scope, element, attrs) { var myDropzone = new Dropzone(".dropzone", { url: "/gallery/upload-file", acceptedFiles: "image/*", uploadMultiple: false, parallelUploads: 1/*, autoProcessQueue: false*/ }); myDropzone.on("queue", function(file, xhr, form){ form.append('last_modified', file.lastModified); form.append('last_modified_date', file.lastModifiedDate); console.log('queue', file); }); myDropzone.on("complete", function(file){ if(file.xhr.status == 200) { myDropzone.removeFile(file); } }); myDropzone.on("queuecomplete", function(){ console.log('queuecomplete'); var promise = GalleryService.Items().get().$promise; promise.then( function(data){ scope.items = data.items; } ); }); /*scope.upload = function(){ // Get the queued files var files = myDropzone.getQueuedFiles(); console.log(files); // Sort theme based on the DOM element index files.sort(function(a, b){ return ($(a.previewElement).index() > $(b.previewElement).index()) ? 1 : -1; }) console.log(files); return; // Clear the dropzone queue myDropzone.removeAllFiles(true); // Add the reordered files to the queue myDropzone.handleFiles(files); return myDropzone.processQueue(); };*/ scope.cancel = function(){ myDropzone.removeAllFiles(true); return false; }; } }; }]); sesfDirectives.directive('carImageUpload', [function() { return { restrict: 'E', scope: true, templateUrl: 'template/car_profile?only=car_image_upload', link: function(scope, element, attrs) { } }; }]); sesfDirectives.directive('sesfNav', ['$route', '$location', function($route, $location){ return { scope: true, link: function(scope, element, attrs) { scope.isActive = function(base_url, path){ var url = base_url + (typeof(path) != 'undefined' ? '/' + path : ''); return (url == '#' + $location.path() ? 'active' : ''); }; } }; }]); sesfDirectives.directive('listView', [function(){ return { restrict: 'E', scope: { listData: '=listData', isEventPage: '=isEventPage' }, templateUrl: 'template/utilities?only=list_view', link: function(scope, element, attrs) { scope.collatedData = []; scope.cols = scope.isEventPage ? 5 : 4; scope.$watch('listData.data', function(){ if(typeof(scope.listData.data) == 'undefined') { scope.collatedData = []; return; } scope.collatedData = []; for (var i = 0; i < scope.listData.data.length; i++) { if (i % scope.cols == 0) { scope.collatedData.push([]); } scope.collatedData[scope.collatedData.length-1].push(scope.listData.data[i]); } }); } }; }]); sesfDirectives.directive('showInvoice', [function(){ return { restrict: 'E', scope: true, templateUrl: 'template/member?only=invoice', link: function(scope, element, attrs) { } }; }]); sesfDirectives.directive('eventList', function() { return { restrict: 'E', scope: true, templateUrl: 'template/events?only=event_list', link: function(scope, element, attrs) { } }; });