fixes to prior statements improvements
[openemr.git] / interface / product_registration / product_registration_controller.js
blob7a154e02cdbc4bb367377153e999ba27d6a8578c
1 /**
2  * ProductRegistrationController (JavaScript)
3  *
4  * LICENSE: This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
14  *
15  * @package OpenEMR
16  * @author  Matthew Vita <matthewvita48@gmail.com>
17  * @link    http://www.open-emr.org
18  */
20 "use strict";
21 function ProductRegistrationController() {
22     var self = this;
24     var _productRegistrationService = new ProductRegistrationService();
26     self.getProductRegistrationStatus = function(callback) {
27         _productRegistrationService.getProductStatus(function(err, data) {
28             if (err) { return callback(err, null); }
30             callback(null, data);
31         });
32     };
34     self.showProductRegistrationModal = function() {
35         _displayFormView();
36     };
38     var _displayFormView = function() {
39          // Workaround to get i18n keys
40          var buttonObject = {};
41          buttonObject[registrationTranslations.submit] = _formSubmissionHandler;
42          buttonObject[registrationTranslations.noThanks] = _formCancellationHandler;
44         jQuery('.product-registration-modal').dialog({
45             resizable: false,
46             height: "auto",
47             width: 400,
48             draggable: false,
49             title: registrationTranslations.title,
50             modal: true,
51             buttons: buttonObject,
52             closeText: registrationTranslations.closeTooltip
53         });
55         // Wire up "enter key" handler in case user doesn't click the modal buttons manually
56         jQuery('.product-registration-modal .email').on('keypress', function(event) {
57             if (event.which == 13) {
58                 _formSubmissionHandler();
59                 return false;
60             }
61         });
62     };
64     var _formSubmissionHandler = function() {
65         var email = jQuery('.product-registration-modal .email').val() || '';
67         if (email === '' || email.indexOf('@') < 0) {
68             jQuery('.product-registration-modal .message').text(registrationTranslations.pleaseProvideValidEmail);
69         } else {
70             jQuery('.product-registration-modal .message').text('');
72             _productRegistrationService.submitRegistration(email, function(err, data) {
73                 if (err) { return _registrationFailedHandler(err); }
75                 _registrationCreatedHandler(data);
76             });
77         }
78     };
80     // If we are on the about_page, show the registration data.
81     self.displayRegistrationInformationIfDivExists = function(data) {
82       if (jQuery('.product-registration').size() > 0) {
83           jQuery('.product-registration .email').text(registrationTranslations.registeredEmail + ' ' + data.email);
84           jQuery('.product-registration .id').text(registrationTranslations.registeredId + ' ' + data.registration_id);
85       }
86     };
88     var _formCancellationHandler = function() {
89       _closeModal();
91       // Note: not checking output here (don't want to bug the user more this session
92       // after they said "no thanks" to the modal). If anything goes wrong, it will be silent.
93       // The only reasons why this would fail would be because of no connection or our server
94       // is down.
95       _productRegistrationService.submitRegistration(false);
96     };
98     var _registrationCreatedHandler = function(data) {
99         jQuery('.product-registration-modal').dialog('option', {
100             buttons: {},
101             title: registrationTranslations.success
102         });
104         jQuery('.product-registration-modal .context').remove();
105         jQuery('.product-registration-modal .email').remove();
106         jQuery('.product-registration-modal .message').text(registrationTranslations.registeredSuccess);
107         _closeModal(2500);
108         self.displayRegistrationInformationIfDivExists(data);
109     };
111     var _registrationFailedHandler = function(error) {
112         jQuery('.product-registration-modal .message').text(error);
113     };
115     var _closeModal = function(closeWaitTimeMilliseconds) {
116       setTimeout(function() {
117         jQuery('.product-registration-modal').dialog('close');
118       }, closeWaitTimeMilliseconds || 0);
119     };