Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.94 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var URL = 'http://localhost:8000/'
  4. var modified = false;
  5.  
  6. var viewModel = function () {
  7. var self = this;
  8. console.log("Robię modified na true")
  9.  
  10. var dataPossibleType = function (name, value) {
  11. this.dateName = name;
  12. this.dateValue = value;
  13. };
  14.  
  15. self.filtreDataTypes = ko.observableArray([
  16. new dataPossibleType("Do", "dateTo"),
  17. new dataPossibleType("Dokładnie", "dateExacly"),
  18. new dataPossibleType("Od", "dateFrom")
  19. ]);
  20.  
  21. var valuePossibleType = function (name, value) {
  22. this.valueName = name;
  23. this.valueValue = value;
  24. };
  25.  
  26. self.filtreValueTypes = ko.observableArray([
  27. new valuePossibleType("Wieksze niz", "greater"),
  28. new valuePossibleType("Mniejsze niz", "less"),
  29. new valuePossibleType("Dokladnie", "equal")
  30. ]);
  31.  
  32.  
  33. self.studentSubscription = null;
  34. self.courseSubscription = null;
  35. self.gradeSubscription = null;
  36.  
  37. self.studentFilters = {
  38. studentId: ko.observable(),
  39. name: ko.observable(),
  40. surname: ko.observable(),
  41. birth_date: ko.observable(),
  42. type: ko.observable(),
  43. dateTo: ko.observable(),
  44. dateExacly: ko.observable(),
  45. dateFrom: ko.observable()
  46.  
  47. };
  48.  
  49. self.coursesFilters = {
  50. subjectName: ko.observable(),
  51. teacherName: ko.observable()
  52. };
  53.  
  54. self.gradesFilters = {
  55. value: ko.observable(),
  56. courseId: ko.observable(),
  57. type: ko.observable()
  58. };
  59.  
  60. self.gradesList = ko.observableArray();
  61. self.studentsList = ko.observableArray();
  62. self.coursesList = ko.observableArray();
  63.  
  64. console.log("download students");
  65. function downloadStudents() {
  66. var jsonData = ko.toJS(self.studentFilters);
  67.  
  68. if (jsonData.birth_date === "") {
  69. delete jsonData.birth_date;
  70. delete jsonData.type;
  71. }
  72. if (jsonData.name === "") {
  73. delete jsonData.name;
  74. }
  75. if (jsonData.surname === "") {
  76. delete jsonData.surname;
  77. }
  78. if (jsonData.ID === "") {
  79. delete jsonData.ID;
  80. }
  81. if (jsonData.type === "") {
  82. delete jsonData.birth_date;
  83. }
  84. else {
  85. if (jsonData.type == "dateTo") {
  86. jsonData.dateTo = jsonData.birth_date;
  87. delete jsonData.birth_date;
  88. delete jsonData.dateExacly;
  89. delete jsonData.dateFrom;
  90. delete jsonData.type;
  91. }
  92.  
  93. if (jsonData.type == "dateExacly") {
  94. jsonData.dateExacly = jsonData.birth_date;
  95. delete jsonData.birth_date;
  96. delete jsonData.dateTo;
  97. delete jsonData.dateFrom;
  98. delete jsonData.type;
  99. }
  100. if (jsonData.type == "dateFrom") {
  101. jsonData.dateFrom = jsonData.birth_date;
  102. delete jsonData.birth_date;
  103. delete jsonData.dateTo;
  104. delete jsonData.dateExacly;
  105. delete jsonData.type;
  106. }
  107.  
  108. }
  109.  
  110. $.ajax({
  111. type: 'GET',
  112. url: URL + 'students',
  113. contentType: "application/json",
  114. data: jsonData,
  115. dataType: "json",
  116. success: function (data) {
  117. self.studentsList.removeAll();
  118. data.forEach(function (record) {
  119. self.studentsList.push(new ObservableObject(record));
  120. self.currentStudentIdx = record["studentId"]
  121. });
  122. self.studentSubscription = self.studentsList.subscribe(removedObjectCallback, null, 'arrayChange');
  123. },
  124. error: function (jq, st, error) {
  125. console.log("error");
  126. alert(error);
  127. }
  128. });
  129. }
  130. downloadStudents();
  131.  
  132. console.log("download courses");
  133. function downloadCourses() {
  134. var jsonData = ko.toJS(self.coursesFilters);
  135. if (jsonData.subjectName === "") {
  136. delete jsonData.name;
  137. }
  138. if (jsonData.teacherName === "") {
  139. delete jsonData.teacherName;
  140. }
  141.  
  142. $.ajax({
  143. type: 'GET',
  144. url: URL + 'subjects',
  145. contentType: "application/json",
  146. data: jsonData,
  147. dataType: "json",
  148. success: function (data) {
  149. self.coursesList.removeAll();
  150. data.forEach(function (record) {
  151. self.coursesList.push(new ObservableObject(record));
  152. });
  153. self.courseSubscription = self.coursesList.subscribe(removedObjectCallback, null, 'arrayChange');
  154. },
  155. error: function (jq, st, error) {
  156. console.log("error")
  157. alert(error);
  158. }
  159. });
  160. }
  161. downloadCourses();
  162.  
  163.  
  164. self.getStudentsGrades = function(student){
  165. var jsonStudent = ko.toJS(student);
  166. var index = jsonStudent["studentId"];
  167. self.modified = true;
  168. self.currentStudentIdx = index;
  169. downloadGrades();
  170. }
  171.  
  172.  
  173. function downloadGrades() {
  174. console.log("funkcja download grades");
  175. var jsonData = ko.toJS(self.gradesFilters);
  176. if (jsonData.value === "") {
  177. delete jsonData.value;
  178. delete jsonData.type;
  179. }
  180. if (jsonData.courseId === "") {
  181. delete jsonData.courseId;
  182. }
  183. if (jsonData.type === "") {
  184. delete jsonData.type;
  185. delete jsonData.value;
  186. }
  187. console.log("Do filtrowania ocen: ");
  188. console.log(jsonData);
  189.  
  190. //var index = self.currentStudent;
  191. //self.currentIdx = index;
  192. console.log("Sciezka: " + URL + 'students/' + self.currentStudentIdx + '/grades');
  193. $.ajax({
  194. type: 'GET',
  195. url: URL + 'students/' +self.currentStudentIdx + '/grades',
  196. contentType: "application/json",
  197. data: jsonData,
  198. dataType: "json",
  199. success: function (data) {
  200. self.gradesList.removeAll();
  201.  
  202. data.forEach(function (record) {
  203. self.gradesList.push(new ObservableObject(record));
  204. console.log(record);
  205. });
  206. self.gradeSubscription = self.gradesList.subscribe(removedObjectCallback, null, 'arrayChange');
  207.  
  208. },
  209. error: function (jq, st, error) {
  210. alert(error);
  211. }
  212. });
  213. window.location.href = '#grades';
  214.  
  215. }
  216.  
  217. self.newStudent = {
  218. name: ko.observable(),
  219. surname: ko.observable(),
  220. birth_date: ko.observable()
  221. };
  222.  
  223. self.newCourse = {
  224. subjectName: ko.observable(),
  225. teacherName: ko.observable()
  226. };
  227.  
  228. self.newGrade = {
  229. date: ko.observable(),
  230. value: ko.observable(),
  231. courseId: ko.observable()
  232. // course : {
  233. // id: ko.observable()
  234. // }
  235. };
  236.  
  237.  
  238. self.addNewStudent = function() {
  239. console.log("Nowy student: ");
  240. console.log(self.newStudent);
  241. console.log(URL+'students');
  242. $.ajax({
  243. url: URL + 'students',
  244. type: 'POST',
  245. dataType : "json",
  246. contentType: "application/json",
  247. data: ko.mapping.toJSON(self.newStudent)
  248. }).done(function(data) {
  249. console.log("Nowy student: ");
  250. console.log(data);
  251. self.studentsList.push(new ObservableObject(data));
  252.  
  253. self.newStudent.name("");
  254. self.newStudent.surname("");
  255. self.newStudent.birth_date("");
  256. });
  257. };
  258.  
  259. self.addNewCourse = function() {
  260. $.ajax({
  261. url: URL + 'subjects',
  262. type: 'POST',
  263. dataType : "json",
  264. contentType: "application/json",
  265. data: ko.mapping.toJSON(self.newCourse)
  266. }).done(function(data) {
  267. console.log("Nowy kurs: ");
  268. console.log(data);
  269. self.coursesList.push(new ObservableObject(data));
  270. self.newCourse.subjectName("");
  271. self.newCourse.teacherName("");
  272. });
  273. };
  274.  
  275. self.deleteStudent = function(student) {
  276. var jsonStudent = ko.toJS(student);
  277. var idx = jsonStudent["studentId"];
  278. $.ajax({
  279. url: URL + 'students/'+ idx,
  280. type: 'DELETE',
  281. dataType : "json",
  282. contentType: "application/json",
  283. }).done(function(data) {
  284. console.log("usuwam studenta")
  285. self.studentsList.remove(student);
  286. });
  287. };
  288.  
  289. self.deleteCourse = function(course) {
  290. var jsonCourse = ko.toJS(course);
  291. var id = jsonCourse["subject_id"];
  292. $.ajax({
  293. url: URL + 'subjects/'+ id,
  294. type: 'DELETE',
  295. dataType : "json",
  296. contentType: "application/json",
  297. }).done(function(data) {
  298. self.coursesList.remove(course);
  299. });
  300. };
  301.  
  302. self.deleteGrade = function(grade) {
  303. var jsonGrade = ko.toJS(grade);
  304. var id = jsonGrade["grade_id"];
  305. var urlTmp = resourceUrl(grade);
  306. var url = urlTmp.substring(1);
  307. console.log("Url w usuwaniu oceny ");
  308. console.log(url)
  309. $.ajax({
  310. url: URL + url,
  311. type: 'DELETE',
  312. dataType : "json",
  313. contentType: "application/json",
  314. }).done(function(data) {
  315. self.gradesList.remove(grade)
  316. });
  317. };
  318.  
  319. self.addNewGrade = function() {
  320. console.log("Nowa ocena: ");
  321. console.log(self.newGrade);
  322. $.ajax({
  323. url: URL + 'students/' + self.currentStudentIdx + '/grades',
  324. type: 'POST',
  325. dataType : "json",
  326. contentType: "application/json",
  327. data: ko.mapping.toJSON(self.newGrade)
  328. }).done(function(data) {
  329. self.gradesList.push(new ObservableObject(data));
  330. self.newGrade.value("");
  331. self.newGrade.date("");
  332. });
  333. };
  334.  
  335. Object.keys(self.studentFilters).forEach(function (key) {
  336. self.studentFilters[key].subscribe(function (val) {
  337. // Disable auto delete from database
  338. if (self.studentSubscription) {
  339. self.studentSubscription.dispose();
  340. }
  341. self.studentsList.removeAll();
  342. console.log("Download students from object");
  343. downloadStudents();
  344. });
  345. });
  346.  
  347. Object.keys(self.coursesFilters).forEach(function (key) {
  348. self.coursesFilters[key].subscribe(function (val) {
  349. if (self.courseSubscription) {
  350. self.courseSubscription.dispose();
  351. }
  352. self.coursesList.removeAll();
  353. console.log("Download courses from object");
  354. downloadCourses();
  355. });
  356. });
  357.  
  358. Object.keys(self.gradesFilters).forEach(function (key) {
  359. console.log("Jestem na zewnątrz object keys od grades");
  360.  
  361. self.gradesFilters[key].subscribe(function (val) {
  362. if (self.gradeSubscription) {
  363. self.gradeSubscription.dispose();
  364. }
  365. self.gradesList.removeAll();
  366. console.log("Download grades from object keys");
  367. downloadGrades();
  368. });
  369. });
  370.  
  371. }
  372.  
  373.  
  374. $(document).ready(function(){
  375. var model = new viewModel()
  376. ko.applyBindings(model);
  377. });
  378.  
  379.  
  380.  
  381.  
  382. function resourceUrl(record) {
  383. var links = record.link();
  384. var resourceUrl = links.find(function(link) {
  385. return link.params.rel() === "self"
  386. });
  387. return resourceUrl.href();
  388. }
  389.  
  390. function ObservableObject(data) {
  391. var self = this;
  392. ko.mapping.fromJS(data, {}, self);
  393.  
  394. ko.computed(function() {
  395. return ko.mapping.toJSON(self);
  396. }).subscribe(function(res) {
  397. var resource = ko.mapping.fromJSON(res);
  398. $.ajax({
  399. url: URL + resourceUrl(resource).substring(1),
  400. type: 'PUT',
  401. dataType : "json",
  402. contentType: "application/json",
  403. data: ko.mapping.toJSON(self)
  404. }).done(function(data) {
  405. console.log('Record updated');
  406. });
  407. });
  408. }
  409.  
  410. function removedObjectCallback(changes) {
  411. console.log("Remove oject callable - changes: ");
  412. console.log(changes);
  413. changes.forEach(function(change) {
  414. // Student / Course deleted from database
  415. if (change.status === 'deleted') {
  416. $.ajax({
  417. url: resourceUrl(change.value),
  418. type: 'DELETE',
  419. dataType : "json",
  420. contentType: "application/json"
  421. }).done(function() {
  422. console.log('Object removed from eStudent service');
  423. });
  424. }
  425. })
  426. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement