plugins.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Avoid `console` errors in browsers that lack a console.
  2. (function() {
  3. var method;
  4. var noop = function () {};
  5. var methods = [
  6. 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
  7. 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
  8. 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
  9. 'timeline', 'timelineEnd', 'timeStamp', 'trace', 'warn'
  10. ];
  11. var length = methods.length;
  12. var console = (window.console = window.console || {});
  13. while (length--) {
  14. method = methods[length];
  15. // Only stub undefined methods.
  16. if (!console[method]) {
  17. console[method] = noop;
  18. }
  19. }
  20. }());
  21. // Place any jQuery/helper plugins in here.
  22. $.page = function(element){
  23. var path = window.location.href.split('/') ;
  24. path = path[path.length-1];
  25. path = path.split('.php');
  26. path = path[0];
  27. return path;
  28. }
  29. $.getForm= function(element){
  30. var o = {};
  31. var obj = $(element);
  32. for(var key in obj.data()){
  33. if(key!="action" && key != "id") continue;
  34. o[key] = obj.attr('data-'+key);
  35. }
  36. $('input,select,textarea',obj).each(function(i,element){
  37. if(element.id!=null && element.id!=""){
  38. if($(element).attr("type")=='checkbox' || $(element).attr("type")=='radio'){
  39. o[element.id] = ($(element).is(':checked')?1:0);
  40. }else{
  41. o[element.id] = $(element).val().replace("'","’");
  42. }
  43. }
  44. });
  45. return o;
  46. }
  47. $.setForm= function(element,data){
  48. var o = {};
  49. var obj = $(element);
  50. $('input,select,textarea',obj).each(function(i,element){
  51. if(element.id!=null && element.id!=""){
  52. if(data[element.id]!=null){
  53. if($(element).attr("type")=='checkbox' || $(element).attr("type")=='radio'){
  54. $(element).prop("checked",data[element.id]==1 || data[element.id]=='true' ?true:false);
  55. }else{
  56. $(element).val(data[element.id]);
  57. }
  58. }
  59. }
  60. });
  61. return o;
  62. }
  63. $.action = function(data,success,error) {
  64. $.ajax({
  65. dataType : 'json',
  66. method : 'POST',
  67. url : 'action.php',
  68. data : data,
  69. success: function(response){
  70. if(response.errors == null ) response.errors.push('Erreur indefinie, merci de contacter un administrateur');
  71. if(response.errors.length ==0 ){
  72. if(success!=null)success(response);
  73. }else{
  74. alert('ERREUR : '+"\n"+response.errors.join("\n"));
  75. if(error!=null) error(response);
  76. }
  77. },
  78. error : function(){
  79. alert('Erreur indefinie, merci de contacter un administrateur');
  80. }
  81. });
  82. }
  83. $.hashData = function(name){
  84. var page = window.location.hash.substring(1);
  85. page += "&"+window.location.search.substring(1);
  86. data = {};
  87. if(page!='' && page!= null){
  88. options = page.split('&');
  89. var data = {};
  90. for(var key in options){
  91. infos = options[key].split('=');
  92. data[infos[0]] = infos[1];
  93. }
  94. }
  95. if(name == null) return data;
  96. if(typeof name === "object"){
  97. data = name;
  98. hashstring = '';
  99. for(var key in data)
  100. hashstring+= "&"+key+"="+data[key];
  101. hashstring = hashstring.substring(1);
  102. window.location.hash = hashstring;
  103. return;
  104. }
  105. return typeof data[name] == "undefined" ? '':data[name];
  106. }
  107. $.urlParam = function(name){
  108. var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
  109. if (results==null){
  110. return null;
  111. }else{
  112. return results[1] || 0;
  113. }
  114. }
  115. $.fn.extend({
  116. fill: function (option,callback){
  117. return this.each(function() {
  118. var obj = $(this);
  119. var model = null;
  120. var container = null;
  121. if(obj.prop("tagName") == 'UL'){
  122. container = obj;
  123. model = container.find('li:first-child');
  124. container.find('li:visible').remove();
  125. }else if(obj.prop("tagName") == 'TABLE'){
  126. container = obj.find('tbody');
  127. model = container.find('tr:first-child');
  128. container.find('tr:visible').remove();
  129. }else{
  130. container = obj;
  131. childName = container.children().get(0).nodeName;
  132. model = container.find(childName+':first-child');
  133. container.find(childName+':visible:not(.nofill)').remove();
  134. }
  135. var tpl = model.get(0).outerHTML;
  136. $.action(option,function(r){
  137. for(var key in r.rows){
  138. var line = $(Mustache.render(tpl,r.rows[key]));
  139. container.append(line);
  140. line.show();
  141. }
  142. if(callback!=null)callback(r);
  143. });
  144. });
  145. },
  146. enter: function (option){
  147. return this.each(function() {
  148. var obj = $(this);
  149. obj.keydown(function(event){
  150. if(event.keyCode == 13){
  151. option();
  152. return false;
  153. }
  154. });
  155. });
  156. },
  157. date: function (){
  158. return this.each(function() {
  159. var obj = $(this);
  160. obj.datepicker({
  161. dateFormat: "dd/mm/yy",
  162. dayNames: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
  163. dayNamesMin: ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
  164. dayNamesShort: ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"],
  165. monthNames: ["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Décembre"],
  166. firstDay: 1,
  167. changeMonth: true,
  168. yearRange: "-100:+0",
  169. changeYear: true,
  170. onSelect: function(dateText, inst) { $( this).trigger( "blur" ); }
  171. });
  172. });
  173. }
  174. });