baseServices.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. var baseServices = angular.module( "baseServices", [] );
  2. baseServices.factory("$jsonToFormData",function() {
  3. function transformRequest( data, getHeaders ) {
  4. var headers = getHeaders();
  5. headers["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8";
  6. return $.param(data);
  7. }
  8. return( transformRequest );
  9. })
  10. .directive('htCheckbox', function() {
  11. return {
  12. restrict: 'A',
  13. link: function(scope, element, attrs) {
  14. var key = attrs["htCheckbox"];
  15. var getValAry = function(){
  16. var val = getValByScope(element,key);
  17. if(val){
  18. return val.split(",");
  19. }
  20. return [];
  21. };
  22. scope.$watch(key,function(newVal,oldVal){
  23. var ary = getValAry();
  24. if(ary&&ary.length&&ary.join(",").indexOf(attrs["value"])!=-1){
  25. element[0].checked = true;
  26. }
  27. });
  28. element.bind('change',function(){
  29. var elementVal = element[0].checked,
  30. option = attrs["value"],
  31. array = getValAry();
  32. if(elementVal){
  33. array.push(option);
  34. }
  35. else{
  36. array.remove(option);
  37. }
  38. setValToScope(element,array.join(','),null,key);
  39. });
  40. }
  41. };
  42. })
  43. .directive('htDate', function() {
  44. return {
  45. restrict: 'A',
  46. link: function(scope, element, attrs) {
  47. var ngModel = attrs.ngModel;
  48. switch(attrs.htDate){
  49. case "date":
  50. $(element).on("focus",function(){
  51. var me = $(this);
  52. WdatePicker({dateFmt:'yyyy-MM-dd',alwaysUseStartDate:true});
  53. me.blur();
  54. scope.$apply(function(){
  55. eval("(scope." + ngModel + "='" + me.val() + "')");
  56. });
  57. });
  58. break;
  59. case "datetime":
  60. $(element).on("focus",function(){
  61. var me = $(this);
  62. WdatePicker({dateFmt:'yyyy-MM-dd',alwaysUseStartDate:true});
  63. me.blur();
  64. scope.$apply(function(){
  65. eval("(scope." + ngModel + "='" + me.val() + "')");
  66. });
  67. });
  68. break;
  69. case "wdateTime":
  70. $(element).on("focus",function(){
  71. var me = $(this), dateFmt= (me.attr('datefmt')?me.attr('datefmt'):'yyyy-MM-dd');
  72. WdatePicker({dateFmt:dateFmt,alwaysUseStartDate:true});
  73. me.blur();
  74. scope.$apply(function(){
  75. eval("(scope." + ngModel + "='" + me.val() + "')");
  76. });
  77. });
  78. break;
  79. }
  80. }
  81. };
  82. })
  83. .service('BaseService', ['$http','$jsonToFormData', function($http,$jsonToFormData) {
  84. var service = {
  85. get:function(url,callback){
  86. $http.get(url).success(function(data,status){
  87. if(callback){
  88. callback(data,status);
  89. }
  90. })
  91. .error(function(data,status){
  92. if(callback){
  93. callback(data,status);
  94. }
  95. //TODO 根据返回的错误状态(status)显示对应的全局提示
  96. });
  97. },
  98. post:function(url,param,callback){
  99. $http.post(url,param,{transformRequest:$jsonToFormData})
  100. .success(function(data,status){
  101. if(callback){
  102. callback(data,status);
  103. }
  104. })
  105. .error(function(data,status){
  106. if(callback){
  107. callback(data,status);
  108. }
  109. //TODO 根据返回的错误状态(status)显示对应的全局提示
  110. });
  111. },
  112. //m内容,b:true->alert输出;false:console
  113. show:function(m,b){
  114. if(b==null||b==false){
  115. // console.info(m);
  116. }else{
  117. alert(m+"");
  118. }
  119. }
  120. }
  121. return service;
  122. }])
  123. .directive('chooseTarget', [function() {
  124. return {
  125. restrict: 'EAC',
  126. scope:{
  127. right:'='
  128. },
  129. link: function(scope, element, attrs) {
  130. var opList = [{k:'',v:'请选择'}
  131. ,{k:'none',v:'无'}
  132. ,{k:'everyone',v:'所有人'}
  133. ,{k:'user',v:'用户'}
  134. ,{k:'role',v:'角色'}
  135. ,{k:'org',v:'组织'}
  136. ,{k:'orgMgr',v:'组织负责人'}
  137. ,{k:'pos',v:'岗位'}
  138. ,{k:'script',v:'脚本'}],
  139. opStr = "",
  140. opNo = attrs.opList?parseToJson(attrs.opList).no:"";
  141. opNo = opNo?opNo.split(","):"";
  142. scope.getOpition = function(){
  143. opStr= "";
  144. for(var i = 0 ; i < opList.length ; i++){
  145. if($.inArray(opList[i].k,opNo)==-1)
  146. opStr +='<option value="'+opList[i].k+'">'+opList[i].v+'</option>';
  147. }
  148. return opStr;
  149. };
  150. scope.v = "v";
  151. if(!scope.right) scope.right = {};
  152. if(!scope.right.hasOwnProperty("v"))
  153. scope.v = "type";
  154. scope.handClick = function(){
  155. // 选择回调
  156. var callback = function(ids, names) {
  157. scope.right.name =names;
  158. scope.right.id = ids;
  159. scope.$digest();
  160. };
  161. switch (scope.right[scope.v]) {
  162. case "user" :
  163. UserDialog({callback : callback});
  164. break;
  165. case "role" :
  166. RoleDialog({callback : callback});
  167. break;
  168. case "org" :
  169. case "orgMgr" :
  170. OrgDialog({callback : callback});
  171. break;
  172. case "pos" :
  173. PosDialog({callback : callback});
  174. break;
  175. case "script" :
  176. ScriptDialog({
  177. callback : function(script) {
  178. scope.right.script =script;
  179. scope.$digest();
  180. }
  181. });
  182. break;
  183. }
  184. };
  185. scope.resetRight = function(){
  186. var v = scope.v,
  187. val = scope.right[v];
  188. scope.right = {};
  189. scope.right[v] = val;
  190. };
  191. scope.remove = function(i){
  192. var ids = scope.right.id.split(",");
  193. var names = scope.right.name.split(",");
  194. ids.splice(i,1);
  195. names.splice(i,1);
  196. scope.right.id = ids.join(",");
  197. scope.right.name = names.join(",");
  198. };
  199. },
  200. template: '<select class="ht-input w-120" ng-model="right[v]" ht-bind-html="getOpition()" validate="{required:true}" ng-change="resetRight()">'
  201. +'</select>'
  202. +'<span ng-hide="right[v]==\'script\'||right[v]==\'everyone\'||right[v]==\'none\'||!right[v]||right.name==\'\'" class="ht-input" style="width: initial;max-width: 260px;">'
  203. +'<span class="owner-span" ng-repeat="item in right.name.split(\',\')">{{item}}'
  204. +'<a class="flootbutton" title="移除该项" ng-click="remove($index)">x</a>'
  205. +'</span>'
  206. +'</span>'
  207. +'<span class="bt-select" ng-click="handClick()" ng-show="right[v]&&right[v]!=\'none\'&&right[v]!=\'everyone\'">选择</span>'
  208. +'<span ng-show="right[v]==\'script\'">'
  209. +'<textarea cols="40" rows="3" ng-model="right.script" class="ht-input" validate="{required:true}" ></textarea>'
  210. +'<a href="javascript:;" class="bt-select" ng-click="handClick()">常用脚本</a>'
  211. +'</span>',
  212. replace: false
  213. };
  214. }])
  215. // <dialog-buttons></dialog-buttons>
  216. .directive('dialogButtons', function($compile) {
  217. return {
  218. restrict : 'E',
  219. replace : true,
  220. template : '<div class="hide-panel">'+
  221. '<div class="panel-top">'+
  222. '<div class="panel-toolbar">'+
  223. '<div class="toolBar">'+
  224. '<div class="group">'+
  225. '<a class="link save" href="javascript:;" ng-click="save();">'+
  226. '<span></span>'+
  227. '保存'+
  228. '</a>'+
  229. '</div>'+
  230. '<div class="group">'+
  231. '<a class="link del" href="javascript:;" ng-click="close()">'+
  232. '<span></span>'+
  233. '关闭'+
  234. '</a>'+
  235. '</div>'+
  236. '</div>'+
  237. '</div>'+
  238. '</div>'+
  239. '</div>'
  240. };
  241. })
  242. .directive('htBindHtml', function($compile) {
  243. return {
  244. restrict : 'A',
  245. link : function(scope, elm, attrs) {
  246. scope.unbindWatch = scope.$watch(attrs.htBindHtml, function(newVal, oldVal) {
  247. if (!elm.data('unbindWatchTag')) {
  248. if(newVal){
  249. elm.data('unbindWatchTag',true);
  250. elm.html(newVal);
  251. scope.htmlFn&&scope.htmlFn.call();
  252. $compile(elm)(scope);
  253. }
  254. else{
  255. //避免重复添加监视
  256. elm.data('unbindWatchTag')&&scope.unbindWatch();
  257. }
  258. }
  259. });
  260. }
  261. };
  262. })
  263. .directive('onFinishRenderFilters', function ($timeout) {
  264. return {
  265. restrict: 'A',
  266. link: function(scope, element, attr) {
  267. if (scope.$last === true) {
  268. $timeout(function() {
  269. scope.$emit('ngRepeatFinished');
  270. });
  271. }
  272. }
  273. };
  274. })
  275. ;