frame.app.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. app.controller('AppCtrl', [ '$rootScope', '$scope', '$http', '$localStorage', '$location', '$modal', '$ocLazyLoad', '$q', 'toaster', function($rootScope, $scope, $http, $localStorage, $location, $modal, $ocLazyLoad, $q, toaster) {
  2. $rootScope.frame = {
  3. CompanyName : '',
  4. FrameVersion : '1.0.0',
  5. WaitWindow : false,
  6. // for chart colors
  7. color : {
  8. primary : '#7266ba',
  9. info : '#23b7e5',
  10. success : '#27c24c',
  11. warning : '#fad733',
  12. danger : '#f05050',
  13. light : '#e8eff0',
  14. dark : '#3a3f51',
  15. black : '#1c2b36'
  16. },
  17. colors : [ '#23b7e5', '#27c24c', '#7266ba', '#FFB6C1', '#87CEFA', '#DB7093', '#7FFF00', '#9932CC', '#6A5ACD' ],
  18. texts : [ 'text-info', 'text-success', 'text-primary', 'text-gold', 'text-lime', 'text-aliceblue', 'text-indianred', 'text-palevioletred', 'text-darkslategray' ],
  19. settings : {
  20. themeID : 1,
  21. navbarHeaderColor : 'bg-black',
  22. navbarCollapseColor : 'bg-white-only',
  23. asideColor : 'bg-black',
  24. headerFixed : true,
  25. asideFixed : true,
  26. asideFolded : false,
  27. asideDock : false,
  28. container : false
  29. }
  30. }
  31. $.ajax({
  32. url : 'frame/optr/init.htm',
  33. async : false,
  34. type : "POST",
  35. dataType : "json",
  36. success : function(data) {
  37. if (data.success == false) {
  38. window.location = "logout.html";
  39. return;
  40. } else
  41. angular.extend($rootScope.frame, data.map);
  42. // param
  43. $rootScope.frame.FrameParam = {};
  44. angular.forEach(data.map.FrameParamArray, function(param, key) {
  45. $rootScope.frame.FrameParam[param.param_name] = param.param_value;
  46. });
  47. document.title = $rootScope.frame.FrameParam.FrameTitle;
  48. // config
  49. $rootScope.frame.configMap = {}, $rootScope.frame.configArray = {};
  50. angular.forEach(data.map.FrameConfigArray, function(config, key) {
  51. $rootScope.frame.configMap[config.config_name + "_" + config.config_value] = config;
  52. if ($rootScope.frame.configArray[config.config_name] == undefined)
  53. $rootScope.frame.configArray[config.config_name] = [];
  54. $rootScope.frame.configArray[config.config_name].push(config);
  55. });
  56. // role
  57. $rootScope.frame.FrameOptr.roleMap = {};
  58. angular.forEach(data.map.FrameOptr.resMap, function(role, key) {
  59. $rootScope.frame.FrameOptr.roleMap['Role' + role] = role;
  60. });
  61. }
  62. });
  63. $rootScope.getConfigText = function(config_name, config_value) {
  64. var config = $rootScope.frame.configMap[config_name + "_" + config_value];
  65. if (config) {
  66. return config.config_text;
  67. }
  68. return config_value;
  69. };
  70. // 获取配置
  71. $rootScope.getConfigArray = function(config_name) {
  72. return $rootScope.frame.configArray[config_name];
  73. };
  74. // 鉴权
  75. $rootScope.authOptrRole = function(role_id) {
  76. return !!$rootScope.frame.FrameOptr.roleMap['Role' + role_id];
  77. };
  78. // 获取图标css
  79. $rootScope.getIconCss = function(css) {
  80. css = $.trim(css);
  81. if (css != undefined && css.length > 0) {
  82. if (css.indexOf('icon') == 0)
  83. return 'glyphicon icon ' + css + ' ';
  84. if (css.indexOf('fa') == 0)
  85. return 'glyphicon fa ' + css + ' ';
  86. if (css.indexOf('glyphicon') == 0)
  87. return 'glyphicon ' + css + ' ';
  88. return 'glyphicon ' + css + ' ';
  89. } else
  90. return 'glyphicon glyphicon-cog text-info-lter ';
  91. };
  92. // 计算文件大小:tostring
  93. $rootScope.getFileSize = function(size) {
  94. if (size == null || size == undefined || isNaN(size))
  95. return;
  96. if (size < 1024)
  97. return size + ' Byte'
  98. if (size < 1024 * 1024)
  99. return Math.round(size / 1024 * 100) / 100 + ' KB';
  100. return Math.round(size / (1024 * 1024) * 100) / 100 + ' MB';
  101. }
  102. // 对话框
  103. $rootScope.dialog = function(params) {
  104. $modal.open({
  105. templateUrl : 'frame/dialog/confirm.html',
  106. controller : 'FrameConfirmCtrl',
  107. backdrop : 'static',
  108. size : 's4',
  109. keyboard : false,
  110. windowClass : 'dialog',
  111. // scope : true,
  112. resolve : {
  113. params : function() {
  114. return angular.copy(params);
  115. }
  116. }
  117. });
  118. }
  119. // 对话框
  120. $rootScope.msg = function(params) {
  121. $modal.open({
  122. templateUrl : 'frame/dialog/alert.html',
  123. controller : 'FrameAlertCtrl',
  124. backdrop : 'static',
  125. size : 's4',
  126. keyboard : false,
  127. windowClass : 'dialog',
  128. // scope : scope,
  129. resolve : {
  130. params : function() {
  131. return angular.copy(params);
  132. }
  133. }
  134. });
  135. }
  136. // 等待
  137. $rootScope.wait = function(params) {
  138. $modal.open({
  139. templateUrl : 'frame/dialog/wait.html',
  140. controller : 'FrameWaitCtrl',
  141. backdrop : 'static',
  142. size : 's4',
  143. keyboard : false,
  144. windowClass : 'dialog',
  145. // scope : true,
  146. resolve : {
  147. params : function() {
  148. return angular.copy(params);
  149. }
  150. }
  151. });
  152. }
  153. // 显示等待层
  154. $rootScope.showWaitWindow = function(wait) {
  155. // 已经打开了等待框
  156. if ($rootScope.frame.WaitWindow == true)
  157. return;
  158. $rootScope.frame.WaitWindow = true;
  159. $rootScope.wait({
  160. seconds : wait
  161. });
  162. }
  163. // 通用弹出层
  164. $rootScope.showModal = function(params) {
  165. var js = params.js || '';
  166. if (js.length != 0) {
  167. $ocLazyLoad.load([ js ]).then(function() {
  168. $modal.open(params)
  169. });
  170. } else
  171. $modal.open(params);
  172. }
  173. // 通用上传文件框
  174. $rootScope.showUpload = function(url, params, fn) {
  175. $ocLazyLoad.load([ 'angularFileUpload' ]).then(function() {
  176. $modal.open({
  177. templateUrl : 'frame/dialog/upload.html',
  178. controller : 'FrameUploadCtrl',
  179. backdrop : 'static',
  180. size : 'lg',
  181. keyboard : false,
  182. windowClass : 'dialog',
  183. // scope : true,
  184. resolve : {
  185. url : function() {
  186. return angular.copy(url);
  187. },
  188. params : function() {
  189. return angular.copy(params);
  190. },
  191. fn : function() {
  192. return angular.copy(fn);
  193. },
  194. }
  195. });
  196. });
  197. }
  198. // 通用状态颜色
  199. $rootScope.getStatusColor = function(status) {
  200. if (status == 'Active')
  201. return {
  202. color : 'green'
  203. };
  204. return {
  205. color : '#ADADAD'
  206. }
  207. }
  208. // 通用编辑窗口
  209. $rootScope.editEntity = function(scope, url, contorl, entity, size, js, keyboard) {
  210. js = js || (url.substring(0, url.length - 4) + 'js');
  211. $rootScope.showModal({
  212. templateUrl : url,
  213. controller : contorl,
  214. keyboard : keyboard == undefined ? true : keyboard,
  215. js : js,
  216. backdrop : 'static',
  217. size : size,
  218. scope : scope,
  219. resolve : {
  220. entity : function() {
  221. return angular.copy(entity);
  222. }
  223. }
  224. });
  225. }
  226. $rootScope.jAjax = function(params) {
  227. var aParam = angular.extend({
  228. async : false,
  229. type : "POST",
  230. dataType : "json"
  231. }, params)
  232. if (params.success) {
  233. aParam.success = function(data) {
  234. if (data.success == false) {
  235. if (data.login == false) {
  236. window.location = "logout.html";
  237. return;
  238. }
  239. toaster.error(data.errorMsg);
  240. return;
  241. }
  242. params.success.call(this, data);
  243. }
  244. }
  245. $.ajax(aParam);
  246. }
  247. $rootScope.shortDateString = function(date) {
  248. if (date == undefined)
  249. return "";
  250. if (angular.isDate(date))
  251. date = date.toJSON();
  252. if (date.length > 10)
  253. return date.substr(0, 10);
  254. return date;
  255. }
  256. $rootScope.shortTimeString = function(date) {
  257. if (date == undefined)
  258. return "";
  259. if (angular.isDate(date))
  260. date = date.toJSON();
  261. if (date.length > 10)
  262. return date.substr(11);
  263. return date;
  264. }
  265. $rootScope.parseShortString = function(date) {
  266. if(date == undefined)
  267. date = new Date();
  268. var str = $rootScope.shortDateString(date);
  269. return new Date(Date.parse(str.replace(/-/g, "/")));
  270. }
  271. $rootScope.parseLongString = function(date) {
  272. if (angular.isDate(date))
  273. return date;
  274. if (date == undefined || date.length != 19)
  275. return undefined;
  276. return new Date(Date.parse(date.replace(/-/g, "/")));
  277. }
  278. $rootScope.shortDateOptions = {
  279. formatYear : 'yyyy',
  280. startingDay : 1,
  281. class : 'datepicker',
  282. };
  283. $rootScope.openDatepicker = function(scope, $event, value) {
  284. $event.preventDefault();
  285. $event.stopPropagation();
  286. scope[value] = true;
  287. };
  288. $rootScope.openNewWindow = function(url) {
  289. window.open(url);
  290. };
  291. $rootScope.removeFirstElement = function(array) {
  292. var ar = new Array();
  293. if (array == undefined)
  294. return;
  295. for (var i = 1; i < array.length; i++)
  296. ar.push(array[i]);
  297. return ar;
  298. };
  299. $rootScope.optrSingleRecord = function(url, key, fn, dialog_msg, toaster_msg) {
  300. var ajax = function() {
  301. $http({
  302. url : url,
  303. method : 'post',
  304. data : {
  305. key : key
  306. }
  307. }).success(function(data) {
  308. if (fn)
  309. fn.call(this, data);
  310. if (toaster_msg == false)
  311. return;
  312. toaster.success('', toaster_msg || '操作成功');
  313. });
  314. };
  315. if (dialog_msg == undefined || dialog_msg.length == 0) {
  316. ajax();
  317. } else {
  318. // 提交
  319. $rootScope.dialog({
  320. success : ajax,
  321. msg : dialog_msg
  322. });
  323. }
  324. }
  325. $rootScope.optrRecord = function(url, entity, fn, dialog_msg, toaster_msg) {
  326. var ajax = function() {
  327. $http({
  328. url : url,
  329. method : 'post',
  330. data : entity
  331. }).success(function(data) {
  332. if (fn)
  333. fn.call(this, data);
  334. if (toaster_msg == false)
  335. return;
  336. toaster.success('', toaster_msg || '操作成功');
  337. });
  338. };
  339. if (dialog_msg == undefined || dialog_msg.length == 0) {
  340. ajax();
  341. } else {
  342. // 提交
  343. $rootScope.dialog({
  344. success : ajax,
  345. msg : dialog_msg
  346. });
  347. }
  348. }
  349. $rootScope.totalValues = function(array, field) {
  350. var value = 0;
  351. angular.forEach(array, function(dr) {
  352. if (dr[field])
  353. value += dr[field];
  354. })
  355. return value;
  356. }
  357. $scope.logout = function() {
  358. $rootScope.dialog({
  359. success : function() {
  360. $http({
  361. url : 'frame/optr/logout.htm',
  362. }).success(function() {
  363. window.location = "./login/login.jsp"
  364. });
  365. },
  366. title : '退出系统提示',
  367. msg : '请问您确定要退出系统吗?'
  368. });
  369. }
  370. // save settings to local storage
  371. if (angular.isDefined($localStorage.settings)) {
  372. $scope.frame.settings = $localStorage.settings;
  373. } else {
  374. $localStorage.settings = $scope.frame.settings;
  375. }
  376. $scope.$watch('frame.settings', function() {
  377. if ($scope.frame.settings.asideDock && $scope.frame.settings.asideFixed) {
  378. $scope.frame.settings.headerFixed = true;
  379. }
  380. $localStorage.settings = $scope.frame.settings;
  381. }, true);
  382. } ]);