UNPKG

unserver-unify

Version:

245 lines (239 loc) 8.3 kB
'use strict'; angular.module('bamboo.mystudy').controller('MyNotesCtrl', function($scope, MystudyService, CourseService, $uibModal, printHelper, ApiService) { var _self = this; $scope.appUrl = ApiService.APPURL; var coursesid = []; var coursewareid = []; $scope.isCollapsed = true; $scope.selectIndex = 0; $scope.noteDict = []; // all notes dict $scope.downloadNoteDict = {}; $scope.courseNoteCountDict = {}; $scope.chapterNoteCountDict = {}; $scope.oneAtATime = true; _self.clickCourse = function(cid, idx) { $scope.selectedCosChapterIds = Object.keys(_self.content[cid]); $scope.selectedCourse = _self.content[cid]; $scope.selectIndex = idx; //clear up note dict $scope.downloadNoteDict = {}; } _self.clickChapter = function(cid, cpid) { $scope.selectedChapterNotesIds = Object.keys(_self.content[cid][cpid]); //courseware ids console.log(_self.content[cid]); console.log(_self.content[cid][cpid]); console.log($scope.selectedChapterNotesIds); _self.CID = cid; _self.CPID = cpid; } _self.init = function() { //get all notes for current user MystudyService.getMyNotes(function(data) { console.log(data); //post processing the received data, looking for { 'created' : xxx , 'content': xxx} _self.myNotesList = []; _self.displayNotesist = []; if (data.content) { //getNotesFromNestedObj(data.content, _self.myNotesList); _self.content = data.content; _self.myNotesList = pareseContent(data.content) _self.displayNotesist = _self.myNotesList; console.log(_self.displayNotesist); // cnt the new notes cnt(within 7 days), and old notes cnt(older than 7 days) _self.newNotesCnt = 0; _self.oldNotesCnt = 0; _self.newNotes = []; _self.oldNotes = []; if (_self.myNotesList) { var weekago = new Date(); weekago.setDate(weekago.getDate() - 7); for (var i = 0; i < _self.myNotesList.length; i += 1) { var then = new Date(_self.myNotesList[i].created); if (weekago <= then) { _self.newNotesCnt += 1; _self.newNotes.push(_self.myNotesList[i]); _self.displaynewNots = _self.newNots; } else { _self.oldNotesCnt += 1; _self.oldNotes.push(_self.myNotesList[i]); _self.displayoldNotes = _self.oldNotes; } } } if (coursesid.length > 0) { CourseService.getCoursesInfoByIds(coursesid, function(data) { //convert arrary to json, index by id _self.coursesinfo = {}; if (data) { console.log(data); for (var i = 0; i < data.length; i += 1) { _self.coursesinfo[data[i]._id] = data[i]; delete _self.coursesinfo[data[i]._id]._id; } console.log(_self.coursesinfo); } }) } if (coursewareid.length > 0) { CourseService.getCoursewareInfoByIds(coursewareid, function(data) { //convert array to json, index by id _self.coursewareinfo = {}; if (data) { for (var i = 0; i < data.length; i += 1) { _self.coursewareinfo[data[i]._id] = data[i]; delete _self.coursewareinfo[data[i]._id]._id; } } console.log(_self.coursewareinfo); }) } } }) }; function collectDownloadNotes() { var ids = Object.keys($scope.ChapterNoteDict[_self.CPID]); console.log(ids); var content = []; angular.forEach(ids, function(id, idx) { if ($scope.noteDict[id]) { console.log($scope.noteDict[id]); angular.forEach($scope.noteDict[id], function(val, idx) { var note = $scope.downloadNoteDict[id]; note && note[idx] && content.push(val); }); } }); return content; } this.downloadNotes = function() { var notesArray = collectDownloadNotes(); var printContent = ""; var courseName = _self.coursesinfo[_self.CID] && _self.coursesinfo[_self.CID].name; var chapterName = _self.coursewareinfo[_self.CPID] && _self.coursewareinfo[_self.CPID].name; console.log(_self.coursewareinfo[_self.CPID]); var courseNameStr = "<h2>" + courseName + "</h3>"; var chapterNameStr = "<h3>" + chapterName + "</h3>"; angular.forEach(notesArray, function(note, idx) { var content = note.content; console.log(courseName, chapterName, content); var contentStr = "<p>" + content + "</p>" printContent +="<span>" + (idx + 1) + "." + "</span>" + contentStr; }); var printnote = {}; printnote.content = courseNameStr + chapterNameStr + printContent; //console.log(note); _self.download(printnote); } this.download = function(note) { console.log(note); var modalInstance = $uibModal.open({ animation: true, component: 'simpleDialogueComponent', resolve: { options: function() { return { content: note.content, title: 'Note', show_header: true, show_footer: false, show_download: true, btn_download: "Print" }; } } }); modalInstance.result.then(function(selectedItem) { printHelper.printText(selectedItem); }, function() { console.info('modal-component dismissed at: ' + new Date()); }); } function getNotesFromNestedObj(root, dest) { for (var prop in root) { var value = root[prop]; if (typeof value === 'object' && !value.created && !value.content) { getNotesFromNestedObj(value, dest); } else { dest.push(value); } } }; /* the input content must by following structure : content : { courseid : { coursewareid : { resource/chapter id : [ { 'content': xxxx, 'created': xxxx } ... ] } ... } ... } the functino below will convet it to format like [ { 'courseid' : xxxx, 'coursewareid' : xxxx, 'chapterid' : xxxx, 'content' : xxxx, 'created' : xxxx }, ... ] */ $scope.ChapterNoteDict = {}; function pareseContent(content) { var result = []; //loop through courseid angular.forEach(content, function(value1, key1) { var courseNoteLen = 0; var cid = key1; // save the cid coursesid.push(cid); // loop through chapterid angular.forEach(value1, function(value2, key2) { var mid = key2; coursewareid.push(mid); $scope.ChapterNoteDict[mid] = value2; var chapterNoteLen = 0; // loop through courseware angular.forEach(value2, function(value3, key3) { var rid = key3; //loop through array if (value3.length > 0) { for (var i = 0; i < value3.length; i++) { courseNoteLen++; chapterNoteLen++; if (value3[i].created && value3[i].content) { var onenote = { cid: cid, mid: mid, rid: rid, created: value3[i].created, content: value3[i].content }; //console.log(onenote); result.push(onenote); if (!$scope.noteDict[rid]) { $scope.noteDict[rid] = []; $scope.noteDict[rid].push(onenote); } else { $scope.noteDict[rid].push(onenote); } } }; } }) $scope.chapterNoteCountDict[mid] = chapterNoteLen; }) $scope.courseNoteCountDict[cid] = courseNoteLen; }) $scope.coursesid = coursesid; return result; }; _self.init(); });