$(function () { 'use strict'; var searchView = window.$SearchView = { visibleObservable: null, isNotUse: ko.observable(true), height: ko.observable(), position: ko.observable(), popupOptions: { width: '315px', height: this.height, position: this.position, visible: this.visibleObservable, closeOnOutsideClick: false, shading: false, animation: { show: { type: "fade", duration: 350, from: 0, to: 1 }, hide: { type: "fade", duration: 200, from: 1, to: 0 } }, }, hide: function () { if (this.visibleObservable() == true) { this.visibleObservable(false); } }, setPopupVisibleObservable: function (visibleObservable) { /* if (this.visibleObservable) { this.visibleObservable(false); }*/ if (visibleObservable === null || visibleObservable() === null) { $SearchView.isNotUse(true); } else { visibleObservable(false); $SearchView.isNotUse(false); } this.visibleObservable = visibleObservable; }, toggleSearchView: function () { if (this.visibleObservable === null) return; this.visibleObservable(!this.visibleObservable()); //var visible = visibles[currentSideMenuId]; //visible( !visible() ); }, createDefaultArray: function (idName, defaultValue) { var obj = { Name: $G('selectAll') }; obj[idName] = defaultValue || 0; return [obj]; }, filterObservableArrayForSelectBox: function (idFieldName, array) { return _.filter(array, function (x) { return x[idFieldName]() !== 0; }); }, createObservableArrayOfSelectBox: function (idName, array, defaultValue) { return ko.observableArray(this.createArrayOfSelectBox(idName, array, defaultValue)); }, createObservableCodeNamesOfSelectBoxForSearch: function (codeNames) { var array = [{ Id: 0, Name: $G('selectAll') }]; return ko.observableArray(array.concat(codeNames)); }, createArrayOfSelectBox: function (idName, array, defaultValue) { var defaultArray = this.createDefaultArray(idName, defaultValue); if (_.isEmpty(array)) { return defaultArray; } return defaultArray.concat(array); }, recalculateHeight: function () { var height = $('.header_wrap').height() + $('#toolbar').height() + 2; this.height($(window).height() - height); }, getSearchItems: function (searchItemOptions, deferredReadyData) { if (_.isUndefined(searchItemOptions)) { return null; } var searchItems = {}; $.each(searchItemOptions, function (i, item) { var sItem = searchItems[item.id] = { //check: ko.observable(false), check: item['isChecked'] ? ko.observable(true) : ko.observable(false), value: item.value || (item.type === 'dateRange' ? [ko.observable(), ko.observable()] : ko.observable()), ignoreValue: item.ignoreValue, handleChangedValue: item.handleChangedValue, type: item.type, withCheckId: item.withCheckId, operator: item.operator, entityForCheckingUse: item.entityForCheckingUse, dataSource: undefined, filterFormatString: item.filterFormatString || undefined, isOnlyDate: item.isOnlyDate || false }; if (_.has(item, 'dataSource')) { if (item.type === 'checkNull') { var array = []; item.dataSource().forEach(function (text, i) { array.push({ Id: i, Name: text }); }); sItem.dataSource = ko.observableArray(array); } else { sItem.dataSource = item.dataSource; } } }); $.each(searchItems, function (name, item) { if (_.isUndefined(item.withCheckId) === false) { searchItems[item.withCheckId].check.subscribe(function (value) { item.check(value); }); } }); if (_.isUndefined(deferredReadyData) === false) { deferredReadyData.done(function () { $.each(searchItemOptions, function (i, item) { if (_.isUndefined(item.defaultValue) === false) { var value = searchItems[item.id].value; value(item.defaultValue); } }); }); } return searchItems; }, getFilter: function (searchItems) { var filter = []; var isValid = BWA.DataUtil.isValidValue; $.each(searchItems, function (name, item) { if (item.check() !== true) return true; switch (item.type) { case 'dateRange': { var tempDate = item.value[0](); // hcLee 2016 02 23 if (item.isOnlyDate === true) { tempDate = moment(tempDate).startOf('day').toDate(); } if (isValid(tempDate)) { filter.push('and'); filter.push([name, '>=', tempDate]); } tempDate = item.value[1](); if (item.isOnlyDate === true) { tempDate = moment(tempDate).endOf('day').toDate(); } if (isValid(tempDate)) { filter.push('and'); filter.push([name, '<=', tempDate]); } } break; case 'checkNull': { if (item.value() == 1) { filter.push('and'); filter.push([name, '<>', null]); } else if (item.value() == 2) { filter.push('and'); filter.push([name, '=', null]); } } break; case 'formatting': { if (_.isUndefined(item.value()) === false) { filter.push('and'); filter.push([item.filterFormatString.formati(item.value())]); //filter.push([item.filterFormatString.formati(item.value()), 'contains', item.value()]); //filter.push([name, 'contains', item.value()]); } } break; default: { var v = item.value(); if (_.isUndefined(v) === false) { if (item.ignoreValue !== v) { switch (typeof v) { case 'string': { if (v.length > 0) { filter.push('and'); filter.push([name, 'contains', v]); } break; } default: { filter.push('and'); filter.push([name, item.operator || '=', v]); break; } } } else if (_.isString(item.entityForCheckingUse)) { filter.push('and'); filter.push([item.entityForCheckingUse + '/IsUse', '=', true]); } } } break; } }); return filter; }, setupInView: function (viewModel, options, popupSearchViewVisible, defaultFilter, handleSearchFilter, handleInitializeUpdate) { this.recalculateHeight(); this.position({ my: 'right top', at: 'right bottom', of: $('#toolbar') }); popupSearchViewVisible(false); var initialized = false; viewModel.popupSearchViewOptions = { width: '300px', height: this.height, position: this.position, visible: popupSearchViewVisible, closeOnOutsideClick: false, shading: false, animation: { //show: { type: "slide", duration: 150, from: { top: -300, opacity: 0.8 }, to: { top: 300, opacity: 1 } }, //hide: { type: "slide", duration: 100, from: { left: window.innerWidth - 260, width: '260px', opacity: 1 }, to: { left: window.innerWidth - 1, width: '0px', opacity: 0.8 } } show: { type: "fade", duration: 350, from: 0, to: 1 }, hide: { type: "fade", duration: 200, from: 1, to: 0 } //show: { type: "slide", duration: 150, from: { left: screenWidth() - 260, width: '260px', opacity: 0.0 }, to: { left: screenWidth() - 260, width: '260px', opacity: 1 } }, //hide: { type: "slide", duration: 100, from: { left: screenWidth() - 260, width: '260px', opacity: 1 }, to: { left: screenWidth() - 260, width: '260px', opacity: 0 } } //show: { type: "slide", duration: 150, from: { left: window.innerWidth, width: '0px', opacity: 0 }, to: { left: window.innerWidth - 270, width: '260px', opacity: 1 } }, //hide: { type: "slide", duration: 100, from: { left: window.innerWidth - 260, width: '260px', opacity: 1 }, to: { left: window.innerWidth, width: '0px', opacity: 1 } } //show: { type: "slide", duration: 150, from: { left: window.innerWidth + 260 , opacity: 1 }, to: { left: window.innerWidth , opacity: 1 } }, //hide: { type: "slide", duration: 100, from: { left: window.innerWidth , opacity: 1 }, to: { left: window.innerWidth + 260 , opacity: 1 } } }, showingAction: function () { }, shownAction: function () { if (initialized === false) { if (_.isFunction(handleInitializeUpdate)) { handleInitializeUpdate(); } initialized = true; } } }; var searchViewItems = this.getSearchItems(options.searchViewItems, options.promiseDataInSearchView); if (_.isNull(searchViewItems)) { throw 'need "searchViewItems"'; } viewModel.searchViewItems = searchViewItems; var self = this; viewModel.handleSearchInSearchView = function () { var filter = self.getFilter(searchViewItems); if (viewModel.name != "PatrolReport") { filter = _.isNull(defaultFilter) ? filter : defaultFilter().slice(0).concat(filter); } else { if (filter.length == 0) { var temp = ['SiteId', '=', BWA.UserInfo.SiteId()]; filter = temp; } else { var temp = ['SiteId', '=', BWA.UserInfo.SiteId()]; filter = filter.slice(1); var length = filter.length filter[length] = "and"; filter[length + 1] = temp; } } if (_.isFunction(viewModel.handleSearchInSearchView2)) viewModel.handleSearchInSearchView2(filter); else handleSearchFilter(filter, searchViewItems); //koFilterWidget().filter( filter ); }; } }; $(window).resize(function (event) { searchView.recalculateHeight(); }); });