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.attr('data-part',r.part);
  84. li.attr('data-id',r.id)
  85. $.message('success','Enregistré');
  86. });
  87. }
  88. function part_part_find(element){
  89. var li = $(element).closest('li');
  90. var label = li.find('.label').val();
  91. var brand = li.find('.brand input').val();
  92. var url = "https://www.ebay.fr/sch/i.html?_from=R40&_sacat=0&LH_BIN=1&LH_PrefLoc=2&_sop=15";
  93. if(null!=brand && brand !='') label = brand+'+'+label;
  94. url += "&_nkw="+label;
  95. window.open(url);
  96. }
  97. function part_part_add(){
  98. var tpl = $('#parts li:eq(0)').get(0).outerHTML;
  99. var data = {
  100. price : 1,
  101. url : 'http://ebay.com'
  102. }
  103. var item = $(Mustache.render(tpl,data));
  104. item.removeClass('hidden');
  105. item.css({
  106. transform : 'scale(0)',
  107. opacity : 0,
  108. });
  109. $('#parts li:eq(0)').after(item);
  110. item.css({
  111. transform : 'scale(1) rotate(0deg)',
  112. opacity : 1,
  113. });
  114. init_components(item);
  115. part_resource_part_upload(item);
  116. }
  117. function part_resource_part_upload(item){
  118. var item = $(item);
  119. var image = item.find('.part-image');
  120. image.click(function(e){
  121. e.preventDefault();
  122. e.stopPropagation();
  123. var picker = image.next('input[type="file"]');
  124. picker.trigger('click');
  125. picker.change(function(){
  126. var file = picker.get(0).files[0];
  127. var reader = new FileReader();
  128. reader.addEventListener("load", function () {
  129. image.css('background','url('+reader.result+')')
  130. .attr('data-stream',reader.result)
  131. .css('background-size','cover');
  132. }, false);
  133. reader.readAsDataURL(file);
  134. });
  135. });
  136. image.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  137. e.preventDefault();
  138. e.stopPropagation();
  139. })
  140. image.on('drop', function (e) {
  141. var droppedFiles = e.originalEvent.dataTransfer.files;
  142. var reader = new FileReader();
  143. reader.readAsDataURL(droppedFiles[0]);
  144. reader.onload = function () {
  145. image.css('background','url('+reader.result+')')
  146. .attr('data-stream',reader.result)
  147. .css('background-size','cover');
  148. };
  149. reader.onerror = function (error) {
  150. console.log('Error: ', error);
  151. };
  152. });
  153. /*
  154. var preload = $('<div class="preload progress-bar progress-bar-striped progress-bar-animated"></div>');
  155. item.find('.part-image').append(preload);
  156. item.find('.part-image').attr('id','part-image-'+$('#parts li').length());
  157. item.find('.part-image').upload({
  158. allowed : 'jpg,png,jpeg,bmp,svg',
  159. size : 0,
  160. action : 'part_part_image_upload',
  161. readonly: false,
  162. start: function(){
  163. preload.show();
  164. },
  165. success: function(response){
  166. if(response.previews.length && response.previews[0].name) {
  167. }
  168. console.log(response);
  169. preload.fadeOut();
  170. },
  171. complete: function(){
  172. preload.fadeOut();
  173. }
  174. });*/
  175. }
  176. //Suppression d'élement part
  177. function part_resource_part_delete(element){
  178. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  179. var line = $(element).closest('li');
  180. if(line.attr('data-id')==''){
  181. line.css('transform','scale(0)');
  182. setTimeout(function(){
  183. line.remove()
  184. },210);
  185. return;
  186. }
  187. $.action({
  188. action : 'part_resource_part_delete',
  189. id : line.attr('data-id')
  190. },function(r){
  191. line.css('transform','scale(0)');
  192. setTimeout(function(){
  193. line.remove()
  194. },210);
  195. });
  196. }
  197. //Suppression d'élement part
  198. function part_part_delete(element){
  199. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  200. var line = $(element).closest('li');
  201. if(line.attr('data-id')==''){
  202. line.css('transform','scale(0)');
  203. setTimeout(function(){
  204. line.remove()
  205. },210);
  206. return;
  207. }
  208. $.action({
  209. action : 'part_part_delete',
  210. id : line.attr('data-id')
  211. },function(r){
  212. line.css('transform','scale(0)');
  213. setTimeout(function(){
  214. line.remove()
  215. },210);
  216. });
  217. }