buli
2023-06-05 e9b970ea36eea5dcf93fd5b965bf13d7010ce0ad
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<apex:component >
<script>
;
'use strict';
angular.module('alController', ['alSfdcService', 'alImageService'])
 
.controller('TopCtrl', ['$scope', '$q', '$location', '$log', 'alAppConfig', 'alPhotos', 'sfdcService',
  function ($scope, $q, $location, $log, alAppConfig, alPhotos, sfdcService) {
      //alert('in topctrl!');
  $scope.working = true;
  $scope.config = alAppConfig;
  $scope.land = {}; //土地情報
  $scope.typeSelecter = {};
  // 土地情報の初期化
  $scope.initLnad = function() {
    $scope.land = {
     'name': new Date()
     , 'status' :  null
     , 'address' : ''
     , 'latLng': {'latitude': null, 'longitude':null}
     , 'pid' : ''
     , 'photoType' : ''
   };
   $scope.type_selecter = {
     'ra_type' : ''
     , 'rp_type' : ''
     , 'ast_type' : ''
   };
 };
 $scope.initLnad();
 
  ///  サービスの呼び出し用関数たち
  $scope.config['longSidePixcel'] = {!$Label.s1_longSidePixel};
  $scope.config['maxFileSize'] = 700000;  // ファイルサイズ上限
  $scope.config['jpegQuality'] = {!$Label.s1_jpegQuality};  //  JPEGの品質レベル  1/10 して canvas.toDataURL()の第2引数として使う。
  $scope.loading = false;
  $scope.working = false;
  
}])
// 土地情報入力
.controller('LandCtrl', ['$scope', '$log', '$q', '$state', 'alGps',  '$stateParams',
  function ($scope, $log, $q, $state, alGps,  $stateParams) {
  $scope.nameFilter = null;
  $scope.loading = true;
  $scope.land.pid = $stateParams.pid;
  
      //alert('in landctrl!   '+$stateParams.pid+'     '+$scope.land.pid);    
 
  var getLandStatus = function(value) {
    var status =  null;
    return status;
  }
  
  // GPSで位置情報を取る
  var resolvePosition = function() {
    var defer = $q.defer();
    if (!$scope.land.latLng['latitude'] || !$scope.land.latLng['longitude']) {
      alGps.getCurrentLocation(
        function(res) {
          $scope.land.latLng['latitude'] = res.lat;
          $scope.land.latLng['longitude'] = res.lng;
          $scope.$apply();
          defer.resolve();
        },
        function() {
          defer.reject();
      });
    }
    else {
      defer.resolve();
    }
    return defer.promise;
  }
 
  $scope.loading = false;
 
  // 次へ ボタン クリック
  $scope.next = function() {
    if($scope.type_selecter.ra_type || $scope.type_selecter.rp_type || $scope.type_selecter.ast_type){
      $state.go('menu.send');
    } else {
      alert('请输入照片种类');
    }
  }
}])
// 画面 写真選択、GPS現在位置取得、送信
.controller('SendCtrl', ['$scope', '$log', '$q', '$location', 'alPhotos', 'alHistory', 'alGps', 'alModal', 'sfdcService','$stateParams',
  function ($scope, $log, $q,$location, alPhotos, alHistory, alGps, alModal, sfdcService, $stateParams) {
  $scope.editMode = false;
  $scope.photos = alPhotos.all();
      
   switch ($scope.land.pid.substr(0,3)){
       case 'a0t':
            $scope.land.photoType = $scope.type_selecter.ra_type;
            break;
       case 'a0J':
            $scope.land.photoType = $scope.type_selecter.rp_type;
            break;
       case '02i':
            $scope.land.photoType = $scope.type_selecter.ast_type;
            break;
  }
      
  if (angular.isUndefined($scope.land.latLng)) {
    $scope.land.latLng = {};
  }
  // 送信中ステーす情報の初期化関数
  var initUploadStatus = function() {
    $scope.showOption = false;
    $scope.status ={"title": "送信中", "msg": "", "finished": false, "success": false, "showLog": false, "logs": [], "max":0, "value":0};
  }
 
  // SFDC 添付ファイル追加
  var createAttachment = function(id, photo, photoType, dt, longitude, latitude) {
    //alert('~~~~~ longitude='+longitude+'\nlatitude='+latitude);
    var defer = $q.defer();
    $scope.status.logs.push((new Date()).toLocaleString() + ': ' + photo.no + '枚目 写真登録開始');
    sfdcService.createAttachment(
      id, photo, photoType, dt, longitude, latitude,
      function (response) {
        $scope.status.logs.push((new Date()).toLocaleString() + ': ' + photo.no + '枚目 写真登録完了 ID=' + response.id);
        $scope.$apply();
        $log.info($scope.status.logs[$scope.status.logs.length-1]);
        var res = {"attachmentId": response.id};
        defer.resolve(res);
      },
      function(e) {
        $scope.status.logs.push((new Date()).toLocaleString() + ': ' + photo.no + '枚目 写真登録失敗');
        $scope.status.logs.push('statusCode: ' + e.statusCode + ', type: ' + e.type + ', message: ' + e.message);
        if(e.status){
            $scope.errMsg = '发生了以下错误。请联系系统管理员。\n'+e.result.error;
        }else{
            $scope.errMsg = '请检查网络连接。';
        }
        
        $scope.$apply();
        $log.error($scope.status.logs[$scope.status.logs.length-1]);
        defer.reject();
    });
    return defer.promise;
  };
 
  // 送信処理
  var sfdcUpload = function(log, photos) {
    log.name = $scope.land.name;
    log.location.lat = $scope.land.latLng.latitude;
    log.location.lng = $scope.land.latLng.longitude;
      
    // 写真の登録
    
    var photo = photos[0];
    var promise = createAttachment($scope.land.pid, photo, $scope.land.photoType, $scope.land.name, $scope.land.latLng.longitude, $scope.land.latLng.latitude);
    promise.then(
        function(res) {
          log.attachmentIds.push(res.Id);
            log.success = true;
            //alHistory.append(log);
            $scope.status.msg = "上传成功。";
            $scope.status.finished =true;
            $scope.status.success = true;
        },function(err){
          log.success = false;
          //alHistory.append(log);
          $scope.status.msg = "上传失败。"+$scope.errMsg;
          $scope.status.finished =true;
          $scope.status.success = false;
    });
  }
 
  var openProgress = function(photos) {
    $scope.status.max = 2 + photos.length;
    alModal.openProgress($scope).then(
      function(res) {
        // 成功
        if ($scope.status.success) {    // 成功終了
          alPhotos.clear();
          $scope.initLnad();
          $location.path('/menu/land');
        }
        initUploadStatus();
      }
    );
  }
  
  var resolvePosition = function() {
    var defer = $q.defer();
    if (!$scope.land.latLng['latitude'] || !$scope.land.latLng['longitude']) {
      $scope.status.logs.push((new Date()).toLocaleString() + ': ' + '位置情報取得開始');
      alGps.getCurrentLocation(
        function(res) {
          $scope.status.value++;
          $scope.land.latLng['latitude'] = res.lat;
          $scope.land.latLng['longitude'] = res.lng;
          $scope.status.logs.push((new Date()).toLocaleString() + ': ' + '位置情報取得完了 ' + $scope.land.latLng['latitude'] + ',' + $scope.land.latLng['longitude']);
          $scope.$apply();
          defer.resolve();
        },
        function() {
          $scope.status.logs.push((new Date()).toLocaleString() + ': ' + '位置情報取得失敗');
          $scope.status.msg = "无法取得现在位置。请开启设备的定位服务(GPS),并重新启动本应用";  // 2015.08.03 追加
          $scope.status.finished =true;     // 2015.08.03 追加
          $scope.status.success = false;    // 2015.08.03 追加
          $scope.$apply();
          defer.reject();
      });
    }
    else {
      defer.resolve();
    }
    return defer.promise;
  }
 
  // 送信ボタンを押した後の処理(確認画面を表示)
  /*$scope.upload = function() {
    $log.info('upload');
    var photos = alPhotos.all();
    // 確認して
    alModal.openConfirm($scope, 'confirmSend.html').then(
      function() {
        // OK
        // モーダル表示
        $scope.status.msg = "正在上传...";
        openProgress(photos);
        // 履歴の雛形
        var log = alHistory.getTemplate($scope.land.latLng);
        // 送信処理
        sfdcUpload(log, photos);
      },
      function() {
        // CANCEL
        initUploadStatus();
      }
    );
  }*/
  $scope.upload = function() {
    $log.info('upload');
    var photos = alPhotos.all();
    // 確認して
    alModal.openConfirm($scope, 'confirmSend.html').then(
      function() {
        // OK
        // モーダル表示
        $scope.status.msg = "正在上传...";
        openProgress(photos);
        // 位置情報取得
        resolvePosition().then(
          function() {
            // 履歴の雛形
            var log = alHistory.getTemplate($scope.land.latLng);
              // 送信処理
            sfdcUpload(log, photos);
          },
          function() {
            // 失敗
            // alModal.openWarning('无法取得位置信息。请检查GPS设置。');
            $log.error('getCurrentLocation failed.');
          }
        );
      },
      function() {
        // CANCEL
        initUploadStatus();
      }
    );
  }
  
  
  // ロングタップした。
  $scope.buttonOnLongPress = function() {
    if ($scope.editMode == true) return;   // 削除モード中に押されると反応してしまうので、
    $scope.showOption = true;
    $scope.status.showLog = true;
  }
 
  $scope.buttonOnTouchEnd = function() {
    if ($scope.editMode == true) return;   // 削除モード中に押されると反応してしまうので、
    $scope.upload();
  }
 
  $scope.cancel = function() {
    $location.path('/menu/land');
  }
 
  $scope.toggleEditMode = function() {
    $scope.editMode = !$scope.editMode;
  }
 
  // 送信中ステータス情報の初期化
  initUploadStatus();
 
}])
// 撮影画面
.controller('PhotosCtrl', ['$scope', '$log', '$q', 'alPhotos', 'alModal',
  function ($scope, $log, $q, alPhotos, alModal) {
  $scope.photos = alPhotos.all();
  $scope.objs = null;
  $scope.disableInput = false;    // INPUT ボタン無効状態 識別用
 
  // 画像の削除
  $scope.removePhoto = function(index) {
    $scope.photos.splice(index, 1);
    if ($scope.photos.length ==0) {
      $scope.toggleEditMode();
    }
  }
 
  $scope.send =function() {
    $location.path('/menu/upload');
  }
 
  // ファイル・タイプのチェック
  var isValidFileType = function(file) {
    if (!file || !(file instanceof File) || !file.type.match("image.*")) {
      return false;
    }
    return true;
  }
 
  // 画像ファイル かチェック して エラーなら
  var checkValidFileType = function(files) {
    if (!files) return false;
    if (files instanceof FileList) {
      // 複数ファイル
      for(var i =0; i < files.length; i++) {
        if (isValidFileType(files[i]) ==false) {
          alModal.openWarning((i+1).toString() +'番目のファイルの種類が違います。');
          return false;
        }
        if(!angular.isDefined(files[i].size) || files[i].size == 0) {
          alModal.openWarning((i+1).toString() +'番目のファイルが読み込めません。');
          return false;
        }
      }
    }
    else {
      // 単一ファイル
      if (isValidFileType(files) == false) {
        alModal.openWarning('ファイルの種類が違います。');
        return false;
      }
    }
    return true;
  }
 
  // 追加できるファイル数を取得
  var getAcceptableFileCount = function(files) {
    var defer = $q.defer();
    if (files.length + $scope.photos.length <= 1) {
      defer.resolve(files.length);
    }
    else if ($scope.photos.length < 1) {
      // 超過してるるが、まだ追加可能のとき
      // 確認ダイアログ OK、なら 追加可能なだけ追加する。
      alModal.openConfirm($scope, 'confirmMaxFileCount.html')
      .then(
        function() {
          defer.resolve(1 - $scope.photos.length);
        },
        function() {
          defer.reject();
        }
      );
    }
    else {
      alModal.openWarning('添付できるファイル数を超過しています。');
      defer.reject();
    }
    return defer.promise;
  }
 
 
  // INPUT ファイル へ反応して、ファイルデータの取り込み
  $scope.$watch("imageFiles", function(imageFiles) {
    if (!imageFiles) return;
    if (imageFiles.length == 0 ) return;
    $scope.disableInput = true;
 
    if (checkValidFileType(imageFiles) == true) {
      getAcceptableFileCount(imageFiles)
      .then(
        function(max) {
          // ファイル・タイプ OK
          alModal.openLoading();    // ローディング中ダイアログ
          var cnt = 0;
          for (var i = 0; i< imageFiles.length && i < max ; i++) {
 
            alPhotos.appendFile(imageFiles[i], $scope.config,
              function(file) {
                $log.info(file);
                cnt++;
                if (cnt == imageFiles.length || cnt == max) {
                  alModal.closeLoading();  // ローディング中ダイアログ を閉じる
                }
              }, function(err) {
                $log.error(err);
                cnt++;
                alModal.closeLoading();  // ローディング中ダイアログ を閉じる
              });
          }
        }
      );
    }
    $scope.disableInput = false;
  });
}])
//ok画面(リダイレクト用)
.controller('redirectCtrl', ['$scope', 
  function ($scope) {
  $scope.redirect = function() {
    sforce.one.navigateToSObject($scope.land.pid);
  }
 
}])
// 撮影画面
.controller('PhotosOldCtrl', ['$scope', '$log', '$location', 'alPhotos', 'alModal', 
  function ($scope, $log, $location, alPhotos, alModal) {
  $scope.editMode = false;
  $scope.photos = alPhotos.all();
  $scope.objs = null;
  $scope.nameFilter = null;
  
  // 絞込み
  $scope.searchFilter = function (obj) {
      var re = new RegExp($scope.nameFilter, 'i');
      return !$scope.nameFilter || re.test(obj.Name);
  };
 
  $scope.doDetail = function (objId) {
 
  }
 
  // 画像の削除
  $scope.removePhoto = function(index) {
    $scope.photos.splice(index, 1);
    if ($scope.photos.length ==0) {
      $scope.editMode = false;
    }
  }
 
  $scope.send =function() {
    $location.path('/menu/upload');
  }
 
  // INPUT ファイル へ反応して、ファイルデータの取り込み
  $scope.$watch("imageFile", function (imageFile) {
    $scope.barValue = 0;
    $scope.Max =0;
    if (!imageFile || !imageFile.type.match("image.*")) {
        return;
    }
    alModal.openLoading();
    alPhotos.appendFile(imageFile, $scope.config,
      function(file) {
        $log.info(file);
        alModal.closeLoading();
      }, function(err) {
        $log.error(err);
        alModal.closeLoading();
      });
  });
}])
;
</script>
</apex:component>