You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

selectpage.js 80 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280
  1. /**
  2. * @summary SelectPage
  3. * @desc Simple and powerful selection plugin
  4. * @file selectpage.js
  5. * @version 2.18
  6. * @author TerryZeng
  7. * @contact https://terryz.github.io/
  8. * @license MIT License
  9. *
  10. */
  11. ;
  12. (function ($) {
  13. "use strict";
  14. /**
  15. * Default options
  16. */
  17. var defaults = {
  18. /**
  19. * Data source
  20. * @type {string|Object}
  21. *
  22. * string:server side request url address
  23. * Object:JSON array,format:[{a:1,b:2,c:3},{...}]
  24. */
  25. data: undefined,
  26. /**
  27. * Language ('cn', 'en', 'ja', 'es', 'pt-br')
  28. * @type string
  29. * @default 'cn'
  30. */
  31. lang: 'cn',
  32. /**
  33. * Multiple select mode(tags)
  34. * @type boolean
  35. * @default false
  36. */
  37. multiple: false,
  38. /**
  39. * pagination or not
  40. * @type boolean
  41. * @default true
  42. */
  43. pagination: true,
  44. /**
  45. * Show up menu button
  46. * @type boolean
  47. * @default true
  48. */
  49. dropButton: true,
  50. /**
  51. * Result list visible size in pagination bar close
  52. * @type number
  53. * @default 10
  54. */
  55. listSize: 10,
  56. /**
  57. * Show control bar in multiple select mode
  58. * @type boolean
  59. * @default true
  60. */
  61. multipleControlbar: true,
  62. /**
  63. * Max selected item limited in multiple select mode
  64. * @type number
  65. * @default 0(unlimited)
  66. */
  67. maxSelectLimit: 0,
  68. /**
  69. * Select result item to close list, work on multiple select mode
  70. * @type boolean
  71. * @default false
  72. */
  73. selectToCloseList: false,
  74. /**
  75. * Init selected item key, the result will match to option.keyField option
  76. * @type string
  77. */
  78. initRecord: undefined,
  79. /**
  80. * The table parameter in server side mode
  81. * @type string
  82. */
  83. dbTable: 'tbl',
  84. /**
  85. * The value field, the value will fill to hidden element
  86. * @type string
  87. * @default 'id'
  88. */
  89. keyField: 'id',
  90. /**
  91. * The show text field, the text will show to input element or tags(multiple mode)
  92. * @type string
  93. * @default 'name'
  94. */
  95. showField: 'name',
  96. /**
  97. * Actually used to search field
  98. * @type string
  99. */
  100. searchField: undefined,
  101. /**
  102. * Search type ('AND' or 'OR')
  103. * @type string
  104. * @default 'AND'
  105. */
  106. andOr: 'AND',
  107. /**
  108. * Result sort type
  109. * @type array - if not set, will default used showField field
  110. * @example
  111. * orderBy : ['id desc']
  112. */
  113. orderBy: undefined,
  114. /**
  115. * Page size
  116. * @type number
  117. * @default 10
  118. */
  119. pageSize: 10,
  120. /**
  121. * Server side request parameters
  122. * @type function
  123. * @return object
  124. * @example params : function(){return {'name':'aa','sex':1};}
  125. */
  126. params: undefined,
  127. /**
  128. * Custom result list item show text
  129. * @type function
  130. * @param data {object} row data
  131. * @return string
  132. */
  133. formatItem: undefined,
  134. /**
  135. * Have some highlight item and lost focus, auto select the highlight item
  136. * @type boolean
  137. * @default false
  138. */
  139. autoFillResult: false,
  140. /**
  141. * Auto select first item in show up result list or search result
  142. * depend on `autoFillResult` option set to true
  143. * @type boolean
  144. * @default false
  145. */
  146. autoSelectFirst: false,
  147. /**
  148. * Whether clear input element text when enter some keywords to search and no result return
  149. * @type boolean
  150. * @default true
  151. */
  152. noResultClean: true,
  153. /**
  154. * Select only mode
  155. * @type boolean
  156. */
  157. selectOnly: false,
  158. /**
  159. * Input to search delay time, work on ajax data source
  160. * @type number
  161. * @default 0.5
  162. */
  163. inputDelay: 0.5,
  164. /**
  165. * -----------------------------------------Callback--------------------------------------------
  166. */
  167. /**
  168. * Result list item selected callback
  169. * @type function
  170. * @param object - selected item json data
  171. * @param self - plugin object
  172. */
  173. eSelect: undefined,
  174. /**
  175. * Before result list show up callback, you can do anything prepared
  176. * @param self - plugin object
  177. */
  178. eOpen: undefined,
  179. /**
  180. * Server side return data convert callback
  181. * @type function
  182. * @param data {object} server side return data
  183. * @param self {object} plugin object
  184. * @return {object} return data format:
  185. * @example
  186. * {
  187. * list : [{name:'aa',sex:1},{name:'bb',sex:1}...],
  188. * totalRow : 100
  189. * }
  190. */
  191. eAjaxSuccess: undefined,
  192. /**
  193. * Close selected item tag callback (multiple mode)
  194. * @type function
  195. * @param removeCount {number} remove item count
  196. * @param self {object} plugin object
  197. */
  198. eTagRemove: undefined,
  199. /**
  200. * Clear selected item callback(single select mode)
  201. * @type function
  202. * @param self {object} plugin object
  203. */
  204. eClear: undefined
  205. };
  206. /**
  207. * SelectPage class definition
  208. * @constructor
  209. * @param {Object} input - input element
  210. * @param {Object} option
  211. */
  212. var SelectPage = function (input, option) {
  213. //特殊字段处理
  214. $.each({data: 'source', keyField: 'primaryKey', showField: 'field', pageSize: 'perPage'}, function (i, j) {
  215. if (typeof option[j] !== 'undefined') {
  216. option[i] = option[j];
  217. delete option[j];
  218. }
  219. });
  220. this.setOption(option);
  221. this.setLanguage();
  222. this.setCssClass();
  223. this.setProp();
  224. this.setElem(input);
  225. this.setButtonAttrDefault();
  226. this.setInitRecord();
  227. this.eDropdownButton();
  228. this.eInput();
  229. this.eWhole();
  230. };
  231. /**
  232. * Plugin version number
  233. */
  234. SelectPage.version = '2.18';
  235. /**
  236. * Plugin object cache key
  237. */
  238. SelectPage.dataKey = 'selectPageObject';
  239. /**
  240. * Options set
  241. * @param {Object} option
  242. */
  243. SelectPage.prototype.setOption = function (option) {
  244. //use showField to default
  245. option.searchField = option.searchField || option.showField;
  246. option.andOr = option.andOr.toUpperCase();
  247. if (option.andOr !== 'AND' && option.andOr !== 'OR')
  248. option.andOr = 'AND';
  249. //support multiple field set
  250. var arr = ['searchField'];
  251. for (var i = 0; i < arr.length; i++) {
  252. option[arr[i]] = this.strToArray(option[arr[i]]);
  253. }
  254. //set default order field
  255. option.orderBy = option.orderBy || option.showField;
  256. //set multiple order field
  257. //example: [ ['id', 'ASC'], ['name', 'DESC'] ]
  258. option.orderBy = this.setOrderbyOption(option.orderBy, option.showField);
  259. //close auto fill result and auto select first in multiple mode and select item not close list
  260. if (option.multiple && !option.selectToCloseList) {
  261. option.autoFillResult = false;
  262. option.autoSelectFirst = false;
  263. }
  264. //show all item when pagination bar close, limited 200
  265. if (!option.pagination)
  266. option.pageSize = 200;
  267. if ($.type(option.listSize) !== 'number' || option.listSize < 0)
  268. option.listSize = 10;
  269. this.option = option;
  270. };
  271. /**
  272. * String convert to array
  273. * @param str {string}
  274. * @return {Array}
  275. */
  276. SelectPage.prototype.strToArray = function (str) {
  277. if (!str)
  278. return '';
  279. return str.replace(/[\s ]+/g, '').split(',');
  280. };
  281. /**
  282. * Set order field
  283. * @param {Array} arg_order
  284. * @param {string} arg_field
  285. * @return {Array}
  286. */
  287. SelectPage.prototype.setOrderbyOption = function (arg_order, arg_field) {
  288. var arr = [], orders = [];
  289. if (typeof arg_order == 'object') {
  290. for (var i = 0; i < arg_order.length; i++) {
  291. orders = $.trim(arg_order[i]).split(' ');
  292. arr[i] = (orders.length == 2) ? orders : [orders[0], 'ASC'];
  293. }
  294. } else {
  295. orders = $.trim(arg_order).split(' ');
  296. arr[0] = (orders.length == 2) ? orders : (orders[0].match(/^(ASC|DESC)$/i)) ? [arg_field, orders[0]] : [orders[0], 'ASC'];
  297. }
  298. return arr;
  299. };
  300. /**
  301. * i18n
  302. */
  303. SelectPage.prototype.setLanguage = function () {
  304. var message, p = this.option;
  305. switch (p.lang) {
  306. // English
  307. case 'en':
  308. message = {
  309. add_btn: 'Add button',
  310. add_title: 'add a box',
  311. del_btn: 'Del button',
  312. del_title: 'delete a box',
  313. next: 'Next',
  314. next_title: 'Next' + p.pageSize + ' (Right key)',
  315. prev: 'Prev',
  316. prev_title: 'Prev' + p.pageSize + ' (Left key)',
  317. first_title: 'First (Shift + Left key)',
  318. last_title: 'Last (Shift + Right key)',
  319. get_all_btn: 'Get All (Down key)',
  320. get_all_alt: '(button)',
  321. close_btn: 'Close (Tab key)',
  322. close_alt: '(button)',
  323. loading: 'loading...',
  324. loading_alt: '(loading)',
  325. page_info: 'page_num of page_count',
  326. select_ng: 'Attention : Please choose from among the list.',
  327. select_ok: 'OK : Correctly selected.',
  328. not_found: 'not found',
  329. ajax_error: 'An error occurred while connecting to server.',
  330. clear: 'Clear content',
  331. select_all: 'Select current page',
  332. unselect_all: 'Clear current page',
  333. clear_all: 'Clear all selected',
  334. max_selected: 'You can only select up to max_selected_limit items'
  335. };
  336. break;
  337. // 中文
  338. case 'cn':
  339. default:
  340. message = {
  341. add_btn: '添加按钮',
  342. add_title: '添加区域',
  343. del_btn: '删除按钮',
  344. del_title: '删除区域',
  345. next: '下一页',
  346. next_title: '下' + p.pageSize + ' (→)',
  347. prev: '上一页',
  348. prev_title: '上' + p.pageSize + ' (←)',
  349. first_title: '首页 (Shift + ←)',
  350. last_title: '尾页 (Shift + →)',
  351. get_all_btn: '获得全部 (↓)',
  352. get_all_alt: '(按钮)',
  353. close_btn: '关闭 (Tab键)',
  354. close_alt: '(按钮)',
  355. loading: '读取中...',
  356. loading_alt: '(读取中)',
  357. page_info: '第 page_num 页(共page_count页)',
  358. select_ng: '请注意:请从列表中选择.',
  359. select_ok: 'OK : 已经选择.',
  360. not_found: '无查询结果',
  361. ajax_error: '连接到服务器时发生错误!',
  362. clear: '清除内容',
  363. select_all: '选择当前页项目',
  364. unselect_all: '取消选择当前页项目',
  365. clear_all: '清除全部已选择项目',
  366. max_selected: '最多只能选择 max_selected_limit 个项目'
  367. };
  368. break;
  369. }
  370. this.message = message;
  371. };
  372. /**
  373. * Css classname defined
  374. */
  375. SelectPage.prototype.setCssClass = function () {
  376. var css_class = {
  377. container: 'sp_container',
  378. container_open: 'sp_container_open',
  379. re_area: 'sp_result_area',
  380. result_open: 'sp_result_area_open',
  381. control_box: 'sp_control_box',
  382. //multiple select mode
  383. element_box: 'sp_element_box',
  384. navi: 'sp_navi',
  385. //result list
  386. results: 'sp_results',
  387. re_off: 'sp_results_off',
  388. select: 'sp_over',
  389. select_ok: 'sp_select_ok',
  390. select_ng: 'sp_select_ng',
  391. selected: 'sp_selected',
  392. input_off: 'sp_input_off',
  393. message_box: 'sp_message_box',
  394. disabled: 'sp_disabled',
  395. button: 'sp_button',
  396. btn_on: 'sp_btn_on',
  397. btn_out: 'sp_btn_out',
  398. input: 'sp_input',
  399. clear_btn: 'sp_clear_btn',
  400. align_right: 'sp_align_right'
  401. };
  402. this.css_class = css_class;
  403. };
  404. /**
  405. * Plugin inner properties
  406. */
  407. SelectPage.prototype.setProp = function () {
  408. this.prop = {
  409. //input disabled status
  410. disabled: false,
  411. current_page: 1,
  412. //total page
  413. max_page: 1,
  414. //ajax data loading status
  415. is_loading: false,
  416. xhr: false,
  417. key_paging: false,
  418. key_select: false,
  419. //last selected item value
  420. prev_value: '',
  421. //last selected item text
  422. selected_text: '',
  423. last_input_time: undefined,
  424. init_set: false
  425. };
  426. this.template = {
  427. tag: {
  428. content: '<li class="selected_tag" itemvalue="#item_value#">#item_text#<span class="tag_close"><i class="spfont sp-close"></i></span></li>',
  429. textKey: '#item_text#',
  430. valueKey: '#item_value#'
  431. },
  432. page: {
  433. current: 'page_num',
  434. total: 'page_count'
  435. },
  436. msg: {
  437. maxSelectLimit: 'max_selected_limit'
  438. }
  439. };
  440. };
  441. /**
  442. * Get the actual width/height of invisible DOM elements with jQuery.
  443. * Source code come from dreamerslab/jquery.actual
  444. * @param element
  445. * @param method
  446. * @returns {*}
  447. */
  448. SelectPage.prototype.elementRealSize = function (element, method) {
  449. var defaults = {
  450. absolute: false,
  451. clone: false,
  452. includeMargin: false,
  453. display: 'block'
  454. };
  455. var configs = defaults, $target = element.eq(0), fix, restore, tmp = [], style = '', $hidden;
  456. fix = function () {
  457. // get all hidden parents
  458. $hidden = $target.parents().addBack().filter(':hidden');
  459. style += 'visibility: hidden !important; display: ' + configs.display + ' !important; ';
  460. if (configs.absolute === true)
  461. style += 'position: absolute !important;';
  462. // save the origin style props
  463. // set the hidden el css to be got the actual value later
  464. $hidden.each(function () {
  465. // Save original style. If no style was set, attr() returns undefined
  466. var $this = $(this), thisStyle = $this.attr('style');
  467. tmp.push(thisStyle);
  468. // Retain as much of the original style as possible, if there is one
  469. $this.attr('style', thisStyle ? thisStyle + ';' + style : style);
  470. });
  471. };
  472. restore = function () {
  473. // restore origin style values
  474. $hidden.each(function (i) {
  475. var $this = $(this), _tmp = tmp[i];
  476. if (_tmp === undefined)
  477. $this.removeAttr('style');
  478. else
  479. $this.attr('style', _tmp);
  480. });
  481. };
  482. fix();
  483. // get the actual value with user specific methed
  484. // it can be 'width', 'height', 'outerWidth', 'innerWidth'... etc
  485. // configs.includeMargin only works for 'outerWidth' and 'outerHeight'
  486. var actual = /(outer)/.test(method) ?
  487. $target[method](configs.includeMargin) :
  488. $target[method]();
  489. restore();
  490. // IMPORTANT, this plugin only return the value of the first element
  491. return actual;
  492. };
  493. /**
  494. * Dom building
  495. * @param {Object} combo_input - original input element
  496. */
  497. SelectPage.prototype.setElem = function (combo_input) {
  498. // 1. build Dom object
  499. var elem = {}, p = this.option, css = this.css_class, msg = this.message, input = $(combo_input);
  500. var cssWidth = input.css("width");
  501. var orgWidth = input.outerWidth();
  502. if (cssWidth.indexOf("%") > -1 || input.parent().size() > 0 && input.parent().width() == orgWidth) {
  503. orgWidth = "100%";
  504. } else {
  505. // fix input width in hidden situation
  506. if (orgWidth <= 0)
  507. orgWidth = this.elementRealSize(input, 'outerWidth');
  508. if (orgWidth < 150)
  509. orgWidth = 150;
  510. }
  511. elem.combo_input = input.attr({'autocomplete': 'off'}).addClass(css.input).wrap('<div>');
  512. if (p.selectOnly)
  513. elem.combo_input.prop('readonly', true);
  514. elem.container = elem.combo_input.parent().addClass(css.container);
  515. if (elem.combo_input.prop('disabled')) {
  516. if (p.multiple)
  517. elem.container.addClass(css.disabled);
  518. else
  519. elem.combo_input.addClass(css.input_off);
  520. }
  521. // set outer box width
  522. elem.container.width(orgWidth);
  523. elem.button = $('<div>').addClass(css.button);
  524. //drop down button
  525. elem.dropdown = $('<span class="sp_caret"></span>');
  526. //clear button 'X' in single mode
  527. elem.clear_btn = $('<div>').html($('<i>').addClass('spfont sp-close')).addClass(css.clear_btn).attr('title', msg.clear);
  528. if (!p.dropButton)
  529. elem.clear_btn.addClass(css.align_right);
  530. //main box in multiple mode
  531. elem.element_box = $('<ul>').addClass(css.element_box);
  532. if (p.multiple && p.multipleControlbar)
  533. elem.control = $('<div>').addClass(css.control_box);
  534. //result list box
  535. elem.result_area = $('<div>').addClass(css.re_area);
  536. //pagination bar
  537. if (p.pagination)
  538. elem.navi = $('<div>').addClass('sp_pagination').append('<ul>');
  539. elem.results = $('<ul>').addClass(css.results);
  540. var namePrefix = '_text',
  541. input_id = elem.combo_input.attr('id') || elem.combo_input.attr('name'),
  542. input_name = elem.combo_input.attr('name') || 'selectPage',
  543. hidden_name = input_name,
  544. hidden_id = input_id;
  545. //switch the id and name attributes of input/hidden element
  546. elem.hidden = $('<input type="hidden" class="sp_hidden" />').attr({
  547. name: hidden_name,
  548. id: hidden_id
  549. }).val('');
  550. elem.combo_input.attr({
  551. name: typeof input.data('name') !== 'undefined' ? input.data('name') : input_name + namePrefix,
  552. id: input_id + namePrefix
  553. });
  554. // 2. DOM element put
  555. elem.container.append(elem.hidden);
  556. if (p.dropButton) {
  557. elem.container.append(elem.button)
  558. elem.button.append(elem.dropdown);
  559. }
  560. $(document.body).append(elem.result_area);
  561. elem.result_area.append(elem.results);
  562. if (p.pagination)
  563. elem.result_area.append(elem.navi);
  564. //Multiple select mode
  565. if (p.multiple) {
  566. if (p.multipleControlbar) {
  567. elem.control.append('<button type="button" class="btn btn-default sp_clear_all" ><i class="spfont sp-clear"></i></button>');
  568. elem.control.append('<button type="button" class="btn btn-default sp_unselect_all" ><i class="spfont sp-unselect-all"></i></button>');
  569. elem.control.append('<button type="button" class="btn btn-default sp_select_all" ><i class="spfont sp-select-all"></i></button>');
  570. elem.control_text = $('<p>');
  571. elem.control.append(elem.control_text);
  572. elem.result_area.prepend(elem.control);
  573. }
  574. elem.container.addClass('sp_container_combo');
  575. elem.combo_input.addClass('sp_combo_input').before(elem.element_box);
  576. var li = $('<li>').addClass('input_box');
  577. li.append(elem.combo_input);
  578. elem.element_box.append(li);
  579. if (elem.combo_input.attr('placeholder'))
  580. elem.combo_input.attr('placeholder_bak', elem.combo_input.attr('placeholder'));
  581. }
  582. this.elem = elem;
  583. };
  584. /**
  585. * Drop down button set to default
  586. */
  587. SelectPage.prototype.setButtonAttrDefault = function () {
  588. /*
  589. if (this.option.selectOnly) {
  590. if ($(this.elem.combo_input).val() !== '') {
  591. if ($(this.elem.hidden).val() !== '') {
  592. //选择条件
  593. $(this.elem.combo_input).attr('title', this.message.select_ok).removeClass(this.css_class.select_ng).addClass(this.css_class.select_ok);
  594. } else {
  595. //输入方式
  596. $(this.elem.combo_input).attr('title', this.message.select_ng).removeClass(this.css_class.select_ok).addClass(this.css_class.select_ng);
  597. }
  598. } else {
  599. $(this.elem.hidden).val('');
  600. $(this.elem.combo_input).removeAttr('title').removeClass(this.css_class.select_ng);
  601. }
  602. }
  603. */
  604. //this.elem.button.attr('title', this.message.get_all_btn);
  605. if (this.option.dropButton)
  606. this.elem.button.attr('title', this.message.close_btn);
  607. };
  608. /**
  609. * Set item need selected after init
  610. * set selected item ways:
  611. * <input value="key">
  612. * <input data-init="key">
  613. */
  614. SelectPage.prototype.setInitRecord = function (refresh) {
  615. var self = this, p = self.option, el = self.elem, key = '';
  616. if ($.type(el.combo_input.data('init')) != 'undefined')
  617. p.initRecord = String(el.combo_input.data('init'));
  618. //data-init and value attribute can be init plugin selected item
  619. //but, if set data-init and value attribute in the same time, plugin will choose data-init attribute first
  620. if (!refresh && !p.initRecord && el.combo_input.val())
  621. p.initRecord = el.combo_input.val();
  622. el.combo_input.val('');
  623. if (!refresh)
  624. el.hidden.val(p.initRecord);
  625. key = refresh && el.hidden.val() ? el.hidden.val() : p.initRecord;
  626. if (key) {
  627. if (typeof p.data === 'object') {
  628. var data = new Array();
  629. var keyarr = key.split(',');
  630. $.each(keyarr, function (index, row) {
  631. for (var i = 0; i < p.data.length; i++) {
  632. if (p.data[i][p.keyField] == row) {
  633. data.push(p.data[i]);
  634. break;
  635. }
  636. }
  637. });
  638. if (!p.multiple && data.length > 1)
  639. data = [data[0]];
  640. self.afterInit(self, data);
  641. } else {//ajax data source mode to init selected item
  642. var _paramsFunc = p.params, _params = {}, searchKey = p.searchField;
  643. var _orgParams = {
  644. searchTable: p.dbTable,
  645. searchKey: p.keyField,
  646. searchValue: key,
  647. orderBy: p.orderBy,
  648. showField: p.showField,
  649. keyField: p.keyField,
  650. keyValue: key,
  651. searchField: p.searchField
  652. };
  653. if (_paramsFunc) {
  654. var result = $.isFunction(_paramsFunc) ? _paramsFunc(self) : _paramsFunc;
  655. if (result && $.isPlainObject(result)) {
  656. _params = $.extend({}, _orgParams, result);
  657. } else {
  658. _params = _orgParams;
  659. }
  660. } else {
  661. _params = _orgParams;
  662. }
  663. $.ajax({
  664. dataType: 'json',
  665. type: 'POST',
  666. url: p.data,
  667. data: _params,
  668. success: function (json) {
  669. var d = null;
  670. if (p.eAjaxSuccess && $.isFunction(p.eAjaxSuccess))
  671. d = p.eAjaxSuccess(json);
  672. self.afterInit(self, d.list);
  673. },
  674. error: function (jqXHR, textStatus, errorThrown) {
  675. self.ajaxErrorNotify(self, errorThrown);
  676. }
  677. });
  678. }
  679. }
  680. };
  681. /**
  682. * Selected item set to plugin
  683. * @param {Object} self
  684. * @param {Object} data - selected item data
  685. */
  686. SelectPage.prototype.afterInit = function (self, data) {
  687. if (!data || ($.isArray(data) && data.length === 0))
  688. return;
  689. if (!$.isArray(data))
  690. data = [data];
  691. var p = self.option, css = self.css_class;
  692. var getText = function (row) {
  693. var text = row[p.showField];
  694. if (p.formatItem && $.isFunction(p.formatItem)) {
  695. try {
  696. text = p.formatItem(row);
  697. } catch (e) {
  698. }
  699. }
  700. return text;
  701. };
  702. if (p.multiple) {
  703. self.prop.init_set = true;
  704. self.clearAll(self);
  705. $.each(data, function (i, row) {
  706. var item = {text: getText(row), value: row[p.keyField]};
  707. if (!self.isAlreadySelected(self, item))
  708. self.addNewTag(self, item);
  709. });
  710. self.tagValuesSet(self);
  711. self.inputResize(self);
  712. console.log(self.elem.hidden.blur());
  713. self.prop.init_set = false;
  714. } else {
  715. var row = data[0];
  716. self.elem.combo_input.val(getText(row));
  717. self.elem.hidden.val(row[p.keyField]);
  718. self.prop.prev_value = getText(row);
  719. self.prop.selected_text = getText(row);
  720. if (p.selectOnly) {
  721. self.elem.combo_input.attr('title', self.message.select_ok).removeClass(css.select_ng).addClass(css.select_ok);
  722. }
  723. self.putClearButton();
  724. }
  725. };
  726. /**
  727. * Drop down button event bind
  728. */
  729. SelectPage.prototype.eDropdownButton = function () {
  730. var self = this;
  731. if (self.option.dropButton) {
  732. self.elem.button.mouseup(function (ev) {
  733. ev.stopPropagation();
  734. if (self.elem.result_area.is(':hidden') && !self.elem.combo_input.prop('disabled')) {
  735. self.elem.combo_input.focus();
  736. } else
  737. self.hideResults(self);
  738. });
  739. }
  740. };
  741. /**
  742. * Events bind
  743. */
  744. SelectPage.prototype.eInput = function () {
  745. var self = this, p = self.option, el = self.elem, msg = self.message;
  746. var showList = function () {
  747. self.prop.page_move = false;
  748. self.suggest(self);
  749. self.setCssFocusedInput(self);
  750. };
  751. el.combo_input.keyup(function (e) {
  752. self.processKey(self, e);
  753. }).keydown(function (e) {
  754. self.processControl(self, e);
  755. }).focus(function (e) {
  756. //When focus on input, show the result list
  757. if (el.result_area.is(':hidden')) {
  758. e.stopPropagation();
  759. self.prop.first_show = true;
  760. showList();
  761. }
  762. });
  763. el.container.on('click.SelectPage', 'div.' + self.css_class.clear_btn, function (e) {
  764. e.stopPropagation();
  765. if (!self.disabled(self)) {
  766. self.clearAll(self);
  767. self.elem.hidden.change();
  768. if (p.eClear && $.isFunction(p.eClear))
  769. p.eClear(self);
  770. }
  771. });
  772. el.result_area.on('mousedown.SelectPage', function (e) {
  773. e.stopPropagation();
  774. });
  775. if (p.multiple) {
  776. if (p.multipleControlbar) {
  777. //Select all item of current page
  778. el.control.find('.sp_select_all').on('click.SelectPage', function (e) {
  779. self.selectAllLine(self);
  780. }).hover(function () {
  781. el.control_text.html(msg.select_all);
  782. }, function () {
  783. el.control_text.html('');
  784. });
  785. //Cancel select all item of current page
  786. el.control.find('.sp_unselect_all').on('click.SelectPage', function (e) {
  787. self.unSelectAllLine(self);
  788. }).hover(function () {
  789. el.control_text.html(msg.unselect_all);
  790. }, function () {
  791. el.control_text.html('');
  792. });
  793. //Clear all selected item
  794. el.control.find('.sp_clear_all').on('click.SelectPage', function (e) {
  795. self.clearAll(self);
  796. }).hover(function () {
  797. el.control_text.html(msg.clear_all);
  798. }, function () {
  799. el.control_text.html('');
  800. });
  801. }
  802. el.element_box.on('click.SelectPage', function (e) {
  803. var srcEl = e.target || e.srcElement;
  804. if ($(srcEl).is('ul'))
  805. el.combo_input.focus();
  806. });
  807. //Tag close
  808. el.element_box.on('click.SelectPage', 'span.tag_close', function () {
  809. var li = $(this).closest('li');
  810. self.removeTag(self, li);
  811. showList();
  812. if (p.eTagRemove && $.isFunction(p.eTagRemove))
  813. p.eTagRemove(1, self);
  814. });
  815. self.inputResize(self);
  816. }
  817. };
  818. /**
  819. * Out of plugin area click event handler
  820. */
  821. SelectPage.prototype.eWhole = function () {
  822. var self = this, css = self.css_class;
  823. var cleanContent = function (obj) {
  824. obj.elem.combo_input.val('');
  825. if (!obj.option.multiple)
  826. obj.elem.hidden.val('');
  827. obj.prop.selected_text = '';
  828. };
  829. //Out of plugin area
  830. $(document.body).off('mousedown.selectPage').on('mousedown.selectPage', function (e) {
  831. var ele = e.target || e.srcElement;
  832. var sp = $(ele).closest('div.' + css.container);
  833. //Open status result list
  834. $('div.' + css.container + '.' + css.container_open).each(function () {
  835. if (this == sp[0])
  836. return;
  837. var $this = $(this), d = $this.find('input.' + css.input).data(SelectPage.dataKey);
  838. if (!d.elem.combo_input.val() && d.elem.hidden.val() && !d.option.multiple) {
  839. d.prop.current_page = 1;//reset page to 1
  840. cleanContent(d);
  841. d.hideResults(d);
  842. return true;
  843. }
  844. if (d.elem.results.find('li').not('.' + css.message_box).size()) {
  845. if (d.option.autoFillResult) {
  846. //have selected item, then hide result list
  847. if (d.elem.hidden.val())
  848. d.hideResults(d);
  849. else if (d.elem.results.find('li.sp_over').size()) {
  850. //no one selected and have highlight item, select the highlight item
  851. d.selectCurrentLine(d, true);
  852. } else if (d.option.autoSelectFirst) {
  853. //no one selected, no one highlight, select the first item
  854. d.nextLine(d);
  855. d.selectCurrentLine(d, true);
  856. } else
  857. d.hideResults(d);
  858. } else
  859. d.hideResults(d);
  860. } else {
  861. //when no one item match, clear search keywords
  862. if (d.option.noResultClean)
  863. cleanContent(d);
  864. else {
  865. if (!d.option.multiple)
  866. d.elem.hidden.val('');
  867. }
  868. d.hideResults(d);
  869. }
  870. });
  871. });
  872. };
  873. /**
  874. * Result list event bind
  875. */
  876. SelectPage.prototype.eResultList = function () {
  877. var self = this, css = this.css_class;
  878. self.elem.results.children('li').hover(function () {
  879. if (self.prop.key_select) {
  880. self.prop.key_select = false;
  881. return;
  882. }
  883. if (!$(this).hasClass(css.selected) && !$(this).hasClass(css.message_box)) {
  884. $(this).addClass(css.select);
  885. self.setCssFocusedResults(self);
  886. }
  887. }, function () {
  888. $(this).removeClass(css.select);
  889. }).click(function (e) {
  890. if (self.prop.key_select) {
  891. self.prop.key_select = false;
  892. return;
  893. }
  894. e.preventDefault();
  895. e.stopPropagation();
  896. if (!$(this).hasClass(css.selected))
  897. self.selectCurrentLine(self, false);
  898. });
  899. };
  900. /**
  901. * Reposition result list when list beyond the visible area
  902. */
  903. SelectPage.prototype.eScroll = function () {
  904. var self = this, css = this.css_class;
  905. $(window).on('scroll.SelectPage', function (e) {
  906. $('div.' + css.container + '.' + css.container_open).each(function () {
  907. var $this = $(this), d = $this.find('input.' + css.input).data(SelectPage.dataKey),
  908. offset = d.elem.result_area.offset(),
  909. screenScrollTop = $(window).scrollTop(),
  910. docHeight = $(document).height(),
  911. viewHeight = $(window).height(),
  912. listHeight = d.elem.result_area.outerHeight(),
  913. listBottom = offset.top + listHeight,
  914. hasOverflow = docHeight > viewHeight,
  915. down = d.elem.result_area.hasClass('shadowDown');
  916. if (hasOverflow) {
  917. if (down) {//open down
  918. if (listBottom > (viewHeight + screenScrollTop))
  919. d.calcResultsSize(d);
  920. } else {//open up
  921. if (offset.top < screenScrollTop)
  922. d.calcResultsSize(d);
  923. }
  924. }
  925. });
  926. });
  927. };
  928. /**
  929. * Page bar button event bind
  930. */
  931. SelectPage.prototype.ePaging = function () {
  932. var self = this;
  933. if (!self.option.pagination)
  934. return;
  935. self.elem.navi.find('li.csFirstPage').off('click').on('click', function (ev) {
  936. //$(self.elem.combo_input).focus();
  937. ev.preventDefault();
  938. self.firstPage(self);
  939. });
  940. self.elem.navi.find('li.csPreviousPage').off('click').on('click', function (ev) {
  941. //$(self.elem.combo_input).focus();
  942. ev.preventDefault();
  943. self.prevPage(self);
  944. });
  945. self.elem.navi.find('li.csNextPage').off('click').on('click', function (ev) {
  946. //$(self.elem.combo_input).focus();
  947. ev.preventDefault();
  948. self.nextPage(self);
  949. });
  950. self.elem.navi.find('li.csLastPage').off('click').on('click', function (ev) {
  951. //$(self.elem.combo_input).focus();
  952. ev.preventDefault();
  953. self.lastPage(self);
  954. });
  955. };
  956. /**
  957. * Ajax request fail
  958. * @param {Object} self
  959. * @param {string} errorThrown
  960. */
  961. SelectPage.prototype.ajaxErrorNotify = function (self, errorThrown) {
  962. self.showMessage(self.message.ajax_error);
  963. };
  964. /**
  965. * Message box
  966. * @param {Object} self
  967. * @param msg {string} the text need to show
  968. */
  969. SelectPage.prototype.showMessage = function (self, msg) {
  970. if (!msg)
  971. return;
  972. var msgLi = '<li class="' + self.css_class.message_box + '"><i class="spfont sp-warning"></i> ' + msg + '</li>';
  973. self.elem.results.empty().append(msgLi).show();
  974. self.calcResultsSize(self);
  975. self.setOpenStatus(self, true);
  976. self.elem.control.hide();
  977. if (self.option.pagination)
  978. self.elem.navi.hide();
  979. };
  980. /**
  981. * @desc Scroll
  982. * @param {Object} self
  983. * @param {boolean} enforce
  984. */
  985. SelectPage.prototype.scrollWindow = function (self, enforce) {
  986. var current_result = self.getCurrentLine(self),
  987. target_top = (current_result && !enforce) ? current_result.offset().top : self.elem.container.offset().top,
  988. target_size;
  989. self.prop.size_li = self.elem.results.children('li:first').outerHeight();
  990. target_size = self.prop.size_li;
  991. var gap, client_height = $(window).height(),
  992. scroll_top = $(window).scrollTop(),
  993. scroll_bottom = scroll_top + client_height - target_size;
  994. if (current_result.length) {
  995. if (target_top < scroll_top || target_size > client_height) {
  996. //scroll to top
  997. gap = target_top - scroll_top;
  998. } else if (target_top > scroll_bottom) {
  999. //scroll down
  1000. gap = target_top - scroll_bottom;
  1001. } else
  1002. return; //do not scroll
  1003. } else if (target_top < scroll_top)
  1004. gap = target_top - scroll_top;
  1005. window.scrollBy(0, gap);
  1006. };
  1007. /**
  1008. * change css class by status
  1009. * @param self
  1010. * @param status {boolean} true: open, false: close
  1011. */
  1012. SelectPage.prototype.setOpenStatus = function (self, status) {
  1013. var el = self.elem, css = self.css_class;
  1014. if (status) {
  1015. el.container.addClass(css.container_open);
  1016. el.result_area.addClass(css.result_open);
  1017. } else {
  1018. el.container.removeClass(css.container_open);
  1019. el.result_area.removeClass(css.result_open);
  1020. }
  1021. };
  1022. /**
  1023. * input element in focus css class set
  1024. * @param {Object} self
  1025. */
  1026. SelectPage.prototype.setCssFocusedInput = function (self) {
  1027. //$(self.elem.results).addClass(self.css_class.re_off);
  1028. //$(self.elem.combo_input).removeClass(self.css_class.input_off);
  1029. };
  1030. /**
  1031. * set result list get focus and input element lost focus
  1032. * @param {Object} self
  1033. */
  1034. SelectPage.prototype.setCssFocusedResults = function (self) {
  1035. //$(self.elem.results).removeClass(self.css_class.re_off);
  1036. //$(self.elem.combo_input).addClass(self.css_class.input_off);
  1037. };
  1038. /**
  1039. * Quick search input keywords listener
  1040. * @param {Object} self
  1041. */
  1042. SelectPage.prototype.checkValue = function (self) {
  1043. var now_value = self.elem.combo_input.val();
  1044. if (now_value != self.prop.prev_value) {
  1045. self.prop.prev_value = now_value;
  1046. self.prop.first_show = false;
  1047. if (self.option.selectOnly)
  1048. self.setButtonAttrDefault();
  1049. if (!self.option.multiple && !now_value) {
  1050. self.elem.combo_input.val('');
  1051. self.elem.hidden.val('');
  1052. self.elem.clear_btn.remove();
  1053. }
  1054. self.suggest(self);
  1055. }
  1056. };
  1057. /**
  1058. * Input handle(regular input)
  1059. * @param {Object} self
  1060. * @param {Object} e - event object
  1061. */
  1062. SelectPage.prototype.processKey = function (self, e) {
  1063. if ($.inArray(e.keyCode, [37, 38, 39, 40, 27, 9, 13]) === -1) {
  1064. if (e.keyCode != 16)
  1065. self.setCssFocusedInput(self); // except Shift(16)
  1066. self.inputResize(self);
  1067. if ($.type(self.option.data) === 'string') {
  1068. self.prop.last_input_time = e.timeStamp;
  1069. setTimeout(function () {
  1070. if ((e.timeStamp - self.prop.last_input_time) === 0)
  1071. self.checkValue(self);
  1072. }, self.option.inputDelay * 1000);
  1073. } else {
  1074. self.checkValue(self);
  1075. }
  1076. }
  1077. }
  1078. /**
  1079. * Input handle(control key)
  1080. * @param {Object} self
  1081. * @param {Object} e - event object
  1082. */
  1083. SelectPage.prototype.processControl = function (self, e) {
  1084. if (($.inArray(e.keyCode, [37, 38, 39, 40, 27, 9]) > -1 && self.elem.result_area.is(':visible')) ||
  1085. ($.inArray(e.keyCode, [13, 9]) > -1 && self.getCurrentLine(self))) {
  1086. e.preventDefault();
  1087. e.stopPropagation();
  1088. e.cancelBubble = true;
  1089. e.returnValue = false;
  1090. switch (e.keyCode) {
  1091. case 37:// left
  1092. if (e.shiftKey)
  1093. self.firstPage(self);
  1094. else
  1095. self.prevPage(self);
  1096. break;
  1097. case 38:// up
  1098. self.prop.key_select = true;
  1099. self.prevLine(self);
  1100. break;
  1101. case 39:// right
  1102. if (e.shiftKey)
  1103. self.lastPage(self);
  1104. else
  1105. self.nextPage(self);
  1106. break;
  1107. case 40:// down
  1108. if (self.elem.results.children('li').length) {
  1109. self.prop.key_select = true;
  1110. self.nextLine(self);
  1111. } else
  1112. self.suggest(self);
  1113. break;
  1114. case 9:// tab
  1115. self.prop.key_paging = true;
  1116. self.selectCurrentLine(self, true);
  1117. //self.hideResults(self);
  1118. break;
  1119. case 13:// return
  1120. self.selectCurrentLine(self, true);
  1121. break;
  1122. case 27:// escape
  1123. self.prop.key_paging = true;
  1124. self.hideResults(self);
  1125. break;
  1126. }
  1127. }
  1128. };
  1129. /**
  1130. * Abort Ajax request
  1131. * @param {Object} self
  1132. */
  1133. SelectPage.prototype.abortAjax = function (self) {
  1134. if (self.prop.xhr) {
  1135. self.prop.xhr.abort();
  1136. self.prop.xhr = false;
  1137. }
  1138. };
  1139. /**
  1140. * Suggest result of search keywords
  1141. * @param {Object} self
  1142. */
  1143. SelectPage.prototype.suggest = function (self) {
  1144. var q_word, val = $.trim(self.elem.combo_input.val());
  1145. if (self.option.multiple)
  1146. q_word = val;
  1147. else {
  1148. if (val && val === self.prop.selected_text)
  1149. q_word = '';
  1150. else
  1151. q_word = val;
  1152. }
  1153. q_word = q_word.split(/[\s ]+/);
  1154. //Before show up result list callback
  1155. if (self.option.eOpen && $.isFunction(self.option.eOpen))
  1156. self.option.eOpen.call(self);
  1157. self.abortAjax(self);
  1158. //self.setLoading(self);
  1159. var which_page_num = self.prop.current_page || 1;
  1160. if (typeof self.option.data == 'object')
  1161. self.searchForJson(self, q_word, which_page_num);
  1162. else
  1163. self.searchForDb(self, q_word, which_page_num);
  1164. };
  1165. /**
  1166. * Loading
  1167. * @param {Object} self
  1168. */
  1169. SelectPage.prototype.setLoading = function (self) {
  1170. if (self.elem.results.html() === '') {
  1171. //self.calcResultsSize(self);
  1172. self.setOpenStatus(self, true);
  1173. }
  1174. };
  1175. /**
  1176. * Search for ajax
  1177. * @param {Object} self
  1178. * @param {Array} q_word - query keyword
  1179. * @param {number} which_page_num - target page number
  1180. */
  1181. SelectPage.prototype.searchForDb = function (self, q_word, which_page_num) {
  1182. var p = self.option;
  1183. if (!p.eAjaxSuccess || !$.isFunction(p.eAjaxSuccess))
  1184. self.hideResults(self);
  1185. var _paramsFunc = p.params, _params = {}, searchKey = p.searchField;
  1186. //when have new query keyword, then reset page number to 1.
  1187. if (q_word.length && q_word[0] && q_word[0] !== self.prop.prev_value)
  1188. which_page_num = 1;
  1189. var _orgParams = {
  1190. q_word: q_word,
  1191. pageNumber: which_page_num,
  1192. pageSize: p.pageSize,
  1193. andOr: p.andOr,
  1194. orderBy: p.orderBy,
  1195. searchTable: p.dbTable,
  1196. showField: self.option.showField,
  1197. keyField: self.option.keyField,
  1198. searchField: self.option.searchField
  1199. };
  1200. _orgParams[searchKey] = q_word[0];
  1201. if (_paramsFunc) {
  1202. var result = $.isFunction(_paramsFunc) ? _paramsFunc(self) : _paramsFunc;
  1203. if (result && $.isPlainObject(result)) {
  1204. _params = $.extend({}, _orgParams, result);
  1205. } else {
  1206. _params = _orgParams;
  1207. }
  1208. } else {
  1209. _params = _orgParams;
  1210. }
  1211. self.prop.xhr = $.ajax({
  1212. dataType: 'json',
  1213. url: p.data,
  1214. type: 'POST',
  1215. data: _params,
  1216. success: function (returnData) {
  1217. if (!returnData || !$.isPlainObject(returnData)) {
  1218. self.hideResults(self);
  1219. self.ajaxErrorNotify(self, errorThrown);
  1220. return;
  1221. }
  1222. var data = {}, json = {};
  1223. try {
  1224. data = p.eAjaxSuccess(returnData);
  1225. json.originalResult = data.list;
  1226. json.cnt_whole = data.totalRow;
  1227. } catch (e) {
  1228. self.showMessage(self, self.message.ajax_error);
  1229. return;
  1230. }
  1231. if (self.elem.navi) {
  1232. $(self.elem.navi).toggleClass("hide", json.cnt_whole <= json.originalResult.length);
  1233. }
  1234. json.candidate = [];
  1235. json.keyField = [];
  1236. if (typeof json.originalResult != 'object') {
  1237. self.prop.xhr = null;
  1238. self.notFoundSearch(self);
  1239. return;
  1240. }
  1241. json.cnt_page = json.originalResult.length;
  1242. for (var i = 0; i < json.cnt_page; i++) {
  1243. for (var key in json.originalResult[i]) {
  1244. if (key == p.keyField) {
  1245. json.keyField.push(json.originalResult[i][key]);
  1246. }
  1247. if (key == p.showField) {
  1248. json.candidate.push(json.originalResult[i][key]);
  1249. }
  1250. }
  1251. }
  1252. self.prepareResults(self, json, q_word, which_page_num);
  1253. },
  1254. error: function (jqXHR, textStatus, errorThrown) {
  1255. if (textStatus != 'abort') {
  1256. self.hideResults(self);
  1257. self.ajaxErrorNotify(self, errorThrown);
  1258. }
  1259. },
  1260. complete: function () {
  1261. self.prop.xhr = null;
  1262. }
  1263. });
  1264. };
  1265. /**
  1266. * Search for json data source
  1267. * @param {Object} self
  1268. * @param {Array} q_word
  1269. * @param {number} which_page_num
  1270. */
  1271. SelectPage.prototype.searchForJson = function (self, q_word, which_page_num) {
  1272. var p = self.option, matched = [], esc_q = [], sorted = [], json = {}, i = 0, arr_reg = [];
  1273. //query keyword filter
  1274. do {
  1275. //'/\W/g'正则代表全部不是字母,数字,下划线,汉字的字符
  1276. //将非法字符进行转义
  1277. esc_q[i] = q_word[i].replace(/\W/g, '\\$&').toString();
  1278. arr_reg[i] = new RegExp(esc_q[i], 'gi');
  1279. i++;
  1280. } while (i < q_word.length);
  1281. // SELECT * FROM data WHERE field LIKE q_word;
  1282. for (i = 0; i < p.data.length; i++) {
  1283. var flag = false, row = p.data[i], itemText;
  1284. for (var j = 0; j < arr_reg.length; j++) {
  1285. itemText = row[p.searchField];
  1286. if (p.formatItem && $.isFunction(p.formatItem))
  1287. itemText = p.formatItem(row);
  1288. if (itemText.match(arr_reg[j])) {
  1289. flag = true;
  1290. if (p.andOr == 'OR')
  1291. break;
  1292. } else {
  1293. flag = false;
  1294. if (p.andOr == 'AND')
  1295. break;
  1296. }
  1297. }
  1298. if (flag)
  1299. matched.push(row);
  1300. }
  1301. // (CASE WHEN ...) then く order some column
  1302. var reg1 = new RegExp('^' + esc_q[0] + '$', 'gi'),
  1303. reg2 = new RegExp('^' + esc_q[0], 'gi'),
  1304. matched1 = [], matched2 = [], matched3 = [];
  1305. for (i = 0; i < matched.length; i++) {
  1306. var orderField = p.orderBy[0][0];
  1307. var orderValue = String(matched[i][orderField]);
  1308. if (orderValue.match(reg1)) {
  1309. matched1.push(matched[i]);
  1310. } else if (orderValue.match(reg2)) {
  1311. matched2.push(matched[i]);
  1312. } else {
  1313. matched3.push(matched[i]);
  1314. }
  1315. }
  1316. if (p.orderBy[0][1].match(/^asc$/i)) {
  1317. matched1 = self.sortAsc(self, matched1);
  1318. matched2 = self.sortAsc(self, matched2);
  1319. matched3 = self.sortAsc(self, matched3);
  1320. } else {
  1321. matched1 = self.sortDesc(self, matched1);
  1322. matched2 = self.sortDesc(self, matched2);
  1323. matched3 = self.sortDesc(self, matched3);
  1324. }
  1325. sorted = sorted.concat(matched1).concat(matched2).concat(matched3);
  1326. /*
  1327. if (sorted.length === undefined || sorted.length === 0 ) {
  1328. self.notFoundSearch(self);
  1329. return;
  1330. }
  1331. */
  1332. json.cnt_whole = sorted.length;
  1333. //page_move used to distinguish between init plugin or page moving
  1334. if (!self.prop.page_move) {
  1335. //only single mode can be used page number relocation
  1336. if (!p.multiple) {
  1337. //get selected item belong page number
  1338. var currentValue = self.elem.hidden.val();
  1339. if ($.type(currentValue) !== 'undefined' && $.trim(currentValue) !== '') {
  1340. var index = 0;
  1341. $.each(sorted, function (i, row) {
  1342. if (row[p.keyField] == currentValue) {
  1343. index = i + 1;
  1344. return false;
  1345. }
  1346. });
  1347. which_page_num = Math.ceil(index / p.pageSize);
  1348. if (which_page_num < 1)
  1349. which_page_num = 1;
  1350. self.prop.current_page = which_page_num;
  1351. }
  1352. }
  1353. } else {
  1354. //set page number to 1 when result number less then page size
  1355. if (sorted.length <= ((which_page_num - 1) * p.pageSize)) {
  1356. which_page_num = 1;
  1357. self.prop.current_page = 1;
  1358. }
  1359. }
  1360. //LIMIT xx OFFSET xx
  1361. var start = (which_page_num - 1) * p.pageSize, end = start + p.pageSize;
  1362. //save original data
  1363. json.originalResult = [];
  1364. //after data filter handle
  1365. for (i = start; i < end; i++) {
  1366. if (sorted[i] === undefined)
  1367. break;
  1368. json.originalResult.push(sorted[i]);
  1369. for (var key in sorted[i]) {
  1370. if (key == p.keyField) {
  1371. if (json.keyField === undefined)
  1372. json.keyField = [];
  1373. json.keyField.push(sorted[i][key]);
  1374. }
  1375. if (key == p.showField) {
  1376. if (json.candidate === undefined)
  1377. json.candidate = [];
  1378. json.candidate.push(sorted[i][key]);
  1379. }
  1380. }
  1381. }
  1382. if (json.candidate === undefined)
  1383. json.candidate = [];
  1384. json.cnt_page = json.candidate.length;
  1385. self.prepareResults(self, json, q_word, which_page_num);
  1386. };
  1387. /**
  1388. * Set order asc
  1389. * @param {Object} self
  1390. * @param {Array} arr - result array
  1391. */
  1392. SelectPage.prototype.sortAsc = function (self, arr) {
  1393. arr.sort(function (a, b) {
  1394. var valA = a[self.option.orderBy[0][0]], valB = b[self.option.orderBy[0][0]];
  1395. return $.type(valA) === 'number' ? valA - valB : String(valA).localeCompare(String(valB));
  1396. });
  1397. return arr;
  1398. };
  1399. /**
  1400. * Set order desc
  1401. * @param {Object} self
  1402. * @param {Array} arr - result array
  1403. */
  1404. SelectPage.prototype.sortDesc = function (self, arr) {
  1405. arr.sort(function (a, b) {
  1406. var valA = a[self.option.orderBy[0][0]], valB = b[self.option.orderBy[0][0]];
  1407. return $.type(valA) === 'number' ? valB - valA : String(valB).localeCompare(String(valA));
  1408. });
  1409. return arr;
  1410. };
  1411. /**
  1412. * Not result found handle
  1413. * @param {Object} self
  1414. */
  1415. SelectPage.prototype.notFoundSearch = function (self) {
  1416. self.elem.results.empty();
  1417. self.calcResultsSize(self);
  1418. self.setOpenStatus(self, true);
  1419. self.setCssFocusedInput(self);
  1420. };
  1421. /**
  1422. * Prepare data to show
  1423. * @param {Object} self
  1424. * @param {Object} json - data result
  1425. * @param {Array} q_word - query keyword
  1426. * @param {number} which_page_num - target page number
  1427. */
  1428. SelectPage.prototype.prepareResults = function (self, json, q_word, which_page_num) {
  1429. if (self.option.pagination)
  1430. self.setNavi(self, json.cnt_whole, json.cnt_page, which_page_num);
  1431. if (!json.keyField)
  1432. json.keyField = false;
  1433. if (self.option.selectOnly && json.candidate.length === 1 && json.candidate[0] == q_word[0]) {
  1434. self.elem.hidden.val(json.keyField[0]);
  1435. this.setButtonAttrDefault();
  1436. }
  1437. var is_query = false;
  1438. if (q_word && q_word.length && q_word[0])
  1439. is_query = true;
  1440. self.displayResults(self, json, is_query);
  1441. };
  1442. /**
  1443. * Build page bar
  1444. * @param {Object} self
  1445. * @param {number} cnt_whole - total record count
  1446. * @param {number} cnt_page
  1447. * @param {number} page_num - current page number
  1448. */
  1449. SelectPage.prototype.setNavi = function (self, cnt_whole, cnt_page, page_num) {
  1450. var msg = self.message;
  1451. /**
  1452. * build pagination bar
  1453. */
  1454. var buildPageNav = function (self, pagebar, page_num, last_page) {
  1455. var updatePageInfo = function () {
  1456. var pageInfo = msg.page_info;
  1457. return pageInfo.replace(self.template.page.current, page_num).replace(self.template.page.total, last_page);
  1458. };
  1459. if (pagebar.find('li').size() === 0) {
  1460. pagebar.hide().empty();
  1461. var iconFist = 'spfont sp-first',
  1462. iconPrev = 'spfont sp-previous',
  1463. iconNext = 'spfont sp-next',
  1464. iconLast = 'spfont sp-last';
  1465. pagebar.append('<li class="csFirstPage" title="' + msg.first_title + '" ><a href="javascript:void(0);"> <i class="' + iconFist + '"></i> </a></li>');
  1466. pagebar.append('<li class="csPreviousPage" title="' + msg.prev_title + '" ><a href="javascript:void(0);"><i class="' + iconPrev + '"></i></a></li>');
  1467. //pagination information
  1468. pagebar.append('<li class="pageInfoBox"><a href="javascript:void(0);"> ' + updatePageInfo() + ' </a></li>');
  1469. pagebar.append('<li class="csNextPage" title="' + msg.next_title + '" ><a href="javascript:void(0);"><i class="' + iconNext + '"></i></a></li>');
  1470. pagebar.append('<li class="csLastPage" title="' + msg.last_title + '" ><a href="javascript:void(0);"> <i class="' + iconLast + '"></i> </a></li>');
  1471. pagebar.show();
  1472. } else {
  1473. pagebar.find('li.pageInfoBox a').html(updatePageInfo());
  1474. }
  1475. };
  1476. var pagebar = self.elem.navi.find('ul'),
  1477. last_page = Math.ceil(cnt_whole / self.option.pageSize); //calculate total page
  1478. if (last_page === 0)
  1479. page_num = 0;
  1480. else {
  1481. if (last_page < page_num)
  1482. page_num = last_page;
  1483. else if (page_num === 0)
  1484. page_num = 1;
  1485. }
  1486. self.prop.current_page = page_num;//update current page number
  1487. self.prop.max_page = last_page;//update page count
  1488. buildPageNav(self, pagebar, page_num, last_page);
  1489. //update paging status
  1490. var dClass = 'disabled',
  1491. first = pagebar.find('li.csFirstPage'),
  1492. previous = pagebar.find('li.csPreviousPage'),
  1493. next = pagebar.find('li.csNextPage'),
  1494. last = pagebar.find('li.csLastPage');
  1495. //first and previous
  1496. if (page_num === 1 || page_num === 0) {
  1497. if (!first.hasClass(dClass))
  1498. first.addClass(dClass);
  1499. if (!previous.hasClass(dClass))
  1500. previous.addClass(dClass);
  1501. } else {
  1502. if (first.hasClass(dClass))
  1503. first.removeClass(dClass);
  1504. if (previous.hasClass(dClass))
  1505. previous.removeClass(dClass);
  1506. }
  1507. //next and last
  1508. if (page_num === last_page || last_page === 0) {
  1509. if (!next.hasClass(dClass))
  1510. next.addClass(dClass);
  1511. if (!last.hasClass(dClass))
  1512. last.addClass(dClass);
  1513. } else {
  1514. if (next.hasClass(dClass))
  1515. next.removeClass(dClass);
  1516. if (last.hasClass(dClass))
  1517. last.removeClass(dClass);
  1518. }
  1519. if (last_page > 1)
  1520. self.ePaging(); //pagination event bind
  1521. };
  1522. /**
  1523. * Render result list
  1524. * @param {Object} self
  1525. * @param {Object} json - result data
  1526. * @param {boolean} is_query - used to different from search to open and just click to open
  1527. */
  1528. SelectPage.prototype.displayResults = function (self, json, is_query) {
  1529. var p = self.option, el = self.elem;
  1530. el.results.hide().empty();
  1531. if (p.multiple && $.type(p.maxSelectLimit) === 'number' && p.maxSelectLimit > 0) {
  1532. var selectedSize = el.element_box.find('li.selected_tag').size();
  1533. if (selectedSize > 0 && selectedSize >= p.maxSelectLimit) {
  1534. var msg = self.message.max_selected;
  1535. self.showMessage(self, msg.replace(self.template.msg.maxSelectLimit, p.maxSelectLimit));
  1536. return;
  1537. }
  1538. }
  1539. if (json.candidate.length) {
  1540. var arr_candidate = json.candidate,
  1541. arr_primary_key = json.keyField,
  1542. keystr = el.hidden.val(),
  1543. keyArr = keystr ? keystr.split(',') : new Array(),
  1544. itemText = '';
  1545. for (var i = 0; i < arr_candidate.length; i++) {
  1546. if (p.formatItem && $.isFunction(p.formatItem)) {
  1547. try {
  1548. itemText = p.formatItem(json.originalResult[i]);
  1549. } catch (e) {
  1550. console.error('formatItem内容格式化函数内容设置不正确!');
  1551. itemText = arr_candidate[i];
  1552. }
  1553. } else
  1554. itemText = arr_candidate[i];
  1555. var list = $('<li>').html(itemText).attr({
  1556. pkey: arr_primary_key[i]
  1557. });
  1558. if (!p.formatItem)
  1559. list.attr('title', itemText);
  1560. //Set selected item highlight
  1561. if ($.inArray(arr_primary_key[i].toString(), keyArr) !== -1) {
  1562. list.addClass(self.css_class.selected);
  1563. }
  1564. //cache item data
  1565. list.data('dataObj', json.originalResult[i]);
  1566. el.results.append(list);
  1567. }
  1568. } else {
  1569. var li = '<li class="' + self.css_class.message_box + '"><i class="spfont sp-warning"></i> ' +
  1570. self.message.not_found + '</li>';
  1571. el.results.append(li);
  1572. }
  1573. el.results.show();
  1574. if (p.multiple && p.multipleControlbar)
  1575. el.control.show();
  1576. if (p.pagination)
  1577. el.navi.show();
  1578. self.calcResultsSize(self);
  1579. self.setOpenStatus(self, true);
  1580. //Result item event bind
  1581. self.eResultList();
  1582. //scrolling listen
  1583. self.eScroll();
  1584. //auto highlight first item in search, have result and set autoSelectFirst to true situation
  1585. if (is_query && json.candidate.length && p.autoSelectFirst)
  1586. self.nextLine(self);
  1587. };
  1588. /**
  1589. * Calculate result list size and position
  1590. * @param {Object} self
  1591. */
  1592. SelectPage.prototype.calcResultsSize = function (self) {
  1593. var p = self.option, el = self.elem;
  1594. var rePosition = function () {
  1595. if (el.container.css('position') === 'static') {
  1596. // position: static
  1597. var offset = el.combo_input.offset();
  1598. el.result_area.css({
  1599. top: offset.top + el.combo_input.outerHeight() + 'px',
  1600. left: offset.left + 'px'
  1601. });
  1602. } else {
  1603. if (!p.pagination) {
  1604. var itemHeight = el.results.find('li:first').outerHeight(true),
  1605. listHeight = itemHeight * p.listSize;
  1606. el.results.css({
  1607. 'max-height': listHeight,
  1608. 'overflow-y': 'auto'
  1609. });
  1610. }
  1611. //handle result list show up side(left, right, up or down)
  1612. var docWidth = $(document).width(),
  1613. docHeight = $(document).height(), //the document full height
  1614. viewHeight = $(window).height(), //browser visible area height
  1615. offset = el.container.offset(),
  1616. screenScrollTop = $(window).scrollTop(),
  1617. listWidth = el.result_area.outerWidth(),
  1618. //result list height
  1619. listHeight = el.result_area.outerHeight(),
  1620. //default left used input element left
  1621. defaultLeft = offset.left, //p.multiple ? -1 : 0;
  1622. //input element height
  1623. inputHeight = el.container.outerHeight(),
  1624. left = (offset.left + listWidth) > docWidth ?
  1625. defaultLeft - (listWidth - el.container.outerWidth()) :
  1626. defaultLeft,
  1627. //the actual top coordinate of input element(outer div)
  1628. screenTop = offset.top, //$(el.container).scrollTop();//offset.top - screenScrollTop;
  1629. top = 0, dist = 5, //set distance between input element and result list
  1630. //the actual top coordinate of result list
  1631. listBottom = screenTop + inputHeight + listHeight + dist,
  1632. hasOverflow = docHeight > viewHeight;
  1633. if ((screenTop - screenScrollTop - dist > listHeight) &&
  1634. (hasOverflow && listBottom > (viewHeight + screenScrollTop)) ||
  1635. (!hasOverflow && listBottom > viewHeight)) {
  1636. //open up
  1637. top = offset.top - listHeight - dist;
  1638. el.result_area.removeClass('shadowUp shadowDown').addClass('shadowUp');
  1639. } else {
  1640. //open down
  1641. top = offset.top + (p.multiple ? el.container.outerHeight() : inputHeight);
  1642. el.result_area.removeClass('shadowUp shadowDown').addClass('shadowDown');
  1643. top += dist;
  1644. }
  1645. return {
  1646. top: top + 'px', left: left + 'px'
  1647. };
  1648. }
  1649. };
  1650. if (el.result_area.is(':visible')) {
  1651. el.result_area.css(rePosition());
  1652. } else {
  1653. var pss = rePosition();
  1654. el.result_area.css(pss).show(1, function () {
  1655. var repss = rePosition();
  1656. if (pss.top !== repss.top || pss.left !== repss.left)
  1657. el.result_area.css(repss);
  1658. });
  1659. }
  1660. };
  1661. /**
  1662. * hide result list
  1663. * @param {Object} self
  1664. */
  1665. SelectPage.prototype.hideResults = function (self) {
  1666. if (self.prop.key_paging) {
  1667. self.scrollWindow(self, true);
  1668. self.prop.key_paging = false;
  1669. }
  1670. self.setCssFocusedInput(self);
  1671. if (self.option.autoFillResult) {
  1672. //self.selectCurrentLine(self, true);
  1673. }
  1674. self.elem.results.empty();
  1675. self.elem.result_area.hide();
  1676. self.setOpenStatus(self, false);
  1677. //unbind window scroll listen
  1678. $(window).off('scroll.SelectPage');
  1679. self.abortAjax(self);
  1680. self.setButtonAttrDefault();
  1681. };
  1682. /**
  1683. * set plugin to disabled / enabled
  1684. * @param self
  1685. * @param disabled
  1686. */
  1687. SelectPage.prototype.disabled = function (self, disabled) {
  1688. var p = self.option, el = self.elem;
  1689. if ($.type(disabled) === 'undefined')
  1690. return el.combo_input.prop('disabled');
  1691. if ($.type(disabled) === 'boolean') {
  1692. el.combo_input.prop('disabled', disabled);
  1693. if (disabled)
  1694. el.container.addClass(self.css_class.disabled);
  1695. else
  1696. el.container.removeClass(self.css_class.disabled);
  1697. }
  1698. };
  1699. /**
  1700. * Go fist page
  1701. * @param {Object} self
  1702. */
  1703. SelectPage.prototype.firstPage = function (self) {
  1704. if (self.prop.current_page > 1) {
  1705. self.prop.current_page = 1;
  1706. self.prop.page_move = true;
  1707. self.suggest(self);
  1708. }
  1709. };
  1710. /**
  1711. * Go previous page
  1712. * @param {Object} self
  1713. */
  1714. SelectPage.prototype.prevPage = function (self) {
  1715. if (self.prop.current_page > 1) {
  1716. self.prop.current_page--;
  1717. self.prop.page_move = true;
  1718. self.suggest(self);
  1719. }
  1720. };
  1721. /**
  1722. * Go next page
  1723. * @param {Object} self
  1724. */
  1725. SelectPage.prototype.nextPage = function (self) {
  1726. if (self.prop.current_page < self.prop.max_page) {
  1727. self.prop.current_page++;
  1728. self.prop.page_move = true;
  1729. self.suggest(self);
  1730. }
  1731. };
  1732. /**
  1733. * Go last page
  1734. * @param {Object} self
  1735. */
  1736. SelectPage.prototype.lastPage = function (self) {
  1737. if (self.prop.current_page < self.prop.max_page) {
  1738. self.prop.current_page = self.prop.max_page;
  1739. self.prop.page_move = true;
  1740. self.suggest(self);
  1741. }
  1742. };
  1743. /**
  1744. * do something after select/unSelect action
  1745. * @param {Object} self
  1746. */
  1747. SelectPage.prototype.afterAction = function (self) {
  1748. self.inputResize(self);
  1749. self.elem.combo_input.change();
  1750. self.setCssFocusedInput(self);
  1751. if (self.prop.init_set)
  1752. return;
  1753. if (self.option.multiple) {
  1754. if (self.option.selectToCloseList) {
  1755. self.hideResults(self);
  1756. self.elem.combo_input.blur();
  1757. } else {
  1758. self.suggest(self);
  1759. self.elem.combo_input.focus();
  1760. }
  1761. } else {
  1762. self.hideResults(self);
  1763. self.elem.combo_input.blur();
  1764. }
  1765. };
  1766. /**
  1767. * Select current list item
  1768. * @param {Object} self
  1769. * @param {boolean} is_enter_key
  1770. */
  1771. SelectPage.prototype.selectCurrentLine = function (self, is_enter_key) {
  1772. self.scrollWindow(self, true);
  1773. var p = self.option, current = self.getCurrentLine(self);
  1774. if (current) {
  1775. if (!p.multiple) {
  1776. self.elem.combo_input.val(current.text());
  1777. self.elem.hidden.val(current.attr('pkey'));
  1778. } else {
  1779. //build tags in multiple selection mode
  1780. self.elem.combo_input.val('');
  1781. var item = {text: current.text(), value: current.attr('pkey')};
  1782. if (!self.isAlreadySelected(self, item)) {
  1783. self.addNewTag(self, item);
  1784. self.tagValuesSet(self);
  1785. }
  1786. }
  1787. if (p.selectOnly)
  1788. self.setButtonAttrDefault();
  1789. //Select item callback
  1790. if (p.eSelect && $.isFunction(p.eSelect))
  1791. p.eSelect(current.data('dataObj'), self);
  1792. self.prop.prev_value = self.elem.combo_input.val();
  1793. self.prop.selected_text = self.elem.combo_input.val();
  1794. self.putClearButton();
  1795. }
  1796. self.afterAction(self);
  1797. };
  1798. /**
  1799. * Show clear button when item selected in single selection mode
  1800. */
  1801. SelectPage.prototype.putClearButton = function () {
  1802. if (!this.option.multiple && !this.elem.combo_input.prop('disabled'))
  1803. this.elem.container.append(this.elem.clear_btn);
  1804. };
  1805. /**
  1806. * Select all list item
  1807. * @param {Object} self
  1808. */
  1809. SelectPage.prototype.selectAllLine = function (self) {
  1810. var p = self.option, jsonarr = new Array();
  1811. self.elem.results.find('li').each(function (i, row) {
  1812. var $row = $(row);
  1813. var item = {text: $row.text(), value: $row.attr('pkey')};
  1814. if (!self.isAlreadySelected(self, item)) {
  1815. self.addNewTag(self, item);
  1816. self.tagValuesSet(self);
  1817. }
  1818. jsonarr.push($row.data('dataObj'));
  1819. //limited max selected items
  1820. if ($.type(p.maxSelectLimit) === 'number' &&
  1821. p.maxSelectLimit > 0 &&
  1822. p.maxSelectLimit === self.elem.element_box.find('li.selected_tag').size()) {
  1823. return false;
  1824. }
  1825. });
  1826. if (p.eSelect && $.isFunction(p.eSelect))
  1827. p.eSelect(jsonarr, self);
  1828. self.afterAction(self);
  1829. };
  1830. /**
  1831. * Cancel select all item in current page
  1832. * @param {Object} self
  1833. */
  1834. SelectPage.prototype.unSelectAllLine = function (self) {
  1835. var p = self.option, size = self.elem.results.find('li').size();
  1836. self.elem.results.find('li').each(function (i, row) {
  1837. var key = $(row).attr('pkey');
  1838. var tag = self.elem.element_box.find('li.selected_tag[itemvalue="' + key + '"]');
  1839. self.removeTag(self, tag);
  1840. });
  1841. self.afterAction(self);
  1842. if (p.eTagRemove && $.isFunction(p.eTagRemove))
  1843. p.eTagRemove(size, self);
  1844. };
  1845. /**
  1846. * Clear all selected items
  1847. * @param {Object} self
  1848. */
  1849. SelectPage.prototype.clearAll = function (self) {
  1850. var p = self.option, size = 0;
  1851. if (p.multiple) {
  1852. size = self.elem.element_box.find('li.selected_tag').size();
  1853. self.elem.element_box.find('li.selected_tag').remove();
  1854. }
  1855. self.reset(self);
  1856. self.afterAction(self);
  1857. if (!p.multiple)
  1858. self.elem.clear_btn.remove();
  1859. if (p.multiple) {
  1860. if (p.eTagRemove && $.isFunction(p.eTagRemove))
  1861. p.eTagRemove(size, self);
  1862. }
  1863. };
  1864. /**
  1865. * reset
  1866. */
  1867. SelectPage.prototype.reset = function (self) {
  1868. self.elem.combo_input.val('');
  1869. self.elem.hidden.val('');
  1870. self.prop.prev_value = '';
  1871. self.prop.selected_text = '';
  1872. self.prop.current_page = 1;
  1873. };
  1874. /**
  1875. * Get current highlight item
  1876. * @param {Object} self
  1877. */
  1878. SelectPage.prototype.getCurrentLine = function (self) {
  1879. if (self.elem.result_area.is(':hidden'))
  1880. return false;
  1881. var obj = self.elem.results.find('li.' + self.css_class.select);
  1882. if (obj.size())
  1883. return obj;
  1884. else
  1885. return false;
  1886. };
  1887. /**
  1888. * Check the result item is already selected or not
  1889. * @param {Object} self
  1890. * @param {Object} item - item info
  1891. */
  1892. SelectPage.prototype.isAlreadySelected = function (self, item) {
  1893. var isExist = false;
  1894. if (item.value) {
  1895. var keys = self.elem.hidden.val();
  1896. if (keys) {
  1897. var karr = keys.split(',');
  1898. if (karr && karr.length && $.inArray(item.value, karr) != -1)
  1899. isExist = true;
  1900. }
  1901. }
  1902. return isExist;
  1903. };
  1904. /**
  1905. * Add a new tag in multiple selection mode
  1906. * @param {Object} self
  1907. * @param {Object} item
  1908. */
  1909. SelectPage.prototype.addNewTag = function (self, item) {
  1910. if (!self.option.multiple || !item)
  1911. return;
  1912. var tmp = self.template.tag.content, tag;
  1913. tmp = tmp.replace(self.template.tag.textKey, item.text);
  1914. tmp = tmp.replace(self.template.tag.valueKey, item.value);
  1915. tag = $(tmp);
  1916. if (self.elem.combo_input.prop('disabled'))
  1917. tag.find('span.tag_close').hide();
  1918. self.elem.combo_input.closest('li').before(tag);
  1919. };
  1920. /**
  1921. * Remove a tag in multiple selection mode
  1922. * @param {Object} self
  1923. * @param {Object} item
  1924. */
  1925. SelectPage.prototype.removeTag = function (self, item) {
  1926. var key = $(item).attr('itemvalue');
  1927. var keys = self.elem.hidden.val();
  1928. if ($.type(key) != 'undefined' && keys) {
  1929. var keyarr = keys.split(','),
  1930. index = $.inArray(key.toString(), keyarr);
  1931. if (index != -1) {
  1932. keyarr.splice(index, 1);
  1933. self.elem.hidden.val(keyarr.toString());
  1934. }
  1935. }
  1936. $(item).remove();
  1937. self.afterAction(self);
  1938. };
  1939. /**
  1940. * Selected item value(keyField) put in to hidden element
  1941. * @param {Object} self
  1942. */
  1943. SelectPage.prototype.tagValuesSet = function (self) {
  1944. if (!self.option.multiple)
  1945. return;
  1946. var tags = self.elem.element_box.find('li.selected_tag');
  1947. if (tags && tags.size()) {
  1948. var result = new Array();
  1949. $.each(tags, function (i, li) {
  1950. var v = $(li).attr('itemvalue');
  1951. if ($.type(v) !== 'undefined')
  1952. result.push(v);
  1953. });
  1954. if (result.length) {
  1955. self.elem.hidden.val(result.join(','));
  1956. }
  1957. }
  1958. };
  1959. /**
  1960. * auto resize input element width in multiple select mode
  1961. * @param {Object} self
  1962. */
  1963. SelectPage.prototype.inputResize = function (self) {
  1964. if (!self.option.multiple)
  1965. return;
  1966. var width = '',
  1967. inputLi = self.elem.combo_input.closest('li');
  1968. var setDefaultSize = function (self, inputLi) {
  1969. inputLi.removeClass('full_width');
  1970. var minimumWidth = self.elem.combo_input.val().length + 1,
  1971. width = (minimumWidth * 0.75) + 'em';
  1972. self.elem.combo_input.css('width', width).removeAttr('placeholder');
  1973. };
  1974. if (self.elem.element_box.find('li.selected_tag').size() === 0) {
  1975. if (self.elem.combo_input.attr('placeholder_bak')) {
  1976. if (!inputLi.hasClass('full_width'))
  1977. inputLi.addClass('full_width');
  1978. self.elem.combo_input.attr('placeholder', self.elem.combo_input.attr('placeholder_bak')).removeAttr('style');
  1979. } else
  1980. setDefaultSize(self, inputLi);
  1981. } else
  1982. setDefaultSize(self, inputLi);
  1983. };
  1984. /**
  1985. * Move to next line
  1986. * @param {Object} self
  1987. */
  1988. SelectPage.prototype.nextLine = function (self) {
  1989. var obj = self.getCurrentLine(self), idx;
  1990. if (!obj)
  1991. idx = -1;
  1992. else {
  1993. idx = self.elem.results.children('li').index(obj);
  1994. obj.removeClass(self.css_class.select);
  1995. }
  1996. idx++;
  1997. if (idx < self.elem.results.children('li').length) {
  1998. var next = self.elem.results.children('li').eq(idx);
  1999. next.addClass(self.css_class.select);
  2000. self.setCssFocusedResults(self);
  2001. } else
  2002. self.setCssFocusedInput(self);
  2003. self.scrollWindow(self, false);
  2004. };
  2005. /**
  2006. * Move to previous line
  2007. * @param {Object} self
  2008. */
  2009. SelectPage.prototype.prevLine = function (self) {
  2010. var obj = self.getCurrentLine(self), idx;
  2011. if (!obj)
  2012. idx = self.elem.results.children('li').length;
  2013. else {
  2014. idx = self.elem.results.children('li').index(obj);
  2015. obj.removeClass(self.css_class.select);
  2016. }
  2017. idx--;
  2018. if (idx > -1) {
  2019. var prev = self.elem.results.children('li').eq(idx);
  2020. prev.addClass(self.css_class.select);
  2021. self.setCssFocusedResults(self);
  2022. } else
  2023. self.setCssFocusedInput(self);
  2024. self.scrollWindow(self, false);
  2025. };
  2026. /**
  2027. * SelectPage plugin definition
  2028. * @global
  2029. * @param option {Object} init plugin option
  2030. */
  2031. function Plugin(option) {
  2032. return this.each(function () {
  2033. var $this = $(this),
  2034. data = $this.data(SelectPage.dataKey),
  2035. params = $.extend({}, defaults, $this.data(), data && data.option, typeof option === 'object' && option);
  2036. if (!data)
  2037. $this.data(SelectPage.dataKey, (data = new SelectPage(this, params)));
  2038. });
  2039. }
  2040. /**
  2041. * Get plugin object
  2042. * @param {object} obj
  2043. * @returns
  2044. */
  2045. function getPlugin(obj) {
  2046. return $(obj).closest('div.sp_container').find('input.sp_input');
  2047. }
  2048. /**
  2049. * Clear all selected item
  2050. */
  2051. function ClearSelected() {
  2052. return this.each(function () {
  2053. var $this = getPlugin(this),
  2054. data = $this.data(SelectPage.dataKey);
  2055. if (data) {
  2056. data.prop.init_set = true;
  2057. data.clearAll(data);
  2058. data.prop.init_set = false;
  2059. }
  2060. });
  2061. }
  2062. /**
  2063. * Refresh result list
  2064. * use case:
  2065. * 1.use $(obj).val('xxx') to modify selectpage selected item key
  2066. * 2.refresh selected item show content/tag text
  2067. */
  2068. function SelectedRefresh() {
  2069. return this.each(function () {
  2070. var $this = getPlugin(this),
  2071. data = $this.data(SelectPage.dataKey);
  2072. if (data && data.elem.hidden.val())
  2073. data.setInitRecord(true);
  2074. });
  2075. }
  2076. /**
  2077. * Modify plugin datasource, only work on json datasource mode
  2078. * @param {array} data - new datasource
  2079. * @example
  2080. * [{name:'aa',sex:1},{name:'bb',sex:0},{...}]
  2081. */
  2082. function ModifyDataSource(data) {
  2083. return this.each(function () {
  2084. if (data && $.isArray(data)) {
  2085. var $this = getPlugin(this),
  2086. plugin = $this.data(SelectPage.dataKey);
  2087. if (plugin) {
  2088. plugin.clearAll(plugin);
  2089. plugin.option.data = data;
  2090. }
  2091. }
  2092. });
  2093. }
  2094. /**
  2095. * Get plugin disabled status or Modify plugin disabled status
  2096. * @param disabled {boolean} set disabled status
  2097. */
  2098. function PluginDisabled(disabled) {
  2099. var status = false;
  2100. this.each(function () {
  2101. var $this = getPlugin(this),
  2102. plugin = $this.data(SelectPage.dataKey);
  2103. if (plugin) {
  2104. if ($.type(disabled) !== 'undefined')
  2105. plugin.disabled(plugin, disabled);
  2106. else
  2107. status = plugin.disabled(plugin);
  2108. }
  2109. });
  2110. return status;
  2111. }
  2112. /**
  2113. * Get selected item text
  2114. * @returns {string}
  2115. */
  2116. function GetInputText() {
  2117. var str = '';
  2118. this.each(function () {
  2119. var $this = getPlugin(this), data = $this.data(SelectPage.dataKey);
  2120. if (data) {
  2121. if (data.option.multiple) {
  2122. var tags = [];
  2123. data.elem.element_box.find('li.selected_tag').each(function (i, tag) {
  2124. tags.push($(tag).text());
  2125. });
  2126. str += tags.toString();
  2127. } else {
  2128. str += data.elem.combo_input.val();
  2129. }
  2130. }
  2131. });
  2132. return str;
  2133. }
  2134. var old = $.fn.selectPage;
  2135. $.fn.selectPage = Plugin;
  2136. $.fn.selectPage.Constructor = SelectPage;
  2137. $.fn.selectPageClear = ClearSelected;
  2138. $.fn.selectPageRefresh = SelectedRefresh;
  2139. $.fn.selectPageData = ModifyDataSource;
  2140. $.fn.selectPageDisabled = PluginDisabled;
  2141. $.fn.selectPageText = GetInputText;
  2142. // SelectPage no conflict
  2143. // =================
  2144. $.fn.selectPage.noConflict = function () {
  2145. $.fn.selectPage = old;
  2146. return this;
  2147. };
  2148. })(window.jQuery);