String.prototype.formati = function () { var theString = this; for (var i = 0; i < arguments.length; i++) { var regExp = new RegExp('\\{' + i + '\\}', 'gm'); theString = theString.replace(regExp, arguments[i]); } return theString; } BWA.DataUtil = window.BWA.DataUtil = { isValidValue: function (value) { return (typeof value !== 'undefined' && value !== null); }, // cyim 2015.12.28 : 필수 입력 정보 체크 isValidInputValue: function (value) { // 널값 및 지정자 제외 if (value == null || value == 'undefined') return false; if (typeof (value) == 'string') { // 공백 제외 (앞뒤) var temp = value.replace(/^\s+|\s+$/g, ""); if (temp.length == 0) return false; else if (temp.length != value.length) return false; else return true; } else if (typeof (value) == 'number') { // 숫자 입력의 경우 특별히 없을듯함 return true; } else return false; }, resetDataModel: function (dataModel) { _.each(dataModel, function (value, name) { if (name === 'toJS' || name === 'fromJS') return; value(undefined); }); }, getDateFromDateTime: function (momentDateTime) { return moment([ momentDateTime.year(), momentDateTime.month(), momentDateTime.date() ]).toDate(); }, getDateTimeFromDateAndTime: function (date, time) { var dateMoment = moment(date), timeMoment = moment(time); return moment([ dateMoment.year(), dateMoment.month(), dateMoment.date(), timeMoment.hour(), timeMoment.minute(), timeMoment.second() ]).toDate(); }, // 2015 05 15 hcLee getDateTimeZeroSecond: function (date, time) { var dateMoment = moment(date), timeMoment = moment(time); return moment([ dateMoment.year(), dateMoment.month(), dateMoment.date(), timeMoment.hour(), timeMoment.minute(), 0 ]).toDate(); }, getDateTimeZeroMin: function (date, time) { var dateMoment = moment(date), timeMoment = moment(time); return moment([ dateMoment.year(), dateMoment.month(), dateMoment.date(), timeMoment.hour(), 0, 0 ]).toDate(); }, getDateTimeZeroHour: function (date) { var dateMoment = moment(date); return moment([ dateMoment.year(), dateMoment.month(), dateMoment.date(), 0, 0, 0 ]).toDate(); }, getDateTimeMaxHour: function (date) { var dateMoment = moment(date); return moment([ dateMoment.year(), dateMoment.month(), dateMoment.date(), 23, 59, 59, ]).toDate(); }, getDateTimeStartDay: function (date) { var dateMoment = moment(date); return moment([ dateMoment.year(), dateMoment.month(), 1, 0, 0, 0 ]).toDate(); }, getDateTimeEndDay: function (date) { var dateMoment = moment(date); return moment([ dateMoment.year(), dateMoment.month() + 1, 0, 23, 59, 59 ]).toDate(); }, setArrayDataSourceFilter: function (dataSource, field, value, koArray, defaultArray) { if (dataSource._items.length != 0) { if (dataSource._items[0].SiteId != null) { dataSource.filter([ [field, value], "and", ["SiteId", BWA.UserInfo.SiteId()]]); } else { dataSource.filter(field, value); } } else { dataSource.filter(field, value); } var deferred = new $.Deferred(); if ($IsValid(value) === false) { if ($IsValid(defaultArray)) { koArray(defaultArray.slice(0)); } else { koArray([]); } deferred.resolve(); } else { dataSource.load().done(function (result) { if ($IsValid(defaultArray)) { koArray(defaultArray.slice(0).concat(result)); } else { koArray(result); } deferred.resolve(); }).fail(function () { deferred.reject(); }); } return deferred.promise(); }, setArrayDataSourceFilterForChartSet: function (dataSource, field, value, koArray, defaultArray) { dataSource.filter(field, value); var deferred = new $.Deferred(); if ($IsValid(value) === false) { if ($IsValid(defaultArray)) { koArray(defaultArray.slice(0)); } else { koArray([]); } deferred.resolve(); } else { dataSource.load().done(function (result) { if ($IsValid(defaultArray)) { koArray(defaultArray.slice(0).concat(result)); } else { var name = ['외기온도', '외기습도']; for (var f = 1000; f < 1002; f++) { var bf = new BemsWebApplication.BemsFormulaBaseViewModel(); bf.FacilityTypeId(value); bf.FormulaId(f); bf.Name(name[f - 1000]); result.push(bf); } var bf = new BemsWebApplication.BemsFormulaBaseViewModel(); bf.FacilityTypeId(value); bf.FormulaId(0); bf.Name('없음'); result.push(bf); koArray(result); } deferred.resolve(); }).fail(function () { deferred.reject(); }); } return deferred.promise(); }, convertViewModelToJS: function (viewModel) { if (_.isNull(viewModel)) { return null; } return BWA.DataUtil.mapObject(viewModel, null, function (name, value) { return value(); }); }, convertHybridViewModelToJS: function (viewModel) { return BWA.DataUtil.mapObject(viewModel, null, function (name, value) { if (_.isFunction(value)) { return value(); } return value; }); }, copyViewModel: function (src, dest) { _.each(dest, function (koObservable, name) { if (name === 'toJS' || name === 'fromJS') return; var value = src[name]; if (value) { if (_.isFunction(value)) { koObservable(value()); } else { koObservable(value); } } }); }, mapObject: function (obj, funcForName, funcForValue) { var object = {}; $.each(obj, function (name, value) { var n = funcForName ? funcForName(name, value) : name; var v = funcForValue ? funcForValue(name, value) : value; if (n !== null) { object[n] = v; } }); return object; }, popFromArray: function (array, predicate) { if (_.isArray(array) === false) { return null; } var found = null; var length = array.length; for (var i = 0; i < length; i++) { if (predicate(array[i], i)) { found = array[i]; array.splice(i, 1); break; } } return found; }, resetViewModel: function (viewModel) { $.each(viewModel, function (name, field) { switch (name) { case 'toJS': // no break case 'fromJS': break; case 'SiteId': field(BWA.UserInfo.SiteId()); break; case 'IsUse': field(true); break; default: field(null); break; } }); }, loadFromDataSource: function (dataSource, koObservableArray, callback) { dataSource.load().done(function (dataArray) { koObservableArray(dataArray); if (_.isFunction(callback)) { callback(); } }); }, createFilterIdMoreThanZero: function (siteId, idName) { return [ ['SiteId', '=', siteId], 'and', [idName, '>', 0] ]; }, createDataSource: function (options, dbModelId) { var dataSourceOptions = {}; if (options.dataSourceOptions !== undefined) { $.extend(dataSourceOptions, options.dataSourceOptions); if (options.dataSourceOptions.select !== undefined) { var select = dataSourceOptions.select; var expand = dataSourceOptions.expand; if ('extendOptions' in dataSourceOptions) { var extendOptions = dataSourceOptions.extendOptions; if (_.has(extendOptions, 'multipleItems')) { extendOptions.multipleItems.forEach(function (m) { select = select.concat(m.fields); select = select.concat(m.entries.map(function (entry) { return '{0}/{1}'.formati(entry, m.id); })); expand = m.entries.concat(expand); }); } } if ('ref' in options) { var ref = options.ref; var refFunc = function (x) { return [ref, '/', x].join(''); }; dataSourceOptions.select = select.map(refFunc); dataSourceOptions.expand = expand.map(refFunc); if ('refOptions' in dataSourceOptions) { var refOptions = dataSourceOptions.refOptions; if ('select' in refOptions) { dataSourceOptions.select = refOptions.select.concat(dataSourceOptions.select); } if ('expand' in refOptions) { dataSourceOptions.expand = refOptions.expand.concat(dataSourceOptions.expand); } } } else { dataSourceOptions.select = select; dataSourceOptions.expand = expand; } dataSourceOptions.map = function (item) { if (typeof extendOptions === "object") { return BWA.db.createViewModelWithMultipleFK(item, extendOptions); } return BWA.db.createViewModel(item); }; } } if (('store' in dataSourceOptions) === false) { dataSourceOptions.store = BemsWebApplication.db[dbModelId]; } if (('map' in dataSourceOptions) === false) { dataSourceOptions.map = function (item) { return new BemsWebApplication[dbModelId + 'ViewModel'](item); }; } var dataSource = new DevExpress.data.DataSource(dataSourceOptions); dataSource._paginate = false; // hcLee 2015 08 24 return dataSource; // }, constructEqualFilter: function (fieldName, value) { return [fieldName, '=', value]; }, andFilter: 'and', constructNotEqualFilter: function (fieldName, value) { return [fieldName, '<>', value]; }, pickFunctions: function (obj) { return _.pick(BWA.Factory, function (value) { return _.isFunction(value); }); }, pickKeys: function (dataModel, dbModelId) { var keys = BWA.db[dbModelId].key(); var model = _.isFunction(dataModel.toJS) ? dataModel.toJS() : dataModel; return _.pick(model, keys); }, koFindWhere: function (array, obj) { return _.find(array, function (item) { var keys = _.keys(obj); return _.every(keys, function (key) { return item[key]() === obj[key]; }); }); }, pushInArrayNoDup: function (array, keys, newObjectValue) { var koValue = $KoValue; var isFound = _.some(array, function (x) { return _.every(keys, function (k) { return koValue(x[k]) === koValue(newObjectValue[k]); }); }); if (isFound === false) { array.push(newObjectValue); return true; } return false; }, getNumberStringWithComma: function (value) { if (typeof (value) === 'function') { value = value(); } return String(value).replace(/\B(?=(\d{3})+(?!\d))/g, ','); }, getLocationDataSourceDelegateForTreeView: function (buildingDataSource, floorDataSource, zoneDataSource) { return function (data, alterObj) { var eq = BWA.DataUtil.constructEqualFilter; var id = data.id; var depth = data.depth; var promise = null; switch (depth) { case 0: // hcLee 2017 02 15 추가 buildingDataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), ]); promise = buildingDataSource.load(); break; case 1: floorDataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), 'and', eq('BuildingId', data.data.BuildingId()) ]); promise = floorDataSource.load(); break; case 2: if (zoneDataSource) { // 2016 04 11 zoneDataSource.filter([ eq('SiteId', BWA.UserInfo.SiteId()), 'and', eq('BuildingId', data.data.BuildingId()), 'and', eq('FloorId', data.data.FloorId()) ]); promise = zoneDataSource.load(); } break; } return promise; }; }, }; window.$NameInStore = function (items, valueId, value, displayId) { var name = null; displayId = displayId || 'Name'; $.each(items, function (i, item) { if (item[valueId]() === value) { name = item[displayId]; return false; } }); return name; }; window.$IsValid = function (value) { return (value !== undefined && value !== null); }; window.$Name = function (items, value) { var name = null; $.each(items, function (i, item) { if (item['Id'] === value) { name = item['Name']; return false; } }); return name; }; window.$KoValue = function (koValue) { if (_.isUndefined(koValue)) { return undefined; } if (_.isFunction(koValue)) { return koValue(); } return koValue; }; // popup ko에 설정시, ko 값이 있으면 해당하는 값만 바꿔야 화면이 갱신된다. window.$KoSet = function (destValue, koValue) { if (_.isUndefined(destValue)) { destValue = ko.observable(); } if (_.isUndefined(koValue)) { destValue(undefined); return destValue; } if (_.isFunction(koValue)) { destValue(koValue()); } else { destValue(koValue); } return destValue; }; window.$NumberFormat = function (value) { if (typeof (value) === 'function') { value = value(); } return String(value).replace(/\B(?=(\d{3})+(?!\d))/g, ','); }; window.$DataSource = function (options, dbModelId) { var dataSourceOptions = {}; var ref = options.ref; if (options.dataSourceOptions !== undefined) { $.extend(dataSourceOptions, options.dataSourceOptions); if (options.dataSourceOptions.select !== undefined) { var extendOptions = dataSourceOptions.extendOptions; if (extendOptions !== undefined) { var select = dataSourceOptions.select; var expand = dataSourceOptions.expand; extendOptions.multipleItems.forEach(function (m) { select = select.concat(m.fields); select = select.concat(m.entries.map(function (entry) { return '{0}/{1}'.formati(entry, m.id); })); expand = m.entries.concat(expand); }); if (ref) { var refFunc = function (x) { return [ref, '/', x].join(''); }; dataSourceOptions.select = select.map(refFunc); dataSourceOptions.expand = expand.map(refFunc); var refOptions = dataSourceOptions.refOptions; if (refOptions !== null && typeof refOptions !== 'undefined') { if (typeof refOptions.select !== 'undefined') { dataSourceOptions.select = refOptions.select.concat(dataSourceOptions.select); } if (typeof refOptions.expand !== 'undefined') { dataSourceOptions.expand = refOptions.expand.concat(dataSourceOptions.expand); } } } else { dataSourceOptions.select = select; dataSourceOptions.expand = expand; } } dataSourceOptions.map = function (item) { if (typeof extendOptions === "object") { return BemsWebApplication.db.createViewModelWithMultipleFK(item, extendOptions); } return BemsWebApplication.db.createViewModel(item); }; } } if (dataSourceOptions.store === undefined) { dataSourceOptions.store = BemsWebApplication.db[dbModelId]; } if (dataSourceOptions.map === undefined) { dataSourceOptions.map = function (item) { return new BemsWebApplication[dbModelId + 'ViewModel'](item); }; } var dataSource = new DevExpress.data.DataSource(dataSourceOptions); return dataSource; };