6d24631aefa7add7de89613a373085b630a5f733.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. $(function () {
  2. 'use strict';
  3. var searchView = window.$SearchView = {
  4. visibleObservable: null,
  5. isNotUse: ko.observable(true),
  6. height: ko.observable(),
  7. position: ko.observable(),
  8. popupOptions: {
  9. width: '315px',
  10. height: this.height,
  11. position: this.position,
  12. visible: this.visibleObservable,
  13. closeOnOutsideClick: false,
  14. shading: false,
  15. animation: {
  16. show: { type: "fade", duration: 350, from: 0, to: 1 },
  17. hide: { type: "fade", duration: 200, from: 1, to: 0 }
  18. },
  19. },
  20. hide: function () {
  21. if (this.visibleObservable() == true) {
  22. this.visibleObservable(false);
  23. }
  24. },
  25. setPopupVisibleObservable: function (visibleObservable) {
  26. /* if (this.visibleObservable) {
  27. this.visibleObservable(false);
  28. }*/
  29. if (visibleObservable === null || visibleObservable() === null) {
  30. $SearchView.isNotUse(true);
  31. }
  32. else {
  33. visibleObservable(false);
  34. $SearchView.isNotUse(false);
  35. }
  36. this.visibleObservable = visibleObservable;
  37. },
  38. toggleSearchView: function () {
  39. if (this.visibleObservable === null) return;
  40. this.visibleObservable(!this.visibleObservable());
  41. //var visible = visibles[currentSideMenuId];
  42. //visible( !visible() );
  43. },
  44. createDefaultArray: function (idName, defaultValue) {
  45. var obj = { Name: $G('selectAll') };
  46. obj[idName] = defaultValue || 0;
  47. return [obj];
  48. },
  49. filterObservableArrayForSelectBox: function (idFieldName, array) {
  50. return _.filter(array, function (x) {
  51. return x[idFieldName]() !== 0;
  52. });
  53. },
  54. createObservableArrayOfSelectBox: function (idName, array, defaultValue) {
  55. return ko.observableArray(this.createArrayOfSelectBox(idName, array, defaultValue));
  56. },
  57. createObservableCodeNamesOfSelectBoxForSearch: function (codeNames) {
  58. var array = [{ Id: 0, Name: $G('selectAll') }];
  59. return ko.observableArray(array.concat(codeNames));
  60. },
  61. createArrayOfSelectBox: function (idName, array, defaultValue) {
  62. var defaultArray = this.createDefaultArray(idName, defaultValue);
  63. if (_.isEmpty(array)) {
  64. return defaultArray;
  65. }
  66. return defaultArray.concat(array);
  67. },
  68. recalculateHeight: function () {
  69. var height = $('.header_wrap').height() + $('#toolbar').height() + 2;
  70. this.height($(window).height() - height);
  71. },
  72. getSearchItems: function (searchItemOptions, deferredReadyData) {
  73. if (_.isUndefined(searchItemOptions)) {
  74. return null;
  75. }
  76. var searchItems = {};
  77. $.each(searchItemOptions, function (i, item) {
  78. var sItem = searchItems[item.id] = {
  79. //check: ko.observable(false),
  80. check: item['isChecked'] ? ko.observable(true) : ko.observable(false),
  81. value: item.value || (item.type === 'dateRange' ? [ko.observable(), ko.observable()] : ko.observable()),
  82. ignoreValue: item.ignoreValue,
  83. handleChangedValue: item.handleChangedValue,
  84. type: item.type,
  85. withCheckId: item.withCheckId,
  86. operator: item.operator,
  87. entityForCheckingUse: item.entityForCheckingUse,
  88. dataSource: undefined,
  89. filterFormatString: item.filterFormatString || undefined,
  90. isOnlyDate: item.isOnlyDate || false
  91. };
  92. if (_.has(item, 'dataSource')) {
  93. if (item.type === 'checkNull') {
  94. var array = [];
  95. item.dataSource().forEach(function (text, i) {
  96. array.push({ Id: i, Name: text });
  97. });
  98. sItem.dataSource = ko.observableArray(array);
  99. }
  100. else {
  101. sItem.dataSource = item.dataSource;
  102. }
  103. }
  104. });
  105. $.each(searchItems, function (name, item) {
  106. if (_.isUndefined(item.withCheckId) === false) {
  107. searchItems[item.withCheckId].check.subscribe(function (value) {
  108. item.check(value);
  109. });
  110. }
  111. });
  112. if (_.isUndefined(deferredReadyData) === false) {
  113. deferredReadyData.done(function () {
  114. $.each(searchItemOptions, function (i, item) {
  115. if (_.isUndefined(item.defaultValue) === false) {
  116. var value = searchItems[item.id].value;
  117. value(item.defaultValue);
  118. }
  119. });
  120. });
  121. }
  122. return searchItems;
  123. },
  124. getFilter: function (searchItems) {
  125. var filter = [];
  126. var isValid = BWA.DataUtil.isValidValue;
  127. $.each(searchItems, function (name, item) {
  128. if (item.check() !== true) return true;
  129. switch (item.type) {
  130. case 'dateRange':
  131. {
  132. var tempDate = item.value[0]();
  133. // hcLee 2016 02 23
  134. if (item.isOnlyDate === true) {
  135. tempDate = moment(tempDate).startOf('day').toDate();
  136. }
  137. if (isValid(tempDate)) {
  138. filter.push('and');
  139. filter.push([name, '>=', tempDate]);
  140. }
  141. tempDate = item.value[1]();
  142. if (item.isOnlyDate === true) {
  143. tempDate = moment(tempDate).endOf('day').toDate();
  144. }
  145. if (isValid(tempDate)) {
  146. filter.push('and');
  147. filter.push([name, '<=', tempDate]);
  148. }
  149. }
  150. break;
  151. case 'checkNull':
  152. {
  153. if (item.value() == 1) {
  154. filter.push('and');
  155. filter.push([name, '<>', null]);
  156. }
  157. else if (item.value() == 2) {
  158. filter.push('and');
  159. filter.push([name, '=', null]);
  160. }
  161. }
  162. break;
  163. case 'formatting':
  164. {
  165. if (_.isUndefined(item.value()) === false) {
  166. filter.push('and');
  167. filter.push([item.filterFormatString.formati(item.value())]);
  168. //filter.push([item.filterFormatString.formati(item.value()), 'contains', item.value()]);
  169. //filter.push([name, 'contains', item.value()]);
  170. }
  171. }
  172. break;
  173. default:
  174. {
  175. var v = item.value();
  176. if (_.isUndefined(v) === false) {
  177. if (item.ignoreValue !== v) {
  178. switch (typeof v) {
  179. case 'string':
  180. {
  181. if (v.length > 0) {
  182. filter.push('and');
  183. filter.push([name, 'contains', v]);
  184. }
  185. break;
  186. }
  187. default:
  188. {
  189. filter.push('and');
  190. filter.push([name, item.operator || '=', v]);
  191. break;
  192. }
  193. }
  194. }
  195. else if (_.isString(item.entityForCheckingUse)) {
  196. filter.push('and');
  197. filter.push([item.entityForCheckingUse + '/IsUse', '=', true]);
  198. }
  199. }
  200. }
  201. break;
  202. }
  203. });
  204. return filter;
  205. },
  206. setupInView: function (viewModel, options, popupSearchViewVisible, defaultFilter, handleSearchFilter, handleInitializeUpdate) {
  207. this.recalculateHeight();
  208. this.position({ my: 'right top', at: 'right bottom', of: $('#toolbar') });
  209. popupSearchViewVisible(false);
  210. var initialized = false;
  211. viewModel.popupSearchViewOptions = {
  212. width: '300px',
  213. height: this.height,
  214. position: this.position,
  215. visible: popupSearchViewVisible,
  216. closeOnOutsideClick: false,
  217. shading: false,
  218. animation: {
  219. //show: { type: "slide", duration: 150, from: { top: -300, opacity: 0.8 }, to: { top: 300, opacity: 1 } },
  220. //hide: { type: "slide", duration: 100, from: { left: window.innerWidth - 260, width: '260px', opacity: 1 }, to: { left: window.innerWidth - 1, width: '0px', opacity: 0.8 } }
  221. show: { type: "fade", duration: 350, from: 0, to: 1 },
  222. hide: { type: "fade", duration: 200, from: 1, to: 0 }
  223. //show: { type: "slide", duration: 150, from: { left: screenWidth() - 260, width: '260px', opacity: 0.0 }, to: { left: screenWidth() - 260, width: '260px', opacity: 1 } },
  224. //hide: { type: "slide", duration: 100, from: { left: screenWidth() - 260, width: '260px', opacity: 1 }, to: { left: screenWidth() - 260, width: '260px', opacity: 0 } }
  225. //show: { type: "slide", duration: 150, from: { left: window.innerWidth, width: '0px', opacity: 0 }, to: { left: window.innerWidth - 270, width: '260px', opacity: 1 } },
  226. //hide: { type: "slide", duration: 100, from: { left: window.innerWidth - 260, width: '260px', opacity: 1 }, to: { left: window.innerWidth, width: '0px', opacity: 1 } }
  227. //show: { type: "slide", duration: 150, from: { left: window.innerWidth + 260 , opacity: 1 }, to: { left: window.innerWidth , opacity: 1 } },
  228. //hide: { type: "slide", duration: 100, from: { left: window.innerWidth , opacity: 1 }, to: { left: window.innerWidth + 260 , opacity: 1 } }
  229. },
  230. showingAction: function () {
  231. },
  232. shownAction: function () {
  233. if (initialized === false) {
  234. if (_.isFunction(handleInitializeUpdate)) {
  235. handleInitializeUpdate();
  236. }
  237. initialized = true;
  238. }
  239. }
  240. };
  241. var searchViewItems = this.getSearchItems(options.searchViewItems, options.promiseDataInSearchView);
  242. if (_.isNull(searchViewItems)) {
  243. throw 'need "searchViewItems"';
  244. }
  245. viewModel.searchViewItems = searchViewItems;
  246. var self = this;
  247. viewModel.handleSearchInSearchView = function () {
  248. var filter = self.getFilter(searchViewItems);
  249. if (viewModel.name != "PatrolReport") {
  250. filter = _.isNull(defaultFilter) ? filter : defaultFilter().slice(0).concat(filter);
  251. } else {
  252. if (filter.length == 0) {
  253. var temp = ['SiteId', '=', BWA.UserInfo.SiteId()];
  254. filter = temp;
  255. }
  256. else {
  257. var temp = ['SiteId', '=', BWA.UserInfo.SiteId()];
  258. filter = filter.slice(1);
  259. var length = filter.length
  260. filter[length] = "and";
  261. filter[length + 1] = temp;
  262. }
  263. }
  264. if (_.isFunction(viewModel.handleSearchInSearchView2))
  265. viewModel.handleSearchInSearchView2(filter);
  266. else
  267. handleSearchFilter(filter, searchViewItems);
  268. //koFilterWidget().filter( filter );
  269. };
  270. }
  271. };
  272. $(window).resize(function (event) {
  273. searchView.recalculateHeight();
  274. });
  275. });