065124d76e69acf2a39b2e9871b307a92c04effa.svn-base 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. String.prototype.formati = function () {
  2. var theString = this;
  3. for (var i = 0; i < arguments.length; i++) {
  4. var regExp = new RegExp('\\{' + i + '\\}', 'gm');
  5. theString = theString.replace(regExp, arguments[i]);
  6. }
  7. return theString;
  8. }
  9. BWA.DataUtil = window.BWA.DataUtil = {
  10. isValidValue: function (value) {
  11. return (typeof value !== 'undefined' && value !== null);
  12. },
  13. // cyim 2015.12.28 : 필수 입력 정보 체크
  14. isValidInputValue: function (value) {
  15. // 널값 및 지정자 제외
  16. if (value == null || value == 'undefined')
  17. return false;
  18. if (typeof (value) == 'string') {
  19. // 공백 제외 (앞뒤)
  20. var temp = value.replace(/^\s+|\s+$/g, "");
  21. if (temp.length == 0)
  22. return false;
  23. else if (temp.length != value.length)
  24. return false;
  25. else
  26. return true;
  27. }
  28. else if (typeof (value) == 'number') {
  29. // 숫자 입력의 경우 특별히 없을듯함
  30. return true;
  31. }
  32. else
  33. return false;
  34. },
  35. resetDataModel: function (dataModel) {
  36. _.each(dataModel, function (value, name) {
  37. if (name === 'toJS' || name === 'fromJS') return;
  38. value(undefined);
  39. });
  40. },
  41. getDateFromDateTime: function (momentDateTime) {
  42. return moment([
  43. momentDateTime.year(),
  44. momentDateTime.month(),
  45. momentDateTime.date()
  46. ]).toDate();
  47. },
  48. getDateTimeFromDateAndTime: function (date, time) {
  49. var dateMoment = moment(date),
  50. timeMoment = moment(time);
  51. return moment([
  52. dateMoment.year(),
  53. dateMoment.month(),
  54. dateMoment.date(),
  55. timeMoment.hour(),
  56. timeMoment.minute(),
  57. timeMoment.second()
  58. ]).toDate();
  59. },
  60. // 2015 05 15 hcLee
  61. getDateTimeZeroSecond: function (date, time) {
  62. var dateMoment = moment(date),
  63. timeMoment = moment(time);
  64. return moment([
  65. dateMoment.year(),
  66. dateMoment.month(),
  67. dateMoment.date(),
  68. timeMoment.hour(),
  69. timeMoment.minute(),
  70. 0
  71. ]).toDate();
  72. },
  73. getDateTimeZeroMin: function (date, time) {
  74. var dateMoment = moment(date),
  75. timeMoment = moment(time);
  76. return moment([
  77. dateMoment.year(),
  78. dateMoment.month(),
  79. dateMoment.date(),
  80. timeMoment.hour(),
  81. 0,
  82. 0
  83. ]).toDate();
  84. },
  85. getDateTimeZeroHour: function (date) {
  86. var dateMoment = moment(date);
  87. return moment([
  88. dateMoment.year(),
  89. dateMoment.month(),
  90. dateMoment.date(),
  91. 0,
  92. 0,
  93. 0
  94. ]).toDate();
  95. },
  96. getDateTimeMaxHour: function (date) {
  97. var dateMoment = moment(date);
  98. return moment([
  99. dateMoment.year(),
  100. dateMoment.month(),
  101. dateMoment.date(),
  102. 23,
  103. 59,
  104. 59,
  105. ]).toDate();
  106. },
  107. getDateTimeStartDay: function (date) {
  108. var dateMoment = moment(date);
  109. return moment([
  110. dateMoment.year(),
  111. dateMoment.month(),
  112. 1,
  113. 0,
  114. 0,
  115. 0
  116. ]).toDate();
  117. },
  118. getDateTimeEndDay: function (date) {
  119. var dateMoment = moment(date);
  120. return moment([
  121. dateMoment.year(),
  122. dateMoment.month() + 1,
  123. 0,
  124. 23,
  125. 59,
  126. 59
  127. ]).toDate();
  128. },
  129. setArrayDataSourceFilter: function (dataSource, field, value, koArray, defaultArray) {
  130. if (dataSource._items.length != 0) {
  131. if (dataSource._items[0].SiteId != null) {
  132. dataSource.filter([
  133. [field, value],
  134. "and",
  135. ["SiteId", BWA.UserInfo.SiteId()]]);
  136. } else {
  137. dataSource.filter(field, value);
  138. }
  139. } else {
  140. dataSource.filter(field, value);
  141. }
  142. var deferred = new $.Deferred();
  143. if ($IsValid(value) === false) {
  144. if ($IsValid(defaultArray)) {
  145. koArray(defaultArray.slice(0));
  146. }
  147. else {
  148. koArray([]);
  149. }
  150. deferred.resolve();
  151. }
  152. else {
  153. dataSource.load().done(function (result) {
  154. if ($IsValid(defaultArray)) {
  155. koArray(defaultArray.slice(0).concat(result));
  156. }
  157. else {
  158. koArray(result);
  159. }
  160. deferred.resolve();
  161. }).fail(function () {
  162. deferred.reject();
  163. });
  164. }
  165. return deferred.promise();
  166. },
  167. setArrayDataSourceFilterForChartSet: function (dataSource, field, value, koArray, defaultArray) {
  168. dataSource.filter(field, value);
  169. var deferred = new $.Deferred();
  170. if ($IsValid(value) === false) {
  171. if ($IsValid(defaultArray)) {
  172. koArray(defaultArray.slice(0));
  173. }
  174. else {
  175. koArray([]);
  176. }
  177. deferred.resolve();
  178. }
  179. else {
  180. dataSource.load().done(function (result) {
  181. if ($IsValid(defaultArray)) {
  182. koArray(defaultArray.slice(0).concat(result));
  183. }
  184. else {
  185. var name = ['외기온도', '외기습도'];
  186. for (var f = 1000; f < 1002; f++) {
  187. var bf = new BemsWebApplication.BemsFormulaBaseViewModel();
  188. bf.FacilityTypeId(value);
  189. bf.FormulaId(f);
  190. bf.Name(name[f - 1000]);
  191. result.push(bf);
  192. }
  193. var bf = new BemsWebApplication.BemsFormulaBaseViewModel();
  194. bf.FacilityTypeId(value);
  195. bf.FormulaId(0);
  196. bf.Name('없음');
  197. result.push(bf);
  198. koArray(result);
  199. }
  200. deferred.resolve();
  201. }).fail(function () {
  202. deferred.reject();
  203. });
  204. }
  205. return deferred.promise();
  206. },
  207. convertViewModelToJS: function (viewModel) {
  208. if (_.isNull(viewModel)) {
  209. return null;
  210. }
  211. return BWA.DataUtil.mapObject(viewModel, null, function (name, value) {
  212. return value();
  213. });
  214. },
  215. convertHybridViewModelToJS: function (viewModel) {
  216. return BWA.DataUtil.mapObject(viewModel, null, function (name, value) {
  217. if (_.isFunction(value)) {
  218. return value();
  219. }
  220. return value;
  221. });
  222. },
  223. copyViewModel: function (src, dest) {
  224. _.each(dest, function (koObservable, name) {
  225. if (name === 'toJS' || name === 'fromJS') return;
  226. var value = src[name];
  227. if (value) {
  228. if (_.isFunction(value)) {
  229. koObservable(value());
  230. }
  231. else {
  232. koObservable(value);
  233. }
  234. }
  235. });
  236. },
  237. mapObject: function (obj, funcForName, funcForValue) {
  238. var object = {};
  239. $.each(obj, function (name, value) {
  240. var n = funcForName ? funcForName(name, value) : name;
  241. var v = funcForValue ? funcForValue(name, value) : value;
  242. if (n !== null) {
  243. object[n] = v;
  244. }
  245. });
  246. return object;
  247. },
  248. popFromArray: function (array, predicate) {
  249. if (_.isArray(array) === false) {
  250. return null;
  251. }
  252. var found = null;
  253. var length = array.length;
  254. for (var i = 0; i < length; i++) {
  255. if (predicate(array[i], i)) {
  256. found = array[i];
  257. array.splice(i, 1);
  258. break;
  259. }
  260. }
  261. return found;
  262. },
  263. resetViewModel: function (viewModel) {
  264. $.each(viewModel, function (name, field) {
  265. switch (name) {
  266. case 'toJS': // no break
  267. case 'fromJS':
  268. break;
  269. case 'SiteId':
  270. field(BWA.UserInfo.SiteId());
  271. break;
  272. case 'IsUse':
  273. field(true);
  274. break;
  275. default:
  276. field(null);
  277. break;
  278. }
  279. });
  280. },
  281. loadFromDataSource: function (dataSource, koObservableArray, callback) {
  282. dataSource.load().done(function (dataArray) {
  283. koObservableArray(dataArray);
  284. if (_.isFunction(callback)) {
  285. callback();
  286. }
  287. });
  288. },
  289. createFilterIdMoreThanZero: function (siteId, idName) {
  290. return [
  291. ['SiteId', '=', siteId],
  292. 'and',
  293. [idName, '>', 0]
  294. ];
  295. },
  296. createDataSource: function (options, dbModelId) {
  297. var dataSourceOptions = {};
  298. if (options.dataSourceOptions !== undefined) {
  299. $.extend(dataSourceOptions, options.dataSourceOptions);
  300. if (options.dataSourceOptions.select !== undefined) {
  301. var select = dataSourceOptions.select;
  302. var expand = dataSourceOptions.expand;
  303. if ('extendOptions' in dataSourceOptions) {
  304. var extendOptions = dataSourceOptions.extendOptions;
  305. if (_.has(extendOptions, 'multipleItems')) {
  306. extendOptions.multipleItems.forEach(function (m) {
  307. select = select.concat(m.fields);
  308. select = select.concat(m.entries.map(function (entry) {
  309. return '{0}/{1}'.formati(entry, m.id);
  310. }));
  311. expand = m.entries.concat(expand);
  312. });
  313. }
  314. }
  315. if ('ref' in options) {
  316. var ref = options.ref;
  317. var refFunc = function (x) {
  318. return [ref, '/', x].join('');
  319. };
  320. dataSourceOptions.select = select.map(refFunc);
  321. dataSourceOptions.expand = expand.map(refFunc);
  322. if ('refOptions' in dataSourceOptions) {
  323. var refOptions = dataSourceOptions.refOptions;
  324. if ('select' in refOptions) {
  325. dataSourceOptions.select = refOptions.select.concat(dataSourceOptions.select);
  326. }
  327. if ('expand' in refOptions) {
  328. dataSourceOptions.expand = refOptions.expand.concat(dataSourceOptions.expand);
  329. }
  330. }
  331. }
  332. else {
  333. dataSourceOptions.select = select;
  334. dataSourceOptions.expand = expand;
  335. }
  336. dataSourceOptions.map = function (item) {
  337. if (typeof extendOptions === "object") {
  338. return BWA.db.createViewModelWithMultipleFK(item, extendOptions);
  339. }
  340. return BWA.db.createViewModel(item);
  341. };
  342. }
  343. }
  344. if (('store' in dataSourceOptions) === false) {
  345. dataSourceOptions.store = BemsWebApplication.db[dbModelId];
  346. }
  347. if (('map' in dataSourceOptions) === false) {
  348. dataSourceOptions.map = function (item) {
  349. return new BemsWebApplication[dbModelId + 'ViewModel'](item);
  350. };
  351. }
  352. var dataSource = new DevExpress.data.DataSource(dataSourceOptions);
  353. dataSource._paginate = false; // hcLee 2015 08 24
  354. return dataSource; //
  355. },
  356. constructEqualFilter: function (fieldName, value) {
  357. return [fieldName, '=', value];
  358. },
  359. andFilter: 'and',
  360. constructNotEqualFilter: function (fieldName, value) {
  361. return [fieldName, '<>', value];
  362. },
  363. pickFunctions: function (obj) {
  364. return _.pick(BWA.Factory, function (value) {
  365. return _.isFunction(value);
  366. });
  367. },
  368. pickKeys: function (dataModel, dbModelId) {
  369. var keys = BWA.db[dbModelId].key();
  370. var model = _.isFunction(dataModel.toJS) ? dataModel.toJS() : dataModel;
  371. return _.pick(model, keys);
  372. },
  373. koFindWhere: function (array, obj) {
  374. return _.find(array, function (item) {
  375. var keys = _.keys(obj);
  376. return _.every(keys, function (key) {
  377. return item[key]() === obj[key];
  378. });
  379. });
  380. },
  381. pushInArrayNoDup: function (array, keys, newObjectValue) {
  382. var koValue = $KoValue;
  383. var isFound = _.some(array, function (x) {
  384. return _.every(keys, function (k) {
  385. return koValue(x[k]) === koValue(newObjectValue[k]);
  386. });
  387. });
  388. if (isFound === false) {
  389. array.push(newObjectValue);
  390. return true;
  391. }
  392. return false;
  393. },
  394. getNumberStringWithComma: function (value) {
  395. if (typeof (value) === 'function') {
  396. value = value();
  397. }
  398. return String(value).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  399. },
  400. getLocationDataSourceDelegateForTreeView: function (buildingDataSource, floorDataSource, zoneDataSource) {
  401. return function (data, alterObj) {
  402. var eq = BWA.DataUtil.constructEqualFilter;
  403. var id = data.id;
  404. var depth = data.depth;
  405. var promise = null;
  406. switch (depth) {
  407. case 0: // hcLee 2017 02 15 추가
  408. buildingDataSource.filter([
  409. eq('SiteId', BWA.UserInfo.SiteId()),
  410. ]);
  411. promise = buildingDataSource.load();
  412. break;
  413. case 1:
  414. floorDataSource.filter([
  415. eq('SiteId', BWA.UserInfo.SiteId()),
  416. 'and',
  417. eq('BuildingId', data.data.BuildingId())
  418. ]);
  419. promise = floorDataSource.load();
  420. break;
  421. case 2:
  422. if (zoneDataSource) { // 2016 04 11
  423. zoneDataSource.filter([
  424. eq('SiteId', BWA.UserInfo.SiteId()),
  425. 'and',
  426. eq('BuildingId', data.data.BuildingId()),
  427. 'and',
  428. eq('FloorId', data.data.FloorId())
  429. ]);
  430. promise = zoneDataSource.load();
  431. }
  432. break;
  433. }
  434. return promise;
  435. };
  436. },
  437. };
  438. window.$NameInStore = function (items, valueId, value, displayId) {
  439. var name = null;
  440. displayId = displayId || 'Name';
  441. $.each(items, function (i, item) {
  442. if (item[valueId]() === value) {
  443. name = item[displayId];
  444. return false;
  445. }
  446. });
  447. return name;
  448. };
  449. window.$IsValid = function (value) {
  450. return (value !== undefined && value !== null);
  451. };
  452. window.$Name = function (items, value) {
  453. var name = null;
  454. $.each(items, function (i, item) {
  455. if (item['Id'] === value) {
  456. name = item['Name'];
  457. return false;
  458. }
  459. });
  460. return name;
  461. };
  462. window.$KoValue = function (koValue) {
  463. if (_.isUndefined(koValue)) {
  464. return undefined;
  465. }
  466. if (_.isFunction(koValue)) {
  467. return koValue();
  468. }
  469. return koValue;
  470. };
  471. // popup ko에 설정시, ko 값이 있으면 해당하는 값만 바꿔야 화면이 갱신된다.
  472. window.$KoSet = function (destValue, koValue) {
  473. if (_.isUndefined(destValue)) {
  474. destValue = ko.observable();
  475. }
  476. if (_.isUndefined(koValue)) {
  477. destValue(undefined);
  478. return destValue;
  479. }
  480. if (_.isFunction(koValue)) {
  481. destValue(koValue());
  482. }
  483. else {
  484. destValue(koValue);
  485. }
  486. return destValue;
  487. };
  488. window.$NumberFormat = function (value) {
  489. if (typeof (value) === 'function') {
  490. value = value();
  491. }
  492. return String(value).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  493. };
  494. window.$DataSource = function (options, dbModelId) {
  495. var dataSourceOptions = {};
  496. var ref = options.ref;
  497. if (options.dataSourceOptions !== undefined) {
  498. $.extend(dataSourceOptions, options.dataSourceOptions);
  499. if (options.dataSourceOptions.select !== undefined) {
  500. var extendOptions = dataSourceOptions.extendOptions;
  501. if (extendOptions !== undefined) {
  502. var select = dataSourceOptions.select;
  503. var expand = dataSourceOptions.expand;
  504. extendOptions.multipleItems.forEach(function (m) {
  505. select = select.concat(m.fields);
  506. select = select.concat(m.entries.map(function (entry) {
  507. return '{0}/{1}'.formati(entry, m.id);
  508. }));
  509. expand = m.entries.concat(expand);
  510. });
  511. if (ref) {
  512. var refFunc = function (x) {
  513. return [ref, '/', x].join('');
  514. };
  515. dataSourceOptions.select = select.map(refFunc);
  516. dataSourceOptions.expand = expand.map(refFunc);
  517. var refOptions = dataSourceOptions.refOptions;
  518. if (refOptions !== null && typeof refOptions !== 'undefined') {
  519. if (typeof refOptions.select !== 'undefined') {
  520. dataSourceOptions.select = refOptions.select.concat(dataSourceOptions.select);
  521. }
  522. if (typeof refOptions.expand !== 'undefined') {
  523. dataSourceOptions.expand = refOptions.expand.concat(dataSourceOptions.expand);
  524. }
  525. }
  526. }
  527. else {
  528. dataSourceOptions.select = select;
  529. dataSourceOptions.expand = expand;
  530. }
  531. }
  532. dataSourceOptions.map = function (item) {
  533. if (typeof extendOptions === "object") {
  534. return BemsWebApplication.db.createViewModelWithMultipleFK(item, extendOptions);
  535. }
  536. return BemsWebApplication.db.createViewModel(item);
  537. };
  538. }
  539. }
  540. if (dataSourceOptions.store === undefined) {
  541. dataSourceOptions.store = BemsWebApplication.db[dbModelId];
  542. }
  543. if (dataSourceOptions.map === undefined) {
  544. dataSourceOptions.map = function (item) {
  545. return new BemsWebApplication[dbModelId + 'ViewModel'](item);
  546. };
  547. }
  548. var dataSource = new DevExpress.data.DataSource(dataSourceOptions);
  549. return dataSource;
  550. };