1/** 2 * Init datepicker for all date fields 3 */ 4jQuery(function () { 5 jQuery('.data_type_dt input').datepicker({ 6 dateFormat: "yy-mm-dd", 7 changeMonth: true, 8 changeYear: true 9 }); 10}); 11 12/** 13 * Init autocompletion for all page alias fields 14 * 15 * @author Adrian Lang <lang@cosmocode.de> 16 * @author Gerrit Uitslag <klapinklapin@gmail.com> 17 */ 18jQuery(function () { 19 /** 20 * Returns aliastype of field 21 * 22 * @param {jQuery} $input 23 * @return {String} aliastype of the input 24 */ 25 function getAliastype($input) { 26 var classes = $input.parent().attr('class').split(' '), 27 multi = false, 28 aliastype = 'data_type_page'; 29 30 jQuery.each(classes, function (i, cls) { 31 //skip base type 32 if (cls == 'data_type_page' || cls == 'data_type_pages') { 33 multi = cls.substr(cls.length - 1, 1) == 's'; 34 return true; 35 } 36 //only data types, no other classes 37 if (cls.substr(0, 10) == 'data_type_') { 38 aliastype = cls; 39 } 40 }); 41 //return singular aliastype 42 return (multi ? aliastype.substr(0, aliastype.length - 1) : aliastype); 43 } 44 45 /** 46 * Ajax request for user suggestions 47 * 48 * @param {Object} request object, with single 'term' property 49 * @param {Function} response callback, argument: the data to suggest to the user. 50 * @param {Function} getTerm callback, argument: the request Object, returns: search term 51 * @param aliastype 52 */ 53 function ajaxsource(request, response, getTerm, aliastype) { 54 jQuery.getJSON( 55 DOKU_BASE + 'lib/exe/ajax.php', { 56 call: 'data_page', 57 aliastype: aliastype, 58 search: getTerm(request) 59 }, function (data) { 60 response(jQuery.map(data, function (name, id) { 61 return { 62 label: name + ' (' + id + ')', 63 value: id 64 } 65 })) 66 } 67 ); 68 } 69 70 function split(val) { 71 return val.split(/,\s*/); 72 } 73 74 function extractLast(term) { 75 return split(term).pop(); 76 } 77 78 79 /** 80 * pick one user 81 */ 82 jQuery(".data_type_page input").autocomplete({ 83 source: function (request, response) { 84 ajaxsource( 85 request, 86 response, 87 function (req) { 88 return req.term; 89 }, 90 getAliastype(this.element) 91 ); 92 } 93 }); 94 95 /** 96 * pick one or more users 97 */ 98 jQuery(".data_type_pages input") 99 // don't navigate away from the field on tab when selecting an item 100 .bind("keydown", function (event) { 101 if (event.keyCode === jQuery.ui.keyCode.TAB && 102 jQuery(this).data("ui-autocomplete").menu.active) { 103 event.preventDefault(); 104 } 105 }) 106 .autocomplete({ 107 minLength: 0, 108 source: function (request, response) { 109 ajaxsource( 110 request, 111 response, 112 function (req) { 113 return extractLast(req.term); 114 }, 115 getAliastype(this.element) 116 ); 117 }, 118 search: function () { 119 // custom minLength 120 var term = extractLast(this.value); 121 if (term.length < 2) { 122 return false; 123 } 124 return true; 125 }, 126 focus: function () { 127 // prevent value inserted on focus 128 return false; 129 }, 130 select: function (event, ui) { 131 var terms = split(this.value); 132 // remove the current input 133 terms.pop(); 134 // add the selected item 135 terms.push(ui.item.value); 136 // add placeholder to get the comma-and-space at the end 137 terms.push(""); 138 this.value = terms.join(", "); 139 return false; 140 } 141 }); 142}); 143