123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635 |
- 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;
- };
|