Blame view

static/js/myqccr.js 7.66 KB
831eac332   zhuzhenchao   add file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  /**
   * Created by wangfei on 2015/11/7.
   */
  function getCookie(name) {
              var cookieValue = null;
              if (document.cookie && document.cookie != '') {
                  var cookies = document.cookie.split(';');
                  for (var i = 0; i < cookies.length; i++) {
                      var cookie = jQuery.trim(cookies[i]);
                      // Does this cookie string begin with the name we want?
                      if (cookie.substring(0, name.length + 1) == (name + '=')) {
                          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                          break;
                      }
                  }
              }
              return cookieValue;
          }
  
  // Cross-browser event handlers.
  function addEvent(obj, evType, fn) {
      if (obj.addEventListener) {
          obj.addEventListener(evType, fn, false);
          return true;
      } else if (obj.attachEvent) {
          var r = obj.attachEvent("on" + evType, fn);
          return r;
      } else {
          return false;
      }
  }
  
  // select2 动态搜索用户信息: 返回用户id
  function search_user_id(selector){
      selector = selector || '.user-select';
      $(selector).select2({
          minimumInputLength: 1,   //至少输入n个字符,才去加载数据
          allowClear: true,  //是否允许用户清除文本信息
          delay: 500,
          formatInputTooShort: "请输入用户名",
          formatNoMatches: "没有匹配的用户",
          formatSearching: "查询中...",
          ajax: {
              url: "/account/search_user_for_select2/",
              dataType: "json",
              type: "POST",
              data: function (params) {
                  return {
                      term: params.term, // search term
                      page: params.page
                  };
              },
              processResults: function (data, params) {
                  params.page = params.page || 1;
                  return {
                      results: data
                  };
              },
              cache: true
          },
          templateResult: format_search_result,
          templateSelection: format_selected_option
      });
  }
  
  // select2 动态搜索用户信息: 返回邮箱
  function search_user_email(selector){
      selector = selector || '.email-select';
      $(selector).select2({
          minimumInputLength: 1,   //至少输入n个字符,才去加载数据
          allowClear: true,  //是否允许用户清除文本信息
          delay: 500,
          formatInputTooShort: "请输入用户名",
          formatNoMatches: "没有匹配的用户",
          formatSearching: "查询中...",
          ajax: {
              url: "/account/search_user_for_select2/",
              dataType: "json",
              type: "POST",
              data: function (params) {
                  return {
                      term: params.term, // search term
                      page: params.page
                  };
              },
              processResults: function (data, params) {
                  params.page = params.page || 1;
                  // 如果需要返回邮件,讲id设为邮件地址
                  for (var i = 0; i < data.length; i ++ ) {
                      data[i]['id'] = data[i]['email'];
                  }
                  return {
                      results: data
                  };
              },
              cache: true
          },
          templateResult: format_search_result,
          templateSelection: format_selected_option
      });
  }
  
  // select2 动态搜索用户信息: 返回用户名
  function search_user_username(selector){
      selector = selector || '.username-select';
      $(selector).select2({
          minimumInputLength: 1,   //至少输入n个字符,才去加载数据
          allowClear: true,  //是否允许用户清除文本信息
          delay: 500,
          formatInputTooShort: "请输入用户名",
          formatNoMatches: "没有匹配的用户",
          formatSearching: "查询中...",
          ajax: {
              url: "/account/search_user_for_select2/",
              dataType: "json",
              type: "POST",
              data: function (params) {
                  return {
                      term: params.term, // search term
                      page: params.page
                  };
              },
              processResults: function (data, params) {
                  params.page = params.page || 1;
                  // 如果需要返回邮件,讲id设为邮件地址
                  for (var i = 0; i < data.length; i ++ ) {
                      data[i]['id'] = data[i]['username'];
                  }
                  return {
                      results: data
                  };
              },
              cache: true
          },
          templateResult: format_search_result,
          templateSelection: format_selected_option
      });
  }
  
  function format_selected_option(data, container) {
      // console.log(data, container);
      if(data.chinese_name && data.dept__name){
          return data.chinese_name + ' - ' + data.dept__name;
      }else{
          return data.text
      }
  
  }
  
  function format_search_result(result) {
      if (!result.id) { return result.text; }
      var $result = $(
          '<span> ' + result.chinese_name + ' - ' + result.dept__name + '</span>'
      );
      return $result;
  }
  
  function format_html(content) {
      return content.replace(/
  /g, '<br/>')
          .replace(/&/g, "&gt;")
          .replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/ /g, "&nbsp;")
          .replace(/\'/g, "&#39;")
          .replace(/\"/g, "&quot;");
  }
  
  //判断当前字符串是否以str开始 先判断是否存在function是避免和js原生方法冲突,自定义方法的效率不如原生的高
  if (typeof String.prototype.startsWith != 'function') {
      String.prototype.startsWith = function (str){
          return this.slice(0, str.length) == str;
      };
  }
      
  //判断当前字符串是否以str结束
  if (typeof String.prototype.endsWith != 'function') {
      String.prototype.endsWith = function (str){
          return this.slice(-str.length) == str;
      };
  }
  
  // 弹出等待中提示框
  function message_loading(text){
      swal({
          title: "",
          text: text,
          imageUrl: "/static/image/reload.gif",
          showCancelButton: false,
          showConfirmButton: false,
      });
  }
  
  /**
   * 用JS实现用 字符串 替换 占位符;
   * alert("http://{0}/{1}".format("www.songyanjun.net", "index.html"));
   * @returns {String}
   */
  String.prototype.format=function() {
    if(arguments.length==0) return this;
    for(var s=this, i=0; i<arguments.length; i++)
      s=s.replace(new RegExp("\\{"+i+"\\}","g"), arguments[i]);
    return s;
  };
  
  /**
   * 页面弹出提示框
   * @param text
   * @param options
   * @param callback 点击的回调函数
   */
  function notice(text, options, callback) {
      text = text || '';
      options = options || {};
      var layout = options.layout || 'topRight';
      var type = options.type || 'warning';
      var timeout = options.timeout || false;
  
      callback = callback || function() {};
      if(text!='') {
          noty({
              text: text,
              layout: layout,
              type: type,
              animation: {
                  open: {height: 'toggle'}, // jQuery animate function property object
                  close: {height: 'toggle'}, // jQuery animate function property object
                  easing: 'swing', // easing
                  speed: 500 // opening & closing animation speed
              },
              timeout: timeout,
              maxVisible: 100,
              callback: { // 回调函数
                  onShow: function() {}, // 显示之前
                  afterShow: function() {}, // 显示之后
                  onClose: function() {}, // 关闭之前
                  afterClose: function() {}, // 关闭之后
                  onCloseClick: callback
              }
          });
      }
  }