File size: 7,334 Bytes
b3787d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';

(function (angular) {
  angular.module('smoochChatPluginWidget', ['ui.bootstrap', 'smoochChatModals'])
    .run(function ($window, $rootScope) {
      $rootScope.online = navigator.onLine;
      $window.addEventListener("offline", function () {
        $rootScope.$apply(function () {
          $rootScope.online = false;
        });
      }, false);
      $window.addEventListener("online", function () {
        $rootScope.$apply(function () {
          $rootScope.online = true;
        });
      }, false);
    })
    .controller('WidgetHomeCtrl', ['$scope', 'Buildfire', 'DataStore', 'TAG_NAMES', 'STATUS_CODE', '$rootScope', '$modal', '$timeout',
      function ($scope, Buildfire, DataStore, TAG_NAMES, STATUS_CODE, $rootScope, $modal, $timeout) {
        var WidgetHome = this;
        WidgetHome.data = null;
        WidgetHome.invalidApiKey = false;
        WidgetHome.apiKey = "";
        WidgetHome.instanceId = null;
        WidgetHome.CHAT_TYPE = {
          PUBLIC: "Public",
          PRIVATE: "Private"
        };

        /*Init method call, it will bring all the pre saved data*/
        WidgetHome.init = function () {
          WidgetHome.updateColorTheme = false;
          WidgetHome.success = function (result) {
            console.info('init success result:', result);
            WidgetHome.data = {};
            if (result.data) {
              WidgetHome.data = result.data;
              if (!WidgetHome.data.settings)
                WidgetHome.data.settings = {};
              WidgetHome.apiKey = WidgetHome.data.settings.apiKey;
            }
            Buildfire.spinner.show();
            if (WidgetHome.apiKey)
              initializeSmooch();
          };

          WidgetHome.error = function (err) {
            if (err && err.code !== STATUS_CODE.NOT_FOUND) {
              console.error('Error while getting data', err);
            }
          };
          DataStore.get(TAG_NAMES.SMOOCH_CHAT_INFO).then(WidgetHome.success, WidgetHome.error);
        };
        WidgetHome.init();

        function initializeSmooch() {

          Buildfire.getContext(function (err, data) {
            var initObj = {
              appToken: WidgetHome.apiKey,
              customText: {headerText: WidgetHome.data.settings.headerText || "How can we help?"}
            };
            Buildfire.auth.getCurrentUser(function (err, user) {
              var smoochApp;
              console.log("_______________________", user);
              if (user) {
                initObj.givenName = user.displayName;
                initObj.email = user.email;
              }
              if (data && data.instanceId) {
                WidgetHome.instanceId = data.instanceId;
                initObj.userId = data.instanceId;
              }
              if (WidgetHome.data.settings && WidgetHome.data.settings.type === WidgetHome.CHAT_TYPE.PRIVATE) {
                if (user) {
                  initObj.userId = user._id;
                  smoochApp = Smooch.init(initObj)
                } else {
                  try {
                    var randomId = JSON.parse(localStorage.getItem('smoochUserRandomId'));
                  } catch (e) {
                    console.log("Error while parsing value: ", e);
                  }
                  console.log("****************", randomId);
                  if (randomId && randomId.id) {
                    initObj.userId = randomId.id;
                    smoochApp = Smooch.init(initObj)
                  }
                  else if (data && data.instanceId) {
                    initObj.userId = data.instanceId + Date.now() + Math.random();
                    localStorage.setItem('smoochUserRandomId', JSON.stringify({id: initObj.userId}));
                    smoochApp = Smooch.init(initObj)
                  }
                }
              } else {
                smoochApp = Smooch.init(initObj)
              }


              smoochApp && smoochApp.then(function (res) {
                Buildfire.spinner.hide();
                if (res && res._id) {
                  WidgetHome.invalidApiKey = false;
                  $('#sk-header').click(function (event) {
                    event.stopPropagation();
                  });
                  $("#sk-footer form a").bind('taphold', function (event) {
                    event.preventDefault();
                  });

                  Smooch.open();
                  $scope.$digest();
                }


              }, function (err) {
                console.log("??????????????", err);
                if (err && err.response && err.response.status == 401) {
                  Buildfire.spinner.hide();
                  WidgetHome.invalidApiKey = true;
                  $scope.$digest();
                }
                Smooch.destroy();
              });
            });
          });
        }

        Buildfire.messaging.onReceivedMessage = function (event) {
          switch (event && event.name) {
            case STATUS_CODE.UPDATED :
              WidgetHome.data = event.data;
              $('#sk-header').click(function (event) {
                event.stopPropagation();
              });
              $("#sk-footer form a").bind('taphold', function (event) {
                event.preventDefault();
              });
              break;
            case STATUS_CODE.SETTINGS_UPDATED :
              WidgetHome.data = event.data;
              WidgetHome.apiKey = WidgetHome.data && WidgetHome.data.settings && WidgetHome.data.settings.apiKey;
              if (WidgetHome.apiKey)
                initializeSmooch();
              else
                Smooch.destroy();
              break;
          }
        };

        $scope.$watch('online', function (newStatus) {
          if (newStatus) {
            $('.send').off("click");
          } else {
            $('.send').on("click", function (e) {

              console.log('------------->INTERNET CONNECTION PROBLEM');
              $modal
                .open({
                  template: [
                    '<div class="padded clearfix">',
                    '<div class="content text-center">',
                    '<p>No internet connection was found. please try again later</p>',
                    '<a class="margin-zero"  ng-click="ok(option)">OK</a>',
                    '</div>',
                    '</div></div>'
                  ].join(''),
                  controller: 'MoreOptionsModalPopupCtrl',
                  controllerAs: 'MoreOptionsPopup',
                  size: 'sm'

                });
              $timeout(function () {
                $('.modal').css('z-index', 9999);
                $('.modal-backdrop').css('z-index', 9999);

              }, 0);

              e.stopPropagation();
            });
          }
        });

        var loginCallback = function () {
          if (WidgetHome.apiKey)
            initializeSmooch();
        };

        /**
         * onLogin() listens when user logins using buildfire.auth api.
         */
        Buildfire.auth.onLogin(loginCallback);


        /*
         * Enable pull down to refresh and fetch fresh data
         */

        buildfire.datastore.onRefresh(function () {
          if (WidgetHome.apiKey)
            initializeSmooch();
        });

      }]);
})(window.angular);