main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. //CHARGEMENT DE LA PAGE
  2. function init_plugin_hackpoint(){
  3. switch($.urlParam('page')){
  4. default:
  5. break;
  6. }
  7. $('#parts').sortable_table({
  8. onSort : hackpoint_part_search
  9. });
  10. $('#sketchs').sortable_table({
  11. onSort : hackpoint_sketch_search
  12. });
  13. hackpoint_resource_search();
  14. }
  15. //Enregistrement des configurations
  16. function hackpoint_setting_save(){
  17. $.action({
  18. action : 'hackpoint_setting_save',
  19. fields : $('#hackpoint-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 hackpoint_part_search(callback){
  25. $('#parts').fill({
  26. action:'hackpoint_part_search',
  27. resource : $('#sketch-editor').attr('data-id'),
  28. showItems : false
  29. },function(){
  30. $('#parts li:not(:eq(0))').each(function(i,li){
  31. hackpoint_resource_part_upload(li);
  32. var div = $('[data-stream]',li);
  33. if($(div).attr('data-stream') =='') return;
  34. $(div).css('background','url(data:'+$(div).attr('data-stream')+')')
  35. .css('background-size','cover');
  36. });
  37. $('#parts li:not(:eq(0))').css('transform','scale(0)').removeClass('hidden')
  38. $('#parts li:not(:eq(0))').each(function(i,li){
  39. var li = $(li);
  40. setTimeout(function(){
  41. li.css('transform','scale(1)');
  42. },i*200);
  43. });
  44. if(callback!=null) callback();
  45. });
  46. }
  47. //Ajout ou modification d'élément part
  48. function hackpoint_part_save(element){
  49. var li = $(element).closest('li');
  50. var data = {
  51. action : 'hackpoint_part_save',
  52. resource : $('#sketch-editor').attr('data-id'),
  53. part : li.attr('data-part'),
  54. id : li.attr('data-id'),
  55. label : li.find('.label').val(),
  56. brand : li.find('.brand input').val(),
  57. price : li.find('.price input').val(),
  58. url : li.find('.url').val(),
  59. picture : li.find('.part-image').attr('data-stream')
  60. }
  61. $.action(data,function(r){
  62. $.message('success','Enregistré');
  63. });
  64. }
  65. function hackpoint_part_find(element){
  66. var li = $(element).closest('li');
  67. var label = li.find('.label').val();
  68. var brand = li.find('.brand input').val();
  69. var url = "https://www.ebay.fr/sch/i.html?_from=R40&_sacat=0&LH_BIN=1&LH_PrefLoc=2&_sop=15";
  70. if(null!=brand && brand !='') label = brand+'+'+label;
  71. url += "&_nkw="+label;
  72. window.open(url);
  73. }
  74. function hackpoint_part_add(){
  75. var tpl = $('#parts li:eq(0)').get(0).outerHTML;
  76. var data = {
  77. price : 1,
  78. url : 'http://ebay.com'
  79. }
  80. var item = $(Mustache.render(tpl,data));
  81. item.removeClass('hidden');
  82. item.css({
  83. transform : 'scale(0)',
  84. opacity : 0,
  85. });
  86. $('#parts li:eq(0)').after(item);
  87. item.css({
  88. transform : 'scale(1) rotate(0deg)',
  89. opacity : 1,
  90. });
  91. init_components(item);
  92. hackpoint_resource_part_upload(item);
  93. }
  94. function hackpoint_resource_part_upload(item){
  95. var item = $(item);
  96. var image = item.find('.part-image');
  97. image.click(function(e){
  98. e.preventDefault();
  99. e.stopPropagation();
  100. var picker = image.next('input[type="file"]');
  101. picker.trigger('click');
  102. picker.change(function(){
  103. var file = picker.get(0).files[0];
  104. var reader = new FileReader();
  105. reader.addEventListener("load", function () {
  106. image.css('background','url('+reader.result+')')
  107. .attr('data-stream',reader.result)
  108. .css('background-size','cover');
  109. }, false);
  110. reader.readAsDataURL(file);
  111. });
  112. });
  113. image.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  114. e.preventDefault();
  115. e.stopPropagation();
  116. })
  117. image.on('drop', function (e) {
  118. var droppedFiles = e.originalEvent.dataTransfer.files;
  119. var reader = new FileReader();
  120. reader.readAsDataURL(droppedFiles[0]);
  121. reader.onload = function () {
  122. image.css('background','url('+reader.result+')')
  123. .attr('data-stream',reader.result)
  124. .css('background-size','cover');
  125. };
  126. reader.onerror = function (error) {
  127. console.log('Error: ', error);
  128. };
  129. });
  130. /*
  131. var preload = $('<div class="preload progress-bar progress-bar-striped progress-bar-animated"></div>');
  132. item.find('.part-image').append(preload);
  133. item.find('.part-image').attr('id','part-image-'+$('#parts li').length());
  134. item.find('.part-image').upload({
  135. allowed : 'jpg,png,jpeg,bmp,svg',
  136. size : 0,
  137. action : 'hackpoint_part_image_upload',
  138. readonly: false,
  139. start: function(){
  140. preload.show();
  141. },
  142. success: function(response){
  143. if(response.previews.length && response.previews[0].name) {
  144. }
  145. console.log(response);
  146. preload.fadeOut();
  147. },
  148. complete: function(){
  149. preload.fadeOut();
  150. }
  151. });*/
  152. }
  153. //Suppression d'élement part
  154. function hackpoint_resource_part_delete(element){
  155. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  156. var line = $(element).closest('li');
  157. if(line.attr('data-id')==''){
  158. line.css('transform','scale(0)');
  159. setTimeout(function(){
  160. line.remove()
  161. },210);
  162. return;
  163. }
  164. $.action({
  165. action : 'hackpoint_resource_part_delete',
  166. id : line.attr('data-id')
  167. },function(r){
  168. line.css('transform','scale(0)');
  169. setTimeout(function(){
  170. line.remove()
  171. },210);
  172. });
  173. }
  174. //Suppression d'élement part
  175. function hackpoint_part_delete(element){
  176. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  177. var line = $(element).closest('li');
  178. if(line.attr('data-id')==''){
  179. line.css('transform','scale(0)');
  180. setTimeout(function(){
  181. line.remove()
  182. },210);
  183. return;
  184. }
  185. $.action({
  186. action : 'hackpoint_part_delete',
  187. id : line.attr('data-id')
  188. },function(r){
  189. line.css('transform','scale(0)');
  190. setTimeout(function(){
  191. line.remove()
  192. },210);
  193. });
  194. }
  195. /** SKETCH **/
  196. //Récuperation d'une liste de sketch dans le tableau #sketchs
  197. function hackpoint_sketch_search(callback){
  198. $('#sketchs').fill({
  199. action:'hackpoint_sketch_search',
  200. filters : $('#filters').filters(),
  201. sort : $('#sketchs').sortable_table('get')
  202. },function(){
  203. if(callback!=null) callback();
  204. });
  205. }
  206. //Ajout ou modification d'élément sketch
  207. function hackpoint_sketch_save(){
  208. var data = {
  209. action : 'hackpoint_sketch_save',
  210. id : $('#sketch-form').attr('data-id'),
  211. label : $('#label').val(),
  212. state : $('#state').prop('checked'),
  213. comment : $('#comment').val()
  214. }
  215. $.action(data);
  216. }
  217. //Suppression d'élement sketch
  218. function hackpoint_sketch_delete(element){
  219. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  220. $.action({
  221. action : 'hackpoint_sketch_delete',
  222. id : $('#sketch-form').attr('data-id')
  223. },function(r){
  224. window.location = 'index.php?module=hackpoint&success=Sketch supprimé';
  225. });
  226. }
  227. /** RESOURCE **/
  228. //Récuperation d'une liste de resource dans le tableau #resources
  229. function hackpoint_resource_search(callback,triggered){
  230. $('#resources').fill({
  231. action:'hackpoint_resource_search',
  232. sketch : $('#sketch-form').attr('data-id'),
  233. showItems : false
  234. },function(){
  235. //rend les menu sortables
  236. $( "#resources" ).sortable({
  237. axis: "y",
  238. update: function( event, ui ){
  239. var sort = [];
  240. $( "#resources li:visible" ).each(function(i,li){
  241. li = $(li);
  242. sort.push(li.attr('data-id'));
  243. });
  244. $.action({
  245. action : 'hackpoint_resource_sort',
  246. sort : sort
  247. },function(r){
  248. });
  249. }
  250. });
  251. $( "#resources" ).disableSelection();
  252. //affiche les menu de façon progressive
  253. $('#resources li:not(:eq(0))').removeClass('hidden')
  254. .css('transform','translateX(-120px)');
  255. $('#resources li:visible').each(function(i,element){
  256. setTimeout(function(){
  257. $(element)
  258. .css('transform','translateX(0px)');
  259. },150*i);
  260. });
  261. var trigger = '#resources li:eq(1)';
  262. if(triggered){
  263. trigger = triggered;
  264. }else if($.urlParam('resource')){
  265. trigger = '#resources li[data-id="'+$.urlParam('resource')+'"]';
  266. }
  267. $(trigger).trigger('click');
  268. if(callback!=null) callback();
  269. });
  270. }
  271. function hackpoint_resource_edit(element){
  272. var line = $(element).closest('li');
  273. var id = line.attr('data-id');
  274. $('#resources li:visible').removeClass('active');
  275. line.addClass('active');
  276. $.action({
  277. action : 'hackpoint_resource_edit',
  278. id : id
  279. },function(r){
  280. $('#sketch-editor').html(r.html).attr('data-id',id);
  281. window.history.replaceState(null, null, "index.php?module=hackpoint&page=sheet.sketch&id="+$('#sketch-form').attr('data-id')+"&resource="+id);
  282. init_components('#sketch-editor');
  283. if(r.javascript){
  284. eval(r.javascript);
  285. }
  286. });
  287. }
  288. function resource_add_document(files){
  289. $.action({
  290. action : 'resource_add_document',
  291. id: $('#sketch-editor').attr('data-id'),
  292. files : files
  293. }, function(r){
  294. $.each(r.files, function(i, file){
  295. var line = $('#sketch-editor li[data-path="'+file.oldPath+'"]');
  296. line.attr('data-path', file.relative);
  297. line.find('a').attr('href', file.url);
  298. line.find('i.pointer').attr('onclick', 'resource_delete_document(this)');
  299. line.find('img').attr('src', file.url);
  300. $('#sketch-editor [data-type="dropzone"] input:not(:visible)').val('');
  301. });
  302. });
  303. }
  304. function resource_delete_document(element){
  305. if(!confirm("Êtes-vous sûr de vouloir supprimer ce fichier ?")) return;
  306. var line = $(element).closest('li');
  307. $.action({
  308. action : 'resource_delete_document',
  309. path : line.attr('data-path')
  310. },function(r){
  311. line.remove();
  312. });
  313. }
  314. function hackpoint_resource_title_edit(event,element){
  315. event.stopPropagation();
  316. event.preventDefault();
  317. var title = $(element);
  318. var span = $(element).find('span');
  319. var input = $(element).find('input');
  320. span.hide();
  321. input.show();
  322. input.focus();
  323. input.val(span.text());
  324. input.select();
  325. input.blur(function(){
  326. input.hide();
  327. span.text(input.val());
  328. span.show();
  329. var li = $(this).closest('li');
  330. $.action({
  331. action : 'hackpoint_resource_save',
  332. id : li.attr('data-id'),
  333. label : input.val()
  334. },function(r){
  335. });
  336. });
  337. console.log(event,'hey!');
  338. }
  339. function hackpoint_resource_mirrorify(element,data){
  340. var editor = CodeMirror.fromTextArea($(element).get(0), data);
  341. editor.on("blur", function(cm,obj){
  342. var data = {};
  343. data.action = 'hackpoint_resource_save_content';
  344. data.id = $('#sketch-editor').attr('data-id');
  345. data.content = cm.getValue();
  346. $('.sketch-preloader').show();
  347. $.action(data,function(r){
  348. setTimeout(function(){
  349. $('.sketch-preloader').fadeOut(200);
  350. },300);
  351. });
  352. });
  353. editor.on("change", function() {
  354. var data ={height:800};
  355. var wrap = editor.getWrapperElement();
  356. var approp = editor.getScrollInfo().height > data.height ? data.height+"px" : "auto";
  357. if (wrap.style.height != approp) {
  358. wrap.style.height = approp;
  359. editor.refresh();
  360. }
  361. });
  362. }
  363. //Ajout ou modification d'élément resource
  364. function hackpoint_resource_save(element){
  365. var data = {
  366. action:'hackpoint_resource_save',
  367. sketch:$('#sketch-form').attr('data-id'),
  368. type:$(element).attr('data-slug')
  369. }
  370. $.action(data,function(r){
  371. hackpoint_resource_search(null,'#resources li[data-id="'+r.id+'"]');
  372. });
  373. }
  374. //Suppression d'élement resource
  375. function hackpoint_resource_delete(element,event){
  376. event.stopPropagation();
  377. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  378. var line = $(element).closest('li');
  379. $.action({
  380. action : 'hackpoint_resource_delete',
  381. id : line.attr('data-id')
  382. },function(r){
  383. $('#resources li:visible(:eq(0))').removeClass('active');
  384. $('#sketch-editor').html('');
  385. line.remove();
  386. });
  387. }