Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. define(['./module'], function(directives){
  2. 'use strict';
  3.  
  4. return directives.directive('ttkdSwiper', ['$log', function ($log) {
  5.  
  6. var ttkdSwiper = {
  7. restrict: 'A',
  8. templateUrl: 'partials/checkin/ttkd-swiper',
  9. scope: {
  10. type: '=',
  11. selectedClassId: '='
  12.  
  13. },
  14.  
  15. controller: function($scope, ProgramSvc, ClassSvc, StudentSvc) {
  16. // Load data based on type
  17. $scope.data = [];
  18.  
  19. // store students so we don't need to reload
  20. $scope.allStudents = [];
  21.  
  22. if($scope.type === 'students' && $scope.selectedClassId) {
  23. $log.log('Attempting to load students...');
  24.  
  25. loadStudents($scope.selectedClassId);
  26.  
  27. } else {
  28. $log.log('Attempting to load programs...');
  29. loadProgramsAndClasses();
  30. }
  31.  
  32. $scope.filterStudents = function(filter, filterParam, maxLateralSlideDeck) {
  33. var data = $scope.allStudents;
  34.  
  35.  
  36. var filtered = data;
  37.  
  38. if(filter && filterParam) {
  39. filtered = _.filter(data, function (student) {
  40. return _.contains(filter, student[filterParam][0]);
  41. });
  42.  
  43. // and sort...
  44. filtered = _.sortBy(filtered, function(student) {
  45. return student[filterParam];
  46. });
  47. }
  48.  
  49. if(!maxLateralSlideDeck) {
  50. maxLateralSlideDeck = 5;
  51. }
  52.  
  53. var returnableFiltered = [];
  54.  
  55. var filteredLength = filtered.length;
  56. var neededLaterals = Math.ceil(filteredLength / maxLateralSlideDeck);
  57.  
  58. for(var i=0; i<neededLaterals; i++) {
  59. var howMany;
  60. if(i+1 === neededLaterals) {
  61. howMany = filteredLength - (i * maxLateralSlideDeck);
  62. } else {
  63. howMany = maxLateralSlideDeck;
  64. }
  65.  
  66. returnableFiltered.push({
  67. students: filtered.splice(0, howMany)
  68. });
  69. }
  70.  
  71. $scope.data = returnableFiltered;
  72. };
  73.  
  74. function loadStudents(selectedClassId) {
  75. var data = [];
  76.  
  77. ClassSvc.read(selectedClassId, null, false).then(function(c){
  78. async.parallel([
  79. function(callback, err){
  80. var returns = 0;
  81.  
  82. _.each(c.students, function(s){
  83. async.parallel([
  84. function(studentCallback, studentErr) {
  85. StudentSvc.read(s, null, false).then(function(student) {
  86. data.push(student);
  87. returns += 1;
  88. studentCallback();
  89. });
  90. }],
  91. function(studentErr) {
  92. if(returns === c.students.length) {
  93. callback();
  94. }
  95. }
  96. );
  97. });
  98. }],
  99.  
  100. function(err){
  101. $scope.allStudents = data;
  102.  
  103. $scope.filterStudents(['A','B','C','D','M','T'], 'firstName');
  104. }
  105. );
  106. });
  107. }
  108.  
  109. function loadProgramsAndClasses() {
  110. var data = [];
  111.  
  112. ProgramSvc.list().then(function(programs){
  113. async.parallel([
  114. function(callback, err){
  115. //Get class objects and put into programs
  116. ClassSvc.list().then(function(classes){
  117. var i;
  118. for(i=0; i<programs.length; i++) {
  119. programs[i].classes = _.where(classes, {'program': programs[i]._id});
  120. }
  121. callback();
  122. });
  123. }],
  124.  
  125. function(err){
  126. _.each(programs, function(p){
  127. data.push({
  128. 'classObjs': p.classes,
  129. 'name': p.name,
  130. 'classNames': _.map(p.classes, function(c){return c.name;}),
  131. '_id': p._id
  132. });
  133. });
  134.  
  135. $scope.data = data;
  136. }
  137. );
  138. });
  139. }
  140. },
  141.  
  142. link: function($scope) {
  143. $scope.$watch('data', function() {
  144.  
  145. var lateralSwipersNested = [];
  146.  
  147. var swiperParent = new Swiper('.swiper-parent', {
  148. mode: 'vertical',
  149. slidesPerView: 3,
  150. centeredSlides: true,
  151. watchActiveIndex: true
  152. });
  153.  
  154. $scope.swiperParent = swiperParent;
  155.  
  156. var lateralHeight;
  157.  
  158. }, true);
  159. }
  160. };
  161.  
  162. return ttkdSwiper;
  163. }])
  164.  
  165. .directive('ttkdSwiperLateralItemClass', ['$log', '$compile', function($log, $compile) {
  166. var compileHack = $compile;
  167. var ttkdSwiperLateralItemClass = {
  168. restrict: 'A',
  169. scope: {
  170. parent: '=',
  171. item: '=',
  172. swiperId: '=',
  173. type: '='
  174. },
  175.  
  176. controller: ['$scope', function($scope) {
  177.  
  178.  
  179.  
  180. }],
  181.  
  182. link: function($scope, element) {
  183. var item = $scope.item;
  184. var swiperParent = $scope.parent;
  185. var slideClassName = 'swiper-nested-' + $scope.swiperId;
  186.  
  187. var itemTitle;
  188.  
  189.  
  190. if($scope.type === 'students') {
  191. var itemTitle = 'TEST';
  192. } else {
  193. var itemTitle = item.name;
  194. }
  195.  
  196.  
  197. //create a parent swiper for horizontal movement
  198. var rowTitle = '<div class="title">' + itemTitle + '</div>';
  199. var lateralSwiperWrapper = swiperParent.createSlide(rowTitle + '<div class="swiper-container nested '+slideClassName+'"><div class="swiper-wrapper nested"></div></div>', 'swiper-slide');
  200.  
  201. lateralSwiperWrapper.append();
  202.  
  203. var newLateralSwiperWrapper = new Swiper('.' + slideClassName, {
  204. mode: 'horizontal',
  205. slidesPerView: 4,
  206. centeredSlides: true,
  207. watchActiveIndex: true,
  208. resistance: '100%'
  209. });
  210.  
  211. var lateralHeight;
  212. if(swiperParent.slides.length > 0){
  213. lateralHeight = swiperParent.slides[0].style.height;
  214. }
  215.  
  216. $scope.classes = {};
  217. $scope.students = {};
  218.  
  219. if($scope.type === 'students') {
  220. for(var j=0; j<item.students.length; j++) {
  221. var studentName = item.students[j].firstName + ' ' + item.students[j].lastName;
  222. var studentId = item.students[j]._id;
  223. $scope.students[studentId] = item.students[j];
  224. var newHSlide = newLateralSwiperWrapper.createSlide('<div ttkd-swiper-slide-item-student students="students" id="\''+studentId+'\'"></div>', 'swiper-slide red-slide');
  225. newHSlide.append();
  226.  
  227. compileHack(newHSlide)($scope);
  228.  
  229. resizeLateralSlides(newLateralSwiperWrapper, lateralHeight);
  230. }
  231. } else {
  232. for(var j=0; j<item.classObjs.length; j++) {
  233. var className = item.classObjs[j].name;
  234. var classId = item.classObjs[j]._id;
  235. $scope.classes[classId] = item.classObjs[j];
  236. //name="\''+className+'\'"
  237. var newHSlide = newLateralSwiperWrapper.createSlide('<div ttkd-swiper-slide-item-class classes="classes" id="\''+classId+'\'"></div>', 'swiper-slide red-slide');
  238. newHSlide.append();
  239.  
  240. compileHack(newHSlide)($scope);
  241.  
  242. resizeLateralSlides(newLateralSwiperWrapper, lateralHeight);
  243. }
  244. }
  245.  
  246.  
  247. //calculateHeight not working... quick/hacky fix
  248. function resizeLateralSlides(swiper, newHeight) {
  249. for(var i=0; i<swiper.slides.length; i++) {
  250. swiper.slides[i].style.height = newHeight;
  251. }
  252. }
  253. }
  254. };
  255.  
  256. return ttkdSwiperLateralItemClass;
  257. }])
  258.  
  259.  
  260.  
  261.  
  262. .directive('ttkdSwiperLateralItemStudent', ['$log', '$compile', function($log, $compile) {
  263. var compileHack = $compile;
  264. var ttkdSwiperLateralItemStudent = {
  265. restrict: 'A',
  266. scope: {
  267. parent: '=',
  268. item: '=',
  269. swiperId: '=',
  270. type: '='
  271. },
  272.  
  273. controller: ['$scope', function($scope) {
  274.  
  275.  
  276.  
  277. }],
  278.  
  279. link: function($scope, element) {
  280. var item = $scope.item;
  281. var swiperParent = $scope.parent;
  282. var slideClassName = 'swiper-nested-' + $scope.swiperId;
  283.  
  284.  
  285.  
  286.  
  287.  
  288. //create a parent swiper for horizontal movement
  289. var lateralSwiperWrapper = swiperParent.createSlide('<div class="swiper-container nested '+slideClassName+'"><div class="swiper-wrapper nested"></div></div>', 'swiper-slide');
  290.  
  291. lateralSwiperWrapper.append();
  292.  
  293.  
  294. var lateralHeight;
  295. if(swiperParent.slides.length > 0){
  296. lateralHeight = swiperParent.slides[0].style.height;
  297. }
  298.  
  299. $scope.students = item.students;
  300.  
  301. // for(var j=0; j<item.students.length; j++) {
  302. // var studentName = item.students[j].firstName + ' ' + item.students[j].lastName;
  303. // var studentId = item.students[j]._id;
  304. // $scope.students[studentId] = item.students[j];
  305. // }
  306.  
  307. }
  308. };
  309.  
  310. return ttkdSwiperLateralItemStudent;
  311. }])
  312.  
  313.  
  314.  
  315. .directive('ttkdSwiperSlideItemClass', ['$log', '$state', function($log, $state) {
  316. var ttkdSwiperSlideItemClass = {
  317. restrict: 'A',
  318. templateUrl: 'partials/checkin/ttkd-swiper-card-class',
  319. scope: {
  320. classId: '=id',
  321. classes: '='
  322. },
  323. replace:true,
  324.  
  325. controller: function($scope) {
  326. $scope.className = $scope.classes[$scope.classId].name;
  327. $scope.classContinue = function() {
  328. $log.log('hit again ' + $scope.classId);
  329. $state.go('checkin.home.unranked', {id: $scope.classId});
  330. }
  331. },
  332.  
  333. link: function($scope) {
  334.  
  335.  
  336. }
  337. };
  338.  
  339. return ttkdSwiperSlideItemClass;
  340. }])
  341.  
  342. .directive('ttkdSwiperSlideItemStudent', ['$log', function($log) {
  343. var ttkdSwiperSlideItemStudent = {
  344. restrict: 'A',
  345. templateUrl: 'partials/checkin/ttkd-swiper-card-student',
  346. scope: {
  347. student: '='
  348. },
  349. replace:true,
  350.  
  351. controller: function($scope) {
  352. $scope.studentName = $scope.students[$scope.studentId].firstName + ' ' + $scope.students[$scope.studentId].lastName;
  353. $scope.studentAvatar = $scope.students[$scope.studentId].avatar;
  354. $scope.studentContinue = function() {
  355. $log.log('hit again student ' + $scope.studentId);
  356. }
  357. },
  358.  
  359. link: function($scope, element) {
  360. $log.log('t');
  361.  
  362. }
  363. };
  364.  
  365. return ttkdSwiperSlideItemStudent;
  366. }]);
  367.  
  368. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement