[TIC-Web] Apply generic resource pooling
[archive/20170607/tools/tic.git] / public / src / js / page / image.js
1 define([
2     'jquery',
3     'lodash',
4     'js/util',
5     'js/logger',
6     '../model/ImageModel',
7     '../model/JobPagingModel',
8     '../widget/JobPaging',
9     '../widget/ImageEmptyItem',
10     '../widget/ImageItem',
11 ], function (
12     $,
13     _,
14     Util,
15     Logger,
16     ImageModel,
17     JobPagingModel,
18     JobPaging,
19     ImageEmptyItem,
20     ImageItem
21 ) {
22     'use strict';
23
24     var logger = Logger('image.js');
25
26     // config
27     var AppConfig;
28
29     // the list for the image
30     var ModelImageList = [];
31
32     // the paging model for the table of job
33     var ModelImagePaging;
34
35     function _update(pageNum) {
36         ModelImageList = [];
37         ModelImagePaging;
38
39         function _updateView(arrImages) {
40             return new Promise(function (resolve, reject) {
41                 logger.info('_updateView');
42                 var targetBody = $('#tic-image-list');
43                 targetBody.empty();
44
45                 if (arrImages.length <= 0) {
46                     targetBody.append(new ImageEmptyItem().getTemplate());
47                 } else {
48                     // when not empty
49                     _.forEach(arrImages, function (value, index) {
50                         targetBody.append(new ImageItem(value, index).getTemplate());
51                     });
52
53                     // bind events
54                     $('#tic-image-list a.btnbiglog').on('click', function (e) {
55                         e.preventDefault();
56                         Util.showBigLogView(this);
57                     });
58                 }
59             });
60         }
61
62         function _updateDataModel(result) {
63             return new Promise(function (resolve, reject) {
64                 logger.info('_updateDataModel');
65
66                 // model
67                 _.forEach(result, function (imageItem) {
68                     var item = new ImageModel(imageItem);
69                     ModelImageList.push(item);
70                 });
71
72                 resolve(ModelImageList);
73             });
74         }
75
76         function _getAllListItem() {
77             logger.info('_getAllListItem');
78             var msgData = {
79                 pageNum: pageNum
80             };
81             return Util.POST(AppConfig.EVENT.IMAGE.IMAGE_GET_ALL_LISTITEM, msgData);
82         }
83
84         function _updatePagingView(dataObj) {
85             return new Promise(function (resolve, reject) {
86                 logger.info('_updatePagingView');
87
88                 var targetPaging = $('#tic-image-list-pagination');
89                 targetPaging.empty();
90
91                 // create the element
92                 targetPaging.append(new JobPaging(dataObj).getTemplate());
93
94                 // bind a events
95                 $('#tic-image-section .pagination > li > a').on('click', function(e) {
96                     var pagenum;
97
98                     e.preventDefault();
99
100                     pagenum = $(this).data('pagenum');
101
102                     if (pagenum !== 0) {
103                         gotoPageNum(pagenum);
104                     }
105                     return false;
106                 });
107
108                 resolve();
109             });
110         }
111
112         function _updatePagingModel(result) {
113             return new Promise(function (resolve, reject) {
114                 logger.info('_updatePagingModel');
115
116                 var totalCount, pagingCount;
117
118                 // initialize
119                 totalCount = 0;
120                 $('#tic-image-list-pagination').empty();
121
122                 // set totalCount
123                 totalCount = Number(result[0].total_count);
124
125                 if (_.isEmpty(ModelImagePaging)) {
126                     ModelImagePaging = new JobPagingModel({
127                         totalCount: totalCount,
128                         currentPageNum: pageNum
129                     })
130                 }
131
132                 resolve(ModelImagePaging);
133             });
134         }
135
136         function _getTotalCount() {
137             logger.info('_getTotalCount');
138             return Util.POST(AppConfig.EVENT.IMAGE.IMAGE_GET_ALL_COUNT);
139         }
140
141         return _getTotalCount()
142             .then(_updatePagingModel)
143             .then(_updatePagingView)
144             .then(_getAllListItem)
145             .then(_updateDataModel)
146             .then(_updateView);
147     }
148
149     function gotoPageNum(pageNum) {
150         logger.info('gotoPageNum : ' + pageNum);
151         _update(pageNum);
152     }
153
154     function updateList(selectedPageNum) {
155         logger.info('updateList');
156
157         // when first,
158         // the default value of pageNum is 1.
159         var pageNum = selectedPageNum || 1;
160         gotoPageNum(pageNum);
161     }
162
163     function init() {
164         logger.info('init');
165
166         Util.getAppConfig().then(function (data) {
167             AppConfig = data;
168             updateList();
169         });
170     }
171
172     init();
173
174     return {
175         /**
176          * Update list in image page
177          * @method updateList
178          */
179         updateList: updateList
180     }
181
182 });