New onsite patient portal, take 4.
[openemr.git] / portal / patient / scripts / app / onsiteactivityviews.js
blob45a3ad66f810e3fd7ef7b2b09c9346142ba69b76
1 /**
2  * View logic for OnsiteActivityViews
3  *
4  * application logic specific to the OnsiteActivityView listing page
5  *
6  * Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
7  *
8  * LICENSE: This program is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU Affero General Public License as
10  *  published by the Free Software Foundation, either version 3 of the
11  *  License, or (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU Affero General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Affero General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * @package OpenEMR
22  * @author Jerry Padgett <sjpadgett@gmail.com>
23  * @link http://www.open-emr.org
24  */
25 var actpage = {
27         onsiteActivityViews: new model.OnsiteActivityViewCollection(),
28         collectionView: null,
29         onsiteActivityView: null,
30         modelView: null,
31         isInitialized: false,
32         isInitializing: false,
33         fetchParams: { filter: '', orderBy: 'patientId', orderDesc: 'DESC', page: 1, status: 'waiting' },
34         fetchInProgress: false,
35         dialogIsOpen: false,
37         init: function() {
38                 // ensure initialization only occurs once
39                 if (actpage.isInitialized || actpage.isInitializing) return;
40                 actpage.isInitializing = true;
42                 if (!$.isReady && console) console.warn('page was initialized before dom is ready.  views may not render properly.');
44                 // make the return button clickable
45                 $("#returnHome").click(function(e) {
46                         e.preventDefault();
47                         window.location.href = './provider';
48                 });
49                 function showPaymentModal(cpid,recid){
50                          var title = 'Patient Online Payment';
51                          var params = {
52                                 buttons: [
53                                                    { text: 'Help', close: false, style: 'info btn-sm',id: 'formHelp'},
54                                                    { text: 'Cancel', close: true, style: 'default btn-sm'},
55                                    //{ text: 'Download', close: false, style: 'success btn-sm',id:'downloadTemplate'},
56                                    { text: 'Done', style: 'danger btn-sm', close:true}],
57                                 size: eModal.size.xl,
58                                 subtitle: 'Provider Audit.',
59                                 title: title,
60                                 useBin: false,
61                                 url: './../portal_payment.php?pid='+cpid+'&user='+cuser+'&recid='+recid
62                             };
63                         return eModal.ajax(params)
64                             .then(function () { });
65                  };
66                 function showDocumentModal(cpid,recid){
67                          var title = 'Patient Documents';
68                          var params = {
69                                 buttons: [
70                                                    { text: 'Help', close: false, style: 'info btn-sm',id: 'formHelp'},
71                                                    { text: 'Cancel', close: true, style: 'default btn-sm'},
72                                    //{ text: 'Download', close: false, style: 'success btn-sm',id:'downloadTemplate'},
73                                    { text: 'Done', style: 'danger btn-sm', close:true}],
74                                 size: eModal.size.xl,
75                                 subtitle: 'Provider Audit.',
76                                 title: title,
77                                 useBin: false,
78                                 url: './onsitedocuments?pid='+cpid+'&user='+cuser+'&recid='+recid
79                             };
80                         return eModal.iframe(params)
81                             .then(function () { });
82                  };
83                 function showProfileModal(cpid){
84                          var title = 'Demographics Legend Red: Chart Values. Blue: Patient Edits.';
85                          var params = {
86                                 buttons: [
87                                                    { text: 'Help', close: false, style: 'info btn-sm',id: 'formHelp'},
88                                                    { text: 'Cancel', close: true, style: 'default btn-sm'},
89                                    { text: 'Revert Edits', close: false, style: 'success btn-sm',id:'replaceAllButton'},
90                                    { text: 'Commit to Chart', style: 'danger btn-sm', close: false,id:'savePatientButton'}],
91                                 size: eModal.size.xl,
92                                 subtitle: 'Provider Audit.',
93                                 title: title,
94                                 useBin: false,
95                                 url: './patientdata?pid='+cpid+'&user='+cuser
96                             };
97                         return eModal.ajax(params)
98                             .then(function () { });
99                  };
101                  $(document.body).on('hidden.bs.modal', function (){
102                                 window.location.href = './onsiteactivityviews';
103                         });
105                 // initialize the collection view
106                 this.collectionView = new view.CollectionView({
107                         el: $("#onsiteActivityViewCollectionContainer"),
108                         templateEl: $("#onsiteActivityViewCollectionTemplate"),
109                         collection: actpage.onsiteActivityViews
110                 });
112                 // initialize the search filter
113                 $('#filter').change(function(obj) {
114                         actpage.fetchParams.filter = $('#filter').val();
115                         actpage.fetchParams.page = 1;
116                         actpage.fetchOnsiteActivityViews(actpage.fetchParams);
117                 });
119                 // make the rows clickable ('rendered' is a custom event, not a standard backbone event)
120                 this.collectionView.on('rendered',function(){
121                         // attach click handler to the table rows for selection
122                         $('table.collection tbody tr').click(function(e) {
123                                 e.preventDefault();
124                                 var m = actpage.onsiteActivityViews.get(this.id);
125                                 var cpid = m.get('patientId');
126                                 var activity = m.get('activity');
127                                 var recid = m.get('tableArgs');
128                                 if(activity == 'document')
129                                         showDocumentModal(cpid,recid);
130                                 else if(activity == 'profile')
131                                         showProfileModal(cpid);
132                                 else if(activity == 'payment')
133                                         showPaymentModal(cpid);
134                         });
136                         // make the headers clickable for sorting
137                         $('table.collection thead tr th').click(function(e) {
138                                 e.preventDefault();
139                                 var prop = this.id.replace('header_','');
141                                 // toggle the ascending/descending before we change the sort prop
142                                 actpage.fetchParams.orderDesc = (prop == actpage.fetchParams.orderBy && !actpage.fetchParams.orderDesc) ? '1' : '';
143                                 actpage.fetchParams.orderBy = prop;
144                                 actpage.fetchParams.page = 1;
145                                 actpage.fetchOnsiteActivityViews(actpage.fetchParams);
146                         });
148                         // attach click handlers to the pagination controls
149                         $('.pageButton').click(function(e) {
150                                 e.preventDefault();
151                                 actpage.fetchParams.page = this.id.substr(5);
152                                 actpage.fetchOnsiteActivityViews(actpage.fetchParams);
153                         });
155                         actpage.isInitialized = true;
156                         actpage.isInitializing = false;
157                 });
159                 this.fetchOnsiteActivityViews({ filter: '', orderBy: 'Date', orderDesc: 'DESC', page: 1, status: 'waiting' });
161                 // initialize the model view
162                 this.modelView = new view.ModelView({
163                         el: $("#onsiteActivityViewModelContainer")
164                 });
166                 this.modelView.templateEl = $("#onsiteActivityViewModelTemplate");
168                 if (model.longPollDuration > 0) {
169                         setInterval(function () {
171                                 if (!actpage.dialogIsOpen)      {
172                                         actpage.fetchOnsiteActivityViews(actpage.fetchParams,true);
173                                 }
175                         }, model.longPollDuration);
176                 }
177         },
179         /**
180          * Fetch the collection data from the server
181          * @param object params passed through to collection.fetch
182          * @param bool true to hide the loading animation
183          */
184         fetchOnsiteActivityViews: function(params, hideLoader) {
185                 // persist the params so that paging/sorting/filtering will play together nicely
186                 //params.status = 'waiting';
187                 actpage.fetchParams = params;
189                 if (actpage.fetchInProgress) {
190                         if (console) console.log('supressing fetch because it is already in progress');
191                 }
193                 actpage.fetchInProgress = true;
195                 if (!hideLoader) app.showProgress('loader');
197                 actpage.onsiteActivityViews.fetch({
199                         data: params,
201                         success: function() {
202                                 if (actpage.onsiteActivityViews.collectionHasChanged) {
203                                         // TODO: add any logic necessary if the collection has changed
204                                         // the sync event will trigger the view to re-render
205                                 }
206                                 app.hideProgress('loader');
207                                 actpage.fetchInProgress = false;
208                         },
209                         error: function(m, r) {
210                                 app.appendAlert(app.getErrorMessage(r), 'alert-error',0,'collectionAlert');
211                                 app.hideProgress('loader');
212                                 actpage.fetchInProgress = false;
213                         }
214                 });
215         },
217         /**
218          * show the dialog for editing a model
219          * @param model
220          */
221         showDetailDialog: function(m) {
223                 // show the modal dialog
224                 $('#onsiteActivityViewDetailDialog').modal({ show: true });
226                 // if a model was specified then that means a user is editing an existing record
227                 // if not, then the user is creating a new record
228                 actpage.onsiteActivityView = m ? m : new model.OnsiteActivityViewModel();
230                 actpage.modelView.model = actpage.onsiteActivityView;
232                 if (actpage.onsiteActivityView.id == null || actpage.onsiteActivityView.id == '') {
233                         // this is a new record, there is no need to contact the server
234                         actpage.renderModelView(false);
235                 } else {
236                         app.showProgress('modelLoader');
238                         // fetch the model from the server so we are not updating stale data
239                         actpage.onsiteActivityView.fetch({
241                                 success: function() {
242                                         // data returned from the server.  render the model view
243                                         actpage.renderModelView(true);
244                                 },
246                                 error: function(m, r) {
247                                         app.appendAlert(app.getErrorMessage(r), 'alert-error',0,'modelAlert');
248                                         app.hideProgress('modelLoader');
249                                 }
251                         });
252                 }
253         },
255         /**
256          * Render the model template in the popup
257          * @param bool show the delete button
258          */
259         renderModelView: function(showDeleteButton)     {
260                 actpage.modelView.render();
262                 app.hideProgress('modelLoader');
263                 // initialize any special controls
264                 try {
265                         $('.date-picker')
266                                 .datepicker()
267                                 .on('changeDate', function(ev){
268                                         $('.date-picker').datepicker('hide');
269                                 });
270                 } catch (error) {
271                         // this happens if the datepicker input.value isn't a valid date
272                         if (console) console.log('datepicker error: '+error.message);
273                 }
274                 if (showDeleteButton) {
275                         // attach click handlers to the delete buttons
276                         $('#deleteOnsiteActivityViewButton').click(function(e) {
277                                 e.preventDefault();
278                                 $('#confirmDeleteOnsiteActivityViewContainer').show('fast');
279                         });
281                         $('#cancelDeleteOnsiteActivityViewButton').click(function(e) {
282                                 e.preventDefault();
283                                 $('#confirmDeleteOnsiteActivityViewContainer').hide('fast');
284                         });
286                         $('#confirmDeleteOnsiteActivityViewButton').click(function(e) {
287                                 e.preventDefault();
288                                 actpage.deleteModel();
289                         });
291                 } else {
292                         // no point in initializing the click handlers if we don't show the button
293                         $('#deleteOnsiteActivityViewButtonContainer').hide();
294                 }
295         },