main.js 6.2 KB

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