main.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //CHARGEMENT DE LA PAGE
  2. function init_plugin_part(){
  3. switch($.urlParam('page')){
  4. case 'list.part':
  5. part_part_search();
  6. break;
  7. default:
  8. break;
  9. }
  10. part_part_search();
  11. $('#parts').sortable_table({
  12. onSort : part_part_search
  13. });
  14. }
  15. //Enregistrement des configurations
  16. function part_setting_save(){
  17. $.action({
  18. action : 'part_setting_save',
  19. fields : $('#part-setting-form').toJson()
  20. },function(){ $.message('info','Configuration enregistrée'); });
  21. }
  22. /** PART **/
  23. //Récuperation d'une liste de part dans le tableau #parts
  24. function part_resource_part_search(callback){
  25. $('#parts').fill({
  26. action:'part_resource_part_search',
  27. resource : $('#sketch-editor').attr('data-id'),
  28. showing : function(li,i){
  29. li.css('transform','scale(0)').removeClass('hidden');
  30. setTimeout(function(){
  31. li.css('transform','scale(1)');
  32. },i*200);
  33. }
  34. },function(){
  35. $('#parts li:not(:eq(0))').each(function(i,li){
  36. part_resource_part_upload(li);
  37. var div = $('[data-stream]',li);
  38. if($(div).attr('data-stream') =='') return;
  39. $(div).css('background','url(data:'+$(div).attr('data-stream')+')')
  40. .css('background-size','cover');
  41. });
  42. if(callback!=null) callback();
  43. });
  44. }
  45. //Récuperation d'une liste de part dans le tableau #parts
  46. function part_part_search(callback){
  47. $('#parts').fill({
  48. action:'part_part_search',
  49. resource : $('#sketch-editor').attr('data-id'),
  50. },function(){
  51. $('#parts li:not(:eq(0))').each(function(i,li){
  52. part_resource_part_upload(li);
  53. var div = $('[data-stream]',li);
  54. if($(div).attr('data-stream') =='') return;
  55. $(div).css('background','url(data:'+$(div).attr('data-stream')+')')
  56. .css('background-size','cover');
  57. });
  58. $('#parts li:not(:eq(0))').css('transform','scale(0)').removeClass('hidden')
  59. $('#parts li:not(:eq(0))').each(function(i,li){
  60. var li = $(li);
  61. setTimeout(function(){
  62. li.css('transform','scale(1)');
  63. },i*200);
  64. });
  65. if(callback!=null) callback();
  66. });
  67. }
  68. //Ajout ou modification d'élément part
  69. function part_part_save(element){
  70. var li = $(element).closest('li');
  71. var data = {
  72. action : 'part_part_save',
  73. resource : $('#sketch-editor').attr('data-id'),
  74. part : li.attr('data-part'),
  75. id : li.attr('data-id'),
  76. label : li.find('.label').val(),
  77. brand : li.find('.brand input').val(),
  78. price : li.find('.price input').val(),
  79. url : li.find('.url').val(),
  80. picture : li.find('.part-image').attr('data-stream')
  81. }
  82. $.action(data,function(r){
  83. li.removeClass('edition');
  84. li.attr('data-part',r.part);
  85. li.attr('data-id',r.id)
  86. $.message('success','Enregistré');
  87. });
  88. }
  89. function part_part_find(element){
  90. var li = $(element).closest('li');
  91. var label = li.find('.label').val();
  92. var brand = li.find('.brand input').val();
  93. var url = "https://www.ebay.fr/sch/i.html?_from=R40&_sacat=0&LH_BIN=1&LH_PrefLoc=2&_sop=15";
  94. if(null!=brand && brand !='') label = brand+'+'+label;
  95. url += "&_nkw="+label;
  96. window.open(url);
  97. }
  98. function part_part_add(){
  99. var tpl = $('#parts li:eq(0)').get(0).outerHTML;
  100. var data = {
  101. price : 1,
  102. url : 'http://ebay.com'
  103. }
  104. var item = $(Mustache.render(tpl,data));
  105. item.removeClass('hidden').addClass('edition');
  106. item.css({
  107. transform : 'scale(0)',
  108. opacity : 0,
  109. });
  110. $('#parts li:eq(0)').after(item);
  111. item.css({
  112. transform : 'scale(1) rotate(0deg)',
  113. opacity : 1,
  114. });
  115. init_components(item);
  116. part_resource_part_upload(item);
  117. }
  118. function part_resource_part_upload(item){
  119. var item = $(item);
  120. var image = item.find('.part-image');
  121. image.click(function(e){
  122. e.preventDefault();
  123. e.stopPropagation();
  124. var picker = image.next('input[type="file"]');
  125. picker.trigger('click');
  126. picker.change(function(){
  127. var file = picker.get(0).files[0];
  128. var reader = new FileReader();
  129. reader.addEventListener("load", function () {
  130. image.css('background','url('+reader.result+')')
  131. .attr('data-stream',reader.result)
  132. .css('background-size','cover');
  133. }, false);
  134. reader.readAsDataURL(file);
  135. });
  136. });
  137. image.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  138. e.preventDefault();
  139. e.stopPropagation();
  140. })
  141. image.on('drop', function (e) {
  142. var droppedFiles = e.originalEvent.dataTransfer.files;
  143. var reader = new FileReader();
  144. reader.readAsDataURL(droppedFiles[0]);
  145. reader.onload = function () {
  146. image.css('background','url('+reader.result+')')
  147. .attr('data-stream',reader.result)
  148. .css('background-size','cover');
  149. };
  150. reader.onerror = function (error) {
  151. console.log('Error: ', error);
  152. };
  153. });
  154. /*
  155. var preload = $('<div class="preload progress-bar progress-bar-striped progress-bar-animated"></div>');
  156. item.find('.part-image').append(preload);
  157. item.find('.part-image').attr('id','part-image-'+$('#parts li').length());
  158. item.find('.part-image').upload({
  159. allowed : 'jpg,png,jpeg,bmp,svg',
  160. size : 0,
  161. action : 'part_part_image_upload',
  162. readonly: false,
  163. start: function(){
  164. preload.show();
  165. },
  166. success: function(response){
  167. if(response.previews.length && response.previews[0].name) {
  168. }
  169. console.log(response);
  170. preload.fadeOut();
  171. },
  172. complete: function(){
  173. preload.fadeOut();
  174. }
  175. });*/
  176. }
  177. //Suppression d'élement part
  178. function part_resource_part_delete(element){
  179. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  180. var line = $(element).closest('li');
  181. if(line.attr('data-id')==''){
  182. line.css('transform','scale(0)');
  183. setTimeout(function(){
  184. line.remove()
  185. },210);
  186. return;
  187. }
  188. $.action({
  189. action : 'part_resource_part_delete',
  190. id : line.attr('data-id')
  191. },function(r){
  192. line.css('transform','scale(0)');
  193. setTimeout(function(){
  194. line.remove()
  195. },210);
  196. });
  197. }
  198. //Suppression d'élement part
  199. function part_part_delete(element){
  200. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  201. var line = $(element).closest('li');
  202. if(line.attr('data-id')==''){
  203. line.css('transform','scale(0)');
  204. setTimeout(function(){
  205. line.remove()
  206. },210);
  207. return;
  208. }
  209. $.action({
  210. action : 'part_part_delete',
  211. id : line.attr('data-id')
  212. },function(r){
  213. line.css('transform','scale(0)');
  214. setTimeout(function(){
  215. line.remove()
  216. },210);
  217. });
  218. }