plugins.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. $.upzoneTransfert = function(e,o){
  116. var list = [];
  117. if(e.dataTransfer) {
  118. filelist = e.dataTransfer.files;
  119. } else if(e.target) {
  120. filelist = e.target.files;
  121. }
  122. if (!filelist || !filelist.length ) return;
  123. totalSize = 0;
  124. totalProgress = 0;
  125. for (var i = 0; i < filelist.length && i < 5; i++) {
  126. list.push(filelist[i]);
  127. totalSize += filelist[i].size;
  128. }
  129. if (list.length) {
  130. var nextFile = list.shift();
  131. //if (nextFile.size >= 262144) { // 256 kb
  132. var xhr = new XMLHttpRequest();
  133. xhr.open('POST', o.url);
  134. xhr.onload = function() {
  135. // result.innerHTML += this.responseText;
  136. // handleComplete(file.size);
  137. if(o.success!=null)o.success(this.responseText);
  138. };
  139. xhr.onerror = function() {
  140. // result.textContent = this.responseText;
  141. // handleComplete(file.size);
  142. console.log(this.responseText,'error');
  143. if(o.error!=null)o.error(this.responseText);
  144. };
  145. xhr.upload.onprogress = function(event) {
  146. // handleProgress(event);
  147. //console.log(event);
  148. }
  149. xhr.upload.onloadstart = function(event) {
  150. }
  151. // création de l'objet FormData
  152. var formData = new FormData();
  153. for(var k in o)
  154. formData.append(k, o[k]);
  155. formData.append('file', nextFile);
  156. xhr.send(formData);
  157. }
  158. }
  159. $.fn.extend({
  160. upzone : function (option){
  161. var defaults = {
  162. url : '',
  163. success : function(){},
  164. error : function(){}
  165. }
  166. var o = $.extend(defaults,option);
  167. return this.each(function() {
  168. var obj = $(this);
  169. obj.before('<input type="file" id="test" style="display:none">');
  170. $('#test').change(function(e) {
  171. $.upzoneTransfert(e,o);
  172. });
  173. obj.click(function(){
  174. $('#test').trigger('click');
  175. });
  176. obj.get(0).addEventListener('drop', function(event) {
  177. event.stopPropagation();
  178. event.preventDefault();
  179. $.upzoneTransfert(event,o);
  180. //var filelist = event.dataTransfer.files;
  181. }, false);
  182. obj.get(0).addEventListener('dragover', function handleDragOver(event) {
  183. event.stopPropagation();
  184. event.preventDefault();
  185. }, false);
  186. });
  187. },
  188. fill: function (option,callback){
  189. return this.each(function() {
  190. var obj = $(this);
  191. var model = null;
  192. var container = null;
  193. if(obj.prop("tagName") == 'UL'){
  194. container = obj;
  195. model = container.find('li:first-child');
  196. container.find('li:visible').remove();
  197. }else if(obj.prop("tagName") == 'TABLE'){
  198. container = obj.find('tbody');
  199. model = container.find('tr:first-child');
  200. container.find('tr:visible').remove();
  201. }else{
  202. container = obj;
  203. childName = container.children().get(0).nodeName;
  204. model = container.find(childName+':first-child');
  205. container.find(childName+':visible:not(.nofill)').remove();
  206. }
  207. var tpl = model.get(0).outerHTML;
  208. $.action(option,function(r){
  209. for(var key in r.rows){
  210. var line = $(Mustache.render(tpl,r.rows[key]));
  211. container.append(line);
  212. line.show();
  213. }
  214. if(callback!=null)callback(r);
  215. });
  216. });
  217. },
  218. enter: function (option){
  219. return this.each(function() {
  220. var obj = $(this);
  221. obj.keydown(function(event){
  222. if(event.keyCode == 13){
  223. option();
  224. return false;
  225. }
  226. });
  227. });
  228. },
  229. date: function (){
  230. return this.each(function() {
  231. var obj = $(this);
  232. obj.datepicker({
  233. dateFormat: "dd/mm/yy",
  234. dayNames: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
  235. dayNamesMin: ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
  236. dayNamesShort: ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"],
  237. monthNames: ["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Décembre"],
  238. firstDay: 1,
  239. changeMonth: true,
  240. yearRange: "-100:+0",
  241. changeYear: true,
  242. onSelect: function(dateText, inst) { $( this).trigger( "blur" ); }
  243. });
  244. });
  245. }
  246. });