PageSourceSearch

https://retainyourbrain.com/components/admin/routine/routineLink.controller.js

js retainyourbrain.com collected 2026-09-28 08:38:01 UTC 10,092 bytes, 213 lines download raw bytes

1(function () {
2    'use strict';
3
4    angular.module('saddleApp.controllers')
5        .controller('adminRoutineLink', adminRoutineLink);
6
7    adminRoutineLink.$inject = ["$scope", "$http", "$window", "$q", "$state", "$stateParams", "$exceptionHandler", "$filter", "utilsFactory", "requestFactory","linkedRoutines","topicRoutines","fromRoutine","categoryTopics", "routineConditionRoutines", "routineCompleteResponses"];
8
9    function adminRoutineLink($scope, $http, $window, $q, $state, $stateParams, $exceptionHandler, $filter, utilsFactory, requestFactory, linkedRoutines, topicRoutines, fromRoutine,categoryTopics, routineConditionRoutines, routineCompleteResponses) {
10           
11        var vm = this;
12        vm.linkedRoutines = linkedRoutines;
13        vm.topicRoutines = topicRoutines;
14        vm.topicRoutinesAll = topicRoutines;
15        vm.fromRoutine = fromRoutine;
16        vm.routineConditionRoutines = routineConditionRoutines;
17        vm.routineCompleteResponses = routineCompleteResponses;
18        vm.fromRoutineConditionRoutines = routineConditionRoutines.filter(x => x.routineId === parseInt($stateParams.routineId));
19        vm.selectedRoutines = []; 
20        vm.selectedTopics = [];
21        vm.thisRelativePath = 'components/routine/views/';
22        vm.status = false;
23        vm.categoryTopics = categoryTopics;
24        vm.categoryTopicsAll = categoryTopics;
25        vm.haveSameResponse = true;
26        vm.sameResponseMessage = [];
27
28        let createdUnixTimestamp = utilsFactory.nowUnixTimestamp();
29
30        vm.reloadAvailableRoutines = function () {
31            if (vm.linkedRoutines.length > 0) {
32                vm.topicRoutines = vm.topicRoutinesAll?.filter(function (tr) {
33                    return !vm.linkedRoutines.some(function (s) {
34                        return tr.id === s.toRoutineId
35                    });
36                });
37            } else {
38                vm.topicRoutines = vm.topicRoutinesAll;
39            }
40        }
41        vm.reloadAvailableRoutines();
42
43        vm.linkRoutine = function(){
44            vm.sameResponseMessage = [];
45            vm.selectedRoutines = vm.topicRoutines.filter(function (f) {
46                return f.selected;
47            }).map(function (m) {
48                // check if each selected routine's complete response matches the linked routine complete response.
49                const toRoutineConditionRoutines = vm.routineConditionRoutines.filter(x => x.routineId === parseInt(m.id));
50
51                /* construct an object to hold routineConditionRoutineId, routineConditionId and updatedResponseOptionId of the fromRoutineConditionRoutines object 
52                   and routineCompleteResponses object  */
53                vm.fromRoutineCombined = vm.fromRoutineConditionRoutines.map(f => ({
54                    routineConditionRoutineId: f.id,
55                    routineConditionId: f.routineConditionId,
56                    updatedResponseOptionId: vm.routineCompleteResponses.find(i => i.routineConditionRoutineId == f.id)?.updatedResponseOptionId,
57                    optionText: f.optionText
58                }));
59
60                /* construct an object to hold routineConditionRoutineId, routineConditionId and updatedResponseOptionId of the toRoutineConditionRoutines object 
61                   and routineCompleteResponses object  */
62                vm.toRoutineCombined = toRoutineConditionRoutines.map(t => ({
63                    routineConditionRoutineId: t.id,
64                    routineConditionId: t.routineConditionId,
65                    updatedResponseOptionId: vm.routineCompleteResponses.find(i => i.routineConditionRoutineId == t.id)?.updatedResponseOptionId,
66                    optionText: t.optionText
67                }));
68
69                // check if to object contains the same routine conditions and routine complete responses as from object
70                vm.haveSameResponse = vm.fromRoutineCombined.every(f => vm.toRoutineCombined.find(t => t.routineConditionId == f.routineConditionId && t.updatedResponseOptionId == f.updatedResponseOptionId)); 
71
72                if (!vm.haveSameResponse) {
73                    vm.sameResponseMessage.push(m.name);
74                    return;
75                } else {
76                    /* enable the link selection when there are more than 1 responses (conditions) exist on the fromRoutine object. this is how we determine the 
77                       routineConditionRoutineId between the from and to routines. */
78                    vm.enableLinkSelection = vm.fromRoutineCombined.length > 1 ? true : false;
79                    return { fromRoutineId: vm.fromRoutine.id, toRoutineId: m.id, created: createdUnixTimestamp };
80                }
81            });
82            if (!vm.enableLinkSelection && vm.haveSameResponse) {
83                // since there is only one possible condition to link add it
84                const routineCondition = {
85                    routineConditionRoutineId: vm.fromRoutineCombined.routineConditionRoutineId
86                };
87                vm.submitLinkRoutine(routineCondition);
88            }
89        }  
90
91        vm.submitLinkRoutine = function (routineCondition) {
92            const selectedRoutinesToPost = vm.selectedRoutines.map(i => ({ 
93                fromRoutineId: i.fromRoutineId, 
94                toRoutineId: i.toRoutineId, 
95                created: i.created, 
96                routineConditionRoutineId: routineCondition.routineConditionRoutineId
97            }));
98            requestFactory.post('routineLink', selectedRoutinesToPost).then(function(response){
99                /* record successfully added */
100                if(response.status == 201){
101                    vm.getLinkedRoutines();
102                    vm.status = false;
103                    vm.enableLinkSelection = false;
104                }
105            })
106            .catch(function(data, status) {
107                console.error('post submit error', status, data);
108            });  
109        }
110
111        vm.unlinkRoutine = function(){
112            vm.selectedRoutines = vm.linkedRoutines.filter(function (f) {
113                return f.selected;
114            });
115            vm.selectedRoutines.forEach(function (s) {
116                requestFactory.delete('routineLink', s.routineLinkId).then(function(response){
117                    if(response.status == 200) {
118                        vm.getLinkedRoutines();
119                        vm.selectedRoutines = [];
120                        vm.status = false;
121                    }
122                })
123                .catch(function(data, status) {
124                    console.error('post submit error', status, data);
125                });    
126            });
127        }  
128
129        vm.getLinkedRoutines = function () {
130            requestFactory.getAll('routineLink', {'routineId': parseInt($stateParams.routineId)}).then(function(response){
131                try {
132                    if(response.data && response.data.length > 0){
133                        vm.linkedRoutines = response.data;
134                        vm.reloadAvailableRoutines();
135                    } 
136                    else{
137                        throw new Error('linked routines not found');    
138                    }
139                } catch(error){
140                    $exceptionHandler(error);
141                }
142            });
143        }
144
145        vm.disableBtn = function(button){
146            switch(button){
147                case 'link':
148                    return !vm.topicRoutines?.some(function(e){
149                        return e.selected;
150                    });
151                case 'unlink':
152                    return !vm.linkedRoutines?.some(function(e){
153                        return e.selected;
154                    });
155                 default:
156                     return true;
157            }
158        }
159        
160        //for toggle
161        vm.changeStatus = function() {
162            vm.status = !vm.status;
163        }
164
165        vm.checkTopic = function () {
166            vm.selectedTopics = vm.categoryTopics.filter(function (f) {
167                return f.selected;
168            });
169            vm.selectedTopics.forEach(x => {
170                requestFactory.getAll('routineLinkRoutines', { 'topicId': parseInt(x.id) }).then(function(response){
171                    try {
172                        if (vm.topicRoutines.filter(t => t.topicId != x.id)) {
173                            if(response.data && response.data.length){
174                                if(response.data)response.data.map(s=>s.notSaved= true);
175                                // Array.prototype.push.apply(vm.topicRoutines, response.data);
176                                Array.prototype.push.apply(vm.topicRoutinesAll, response.data);
177                                vm.reloadAvailableRoutines();
178                                vm.reloadCategoryTopics();
179                            } 
180                            else{
181                                throw new Error('linked routines not found');    
182                            }
183                        }
184                        
185                    } catch(error){
186                        $exceptionHandler(error);
187                    }
188                });
189            })
190            
191        }
192
193        vm.reloadCategoryTopics = function () {
194            if (vm.categoryTopics.length > 0) {
195                vm.categoryTopics = vm.categoryTopicsAll?.filter(function (tr) {
196                    return !vm.topicRoutines.some(function (s) {
197                        return tr.id === s.topicId
198                    });
199                });
200            } else {
201                vm.topicRoutines = vm.topicRoutinesAll;
202            }
203        }
204
205        vm.removeCategoryTopic = function (name) {
206            const removedTopic = vm.categoryTopicsAll.filter(x => x.name == name);
207            removedTopic.map(s => s.selected = false);
208            vm.topicRoutines = vm.topicRoutines.filter(x => x.topicName != name);
209            vm.topicRoutinesAll = vm.topicRoutinesAll.filter(x => x.topicName != name);
210            Array.prototype.push.apply(vm.categoryTopics, removedTopic);
211        }
212    }
213})();

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.