main.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. //CHARGEMENT DE LA PAGE
  2. function init_plugin_hackpoint(){
  3. switch($.urlParam('page')){
  4. case 'sheet.sketch':
  5. if($('.hackpoint').hasClass('readonly')){
  6. $('.editable-input').attr('readonly','readonly');
  7. init_components();
  8. }
  9. break;
  10. case 'list.part':
  11. hackpoint_part_search();
  12. break;
  13. default:
  14. break;
  15. }
  16. $('#parts').sortable_table({
  17. onSort : hackpoint_part_search
  18. });
  19. $('#sketchs').sortable_table({
  20. onSort : hackpoint_sketch_search
  21. });
  22. hackpoint_resource_search();
  23. }
  24. //Enregistrement des configurations
  25. function hackpoint_setting_save(){
  26. $.action({
  27. action : 'hackpoint_setting_save',
  28. fields : $('#hackpoint-setting-form').toJson()
  29. },function(){ $.message('info','Configuration enregistrée'); });
  30. }
  31. //plugin jquery pour modale
  32. $.fn.extend({
  33. modalize : function(options){
  34. var obj = $(this);
  35. var o = $.extend({
  36. blur : null
  37. }, options);
  38. var overlay = $('.modalize-overlay');
  39. var modal = $('.modalize');
  40. if(overlay.length==0){
  41. overlay = $('<div class="modalize-overlay"></div>');
  42. modal = $('<div class="modalize bounceIn"></div>');
  43. $('body').append(overlay).append(modal);
  44. }
  45. var form = obj.detach();
  46. form.removeClass('hide');
  47. modal.html(form.get(0).outerHTML);
  48. var modalize = {
  49. options : o,
  50. modal : modal,
  51. overlay : overlay,
  52. hide : function(){
  53. overlay.fadeOut();
  54. $(o.blur).removeClass('blur');
  55. modal.hide();
  56. }
  57. }
  58. modal.css('height',o.height?o.height+'px':'');
  59. modal.show();
  60. overlay.show();
  61. $(o.blur).addClass('blur');
  62. overlay.click(function(){
  63. modalize.hide();
  64. });
  65. return modalize;
  66. }
  67. });
  68. /** PART **/
  69. //Récuperation d'une liste de part dans le tableau #parts
  70. function hackpoint_resource_part_search(callback){
  71. $('#parts').fill({
  72. action:'hackpoint_resource_part_search',
  73. resource : $('#sketch-editor').attr('data-id'),
  74. showing : function(li,i){
  75. li.css('transform','scale(0)').removeClass('hidden');
  76. setTimeout(function(){
  77. li.css('transform','scale(1)');
  78. },i*200);
  79. }
  80. },function(){
  81. $('#parts li:not(:eq(0))').each(function(i,li){
  82. hackpoint_resource_part_upload(li);
  83. var div = $('[data-stream]',li);
  84. if($(div).attr('data-stream') =='') return;
  85. $(div).css('background','url(data:'+$(div).attr('data-stream')+')')
  86. .css('background-size','cover');
  87. });
  88. if(callback!=null) callback();
  89. });
  90. }
  91. //Récuperation d'une liste de part dans le tableau #parts
  92. function hackpoint_part_search(callback){
  93. $('#parts').fill({
  94. action:'hackpoint_part_search',
  95. resource : $('#sketch-editor').attr('data-id'),
  96. },function(){
  97. $('#parts li:not(:eq(0))').each(function(i,li){
  98. hackpoint_resource_part_upload(li);
  99. var div = $('[data-stream]',li);
  100. if($(div).attr('data-stream') =='') return;
  101. $(div).css('background','url(data:'+$(div).attr('data-stream')+')')
  102. .css('background-size','cover');
  103. });
  104. $('#parts li:not(:eq(0))').css('transform','scale(0)').removeClass('hidden')
  105. $('#parts li:not(:eq(0))').each(function(i,li){
  106. var li = $(li);
  107. setTimeout(function(){
  108. li.css('transform','scale(1)');
  109. },i*200);
  110. });
  111. if(callback!=null) callback();
  112. });
  113. }
  114. //Ajout ou modification d'élément part
  115. function hackpoint_part_save(element){
  116. var li = $(element).closest('li');
  117. var data = {
  118. action : 'hackpoint_part_save',
  119. resource : $('#sketch-editor').attr('data-id'),
  120. part : li.attr('data-part'),
  121. id : li.attr('data-id'),
  122. label : li.find('.label').val(),
  123. brand : li.find('.brand input').val(),
  124. price : li.find('.price input').val(),
  125. url : li.find('.url').val(),
  126. picture : li.find('.part-image').attr('data-stream')
  127. }
  128. $.action(data,function(r){
  129. li.attr('data-part',r.part);
  130. li.attr('data-id',r.id)
  131. $.message('success','Enregistré');
  132. });
  133. }
  134. function hackpoint_part_find(element){
  135. var li = $(element).closest('li');
  136. var label = li.find('.label').val();
  137. var brand = li.find('.brand input').val();
  138. var url = "https://www.ebay.fr/sch/i.html?_from=R40&_sacat=0&LH_BIN=1&LH_PrefLoc=2&_sop=15";
  139. if(null!=brand && brand !='') label = brand+'+'+label;
  140. url += "&_nkw="+label;
  141. window.open(url);
  142. }
  143. function hackpoint_part_add(){
  144. var tpl = $('#parts li:eq(0)').get(0).outerHTML;
  145. var data = {
  146. price : 1,
  147. url : 'http://ebay.com'
  148. }
  149. var item = $(Mustache.render(tpl,data));
  150. item.removeClass('hidden');
  151. item.css({
  152. transform : 'scale(0)',
  153. opacity : 0,
  154. });
  155. $('#parts li:eq(0)').after(item);
  156. item.css({
  157. transform : 'scale(1) rotate(0deg)',
  158. opacity : 1,
  159. });
  160. init_components(item);
  161. hackpoint_resource_part_upload(item);
  162. }
  163. function hackpoint_resource_part_upload(item){
  164. var item = $(item);
  165. var image = item.find('.part-image');
  166. image.click(function(e){
  167. e.preventDefault();
  168. e.stopPropagation();
  169. var picker = image.next('input[type="file"]');
  170. picker.trigger('click');
  171. picker.change(function(){
  172. var file = picker.get(0).files[0];
  173. var reader = new FileReader();
  174. reader.addEventListener("load", function () {
  175. image.css('background','url('+reader.result+')')
  176. .attr('data-stream',reader.result)
  177. .css('background-size','cover');
  178. }, false);
  179. reader.readAsDataURL(file);
  180. });
  181. });
  182. image.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  183. e.preventDefault();
  184. e.stopPropagation();
  185. })
  186. image.on('drop', function (e) {
  187. var droppedFiles = e.originalEvent.dataTransfer.files;
  188. var reader = new FileReader();
  189. reader.readAsDataURL(droppedFiles[0]);
  190. reader.onload = function () {
  191. image.css('background','url('+reader.result+')')
  192. .attr('data-stream',reader.result)
  193. .css('background-size','cover');
  194. };
  195. reader.onerror = function (error) {
  196. console.log('Error: ', error);
  197. };
  198. });
  199. /*
  200. var preload = $('<div class="preload progress-bar progress-bar-striped progress-bar-animated"></div>');
  201. item.find('.part-image').append(preload);
  202. item.find('.part-image').attr('id','part-image-'+$('#parts li').length());
  203. item.find('.part-image').upload({
  204. allowed : 'jpg,png,jpeg,bmp,svg',
  205. size : 0,
  206. action : 'hackpoint_part_image_upload',
  207. readonly: false,
  208. start: function(){
  209. preload.show();
  210. },
  211. success: function(response){
  212. if(response.previews.length && response.previews[0].name) {
  213. }
  214. console.log(response);
  215. preload.fadeOut();
  216. },
  217. complete: function(){
  218. preload.fadeOut();
  219. }
  220. });*/
  221. }
  222. //Suppression d'élement part
  223. function hackpoint_resource_part_delete(element){
  224. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  225. var line = $(element).closest('li');
  226. if(line.attr('data-id')==''){
  227. line.css('transform','scale(0)');
  228. setTimeout(function(){
  229. line.remove()
  230. },210);
  231. return;
  232. }
  233. $.action({
  234. action : 'hackpoint_resource_part_delete',
  235. id : line.attr('data-id')
  236. },function(r){
  237. line.css('transform','scale(0)');
  238. setTimeout(function(){
  239. line.remove()
  240. },210);
  241. });
  242. }
  243. //Suppression d'élement part
  244. function hackpoint_part_delete(element){
  245. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  246. var line = $(element).closest('li');
  247. if(line.attr('data-id')==''){
  248. line.css('transform','scale(0)');
  249. setTimeout(function(){
  250. line.remove()
  251. },210);
  252. return;
  253. }
  254. $.action({
  255. action : 'hackpoint_part_delete',
  256. id : line.attr('data-id')
  257. },function(r){
  258. line.css('transform','scale(0)');
  259. setTimeout(function(){
  260. line.remove()
  261. },210);
  262. });
  263. }
  264. /** SKETCH **/
  265. //Récuperation d'une liste de sketch dans le tableau #sketchs
  266. function hackpoint_sketch_search(callback){
  267. $('#sketchs').fill({
  268. action:'hackpoint_sketch_search',
  269. filters : $('#filters').filters(),
  270. sort : $('#sketchs').sortable_table('get')
  271. },function(r){
  272. if(!r.rows) $('.no-sketch').removeClass('hidden');
  273. init_components('#sketchs');
  274. $('.progress').click(function(e){
  275. var x = e.pageX - $(this).offset().left;
  276. var percent = Math.round(x *100 / $(this).width());
  277. var progressClass = 'bg-danger';
  278. if(percent>97) percent = 100;
  279. if(percent > 30) progressClass = 'bg-warning';
  280. if(percent > 45) progressClass = 'bg-info';
  281. if(percent > 65) progressClass = '';
  282. if(percent > 85) progressClass = 'bg-success';
  283. var li = $(this).closest('li');
  284. $(this).find('.progress-bar')
  285. .css('width',percent+'%')
  286. .attr('aria-valuenow',percent)
  287. .text(percent+'%')
  288. .attr('class','progress-bar progress-bar-striped progress-bar-animated '+progressClass);
  289. $.action({
  290. action : 'hackpoint_sketch_progress_save',
  291. id : li.attr('data-id'),
  292. progress : percent,
  293. },function(r){});
  294. });
  295. if(callback!=null) callback();
  296. });
  297. }
  298. //Ajout ou modification d'élément sketch
  299. function hackpoint_sketch_save(){
  300. if($('.hackpoint').hasClass('readonly')) return;
  301. $('.sketch-preloader').show();
  302. var data = {
  303. action : 'hackpoint_sketch_save',
  304. id : $('#sketch-form').attr('data-id'),
  305. label : $('#label').val(),
  306. state : $('#state').prop('checked'),
  307. comment : $('#comment').val()
  308. }
  309. $.action(data,function(){
  310. $('.sketch-preloader').fadeOut(300);
  311. });
  312. }
  313. function hackpoint_sketch_download(){
  314. var id = $('#sketch-form').attr('data-id');
  315. $.action({
  316. action : 'hackpoint_sketch_download',
  317. downloadResponse : true,
  318. id : id
  319. },function(r){
  320. });
  321. }
  322. function hackpoint_sketch_share(){
  323. $('#share-sketch-modal').modalize({
  324. blur:".hackpoint,#mainMenu",
  325. height:300
  326. });
  327. hackpoint_sketch_share_mode();
  328. $('.share-input').click(function () {
  329. $(this).select();
  330. });
  331. $('.share-menu-mode').click(function () {
  332. hackpoint_sketch_share_mode();
  333. });
  334. }
  335. function hackpoint_sketch_share_mode(){
  336. var tpl = $('.shareCode').html();
  337. var data = {
  338. sketch : $('#sketch-form').attr('data-id'),
  339. resource : $.urlParam('resource'),
  340. menu : $('.share-menu-mode').prop('checked')?1:0,
  341. url : window.location.protocol+'//' + window.location.hostname + window.location.pathname
  342. }
  343. var html = Mustache.render(tpl,data);
  344. $('.share-input').html(html);
  345. }
  346. function hackpoint_sketch_save_cover(input,stream){
  347. console.log('hey');
  348. var li = input.closest('li');
  349. $.action({
  350. action : 'hackpoint_sketch_save_cover',
  351. stream : stream,
  352. sketch : li.attr('data-id')
  353. },function(r){
  354. input.attr('src',r.stream);
  355. });
  356. }
  357. //Suppression d'élement sketch
  358. function hackpoint_sketch_delete(element){
  359. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  360. $.action({
  361. action : 'hackpoint_sketch_delete',
  362. id : $('#sketch-form').attr('data-id')
  363. },function(r){
  364. window.location = 'index.php?module=hackpoint&success=Sketch supprimé';
  365. });
  366. }
  367. /** RESOURCE **/
  368. //Récuperation d'une liste de resource dans le tableau #resources
  369. function hackpoint_resource_search(callback,triggered){
  370. $('#resources').fill({
  371. action:'hackpoint_resource_search',
  372. sketch : $('#sketch-form').attr('data-id'),
  373. showing : function(li,i){
  374. //affiche les menu de façon progressive
  375. li.removeClass('hidden')
  376. .css('transform','translateX(-120px)');
  377. setTimeout(function(){
  378. li
  379. .css('transform','translateX(0px)');
  380. },150*i);
  381. }
  382. },function(){
  383. //rend les menu sortables
  384. $( ".hackpoint.editable #resources" ).sortable({
  385. axis: "y",
  386. update: function( event, ui ){
  387. var sort = [];
  388. $( "#resources li:visible" ).each(function(i,li){
  389. li = $(li);
  390. sort.push(li.attr('data-id'));
  391. });
  392. $.action({
  393. action : 'hackpoint_resource_sort',
  394. sort : sort
  395. },function(r){
  396. });
  397. }
  398. });
  399. $( "#resources" ).disableSelection();
  400. var trigger = '#resources li:eq(1)';
  401. if(triggered){
  402. trigger = triggered;
  403. }else if($.urlParam('resource')){
  404. trigger = '#resources li[data-id="'+$.urlParam('resource')+'"]';
  405. }
  406. $(trigger).trigger('click');
  407. if(callback!=null) callback();
  408. });
  409. }
  410. function hackpoint_resource_edit(element){
  411. var line = $(element).closest('li');
  412. var id = line.attr('data-id');
  413. $('#resources li:visible').removeClass('active');
  414. line.addClass('active');
  415. $.action({
  416. action : 'hackpoint_resource_edit',
  417. id : id
  418. },function(r){
  419. $('#sketch-editor')
  420. .html(r.html)
  421. .attr('data-id',id)
  422. .attr('class','resource-'+r.resourceType);
  423. window.history.replaceState(null, null, "index.php?module=hackpoint&page=sheet.sketch&id="+$('#sketch-form').attr('data-id')+"&resource="+id);
  424. init_components('#sketch-editor');
  425. if(r.javascript){
  426. eval(r.javascript);
  427. }
  428. });
  429. }
  430. function resource_add_document(files){
  431. $.action({
  432. action : 'resource_add_document',
  433. id: $('#sketch-editor').attr('data-id'),
  434. files : files
  435. }, function(r){
  436. $.each(r.files, function(i, file){
  437. var line = $('#sketch-editor li[data-path="'+file.oldPath+'"]');
  438. line.attr('data-path', file.relative);
  439. line.find('a').attr('href', file.url);
  440. line.find('i.pointer').attr('onclick', 'resource_delete_document(this)');
  441. if($('.hackpoint').hasClass('readonly')) line.find('i.pointer').hide();
  442. if(!file.icon){
  443. line.find('img').attr('src', file.url);
  444. }else{
  445. line.find('img').after('<i class="'+file.icon+'"></i>');
  446. line.find('img').remove();
  447. }
  448. $('#sketch-editor [data-type="dropzone"] input:not(:visible)').val('');
  449. });
  450. });
  451. }
  452. function resource_delete_document(element){
  453. if($('.hackpoint').hasClass('readonly')) return;
  454. if(!confirm("Êtes-vous sûr de vouloir supprimer ce fichier ?")) return;
  455. var line = $(element).closest('li');
  456. $.action({
  457. action : 'resource_delete_document',
  458. path : line.attr('data-path')
  459. },function(r){
  460. line.remove();
  461. });
  462. }
  463. function hackpoint_resource_title_edit(event,element){
  464. event.stopPropagation();
  465. event.preventDefault();
  466. var title = $(element);
  467. var span = $(element).find('span');
  468. var input = $(element).find('input');
  469. span.hide();
  470. input.removeClass('hidden');
  471. input.focus();
  472. input.val(span.text());
  473. input.select();
  474. input.blur(function(){
  475. input.hide();
  476. span.text(input.val());
  477. span.show();
  478. var li = $(this).closest('li');
  479. $.action({
  480. action : 'hackpoint_resource_save',
  481. id : li.attr('data-id'),
  482. label : input.val()
  483. },function(r){
  484. });
  485. });
  486. console.log(event,'hey!');
  487. }
  488. function hackpoint_resource_mirrorify(element,data){
  489. if($('.hackpoint').hasClass('readonly')) data.readOnly = true;
  490. var editor = CodeMirror.fromTextArea($(element).get(0), data);
  491. editor.on("blur", function(cm,obj){
  492. if($('.hackpoint').hasClass('readonly')) return;
  493. var data = {};
  494. data.action = 'hackpoint_resource_save_content';
  495. data.id = $('#sketch-editor').attr('data-id');
  496. data.content = cm.getValue();
  497. $('.sketch-preloader').show();
  498. $.action(data,function(r){
  499. setTimeout(function(){
  500. $('.sketch-preloader').fadeOut(200);
  501. },300);
  502. });
  503. });
  504. editor.on("change", function() {
  505. var data ={height:800};
  506. var wrap = editor.getWrapperElement();
  507. var approp = editor.getScrollInfo().height > data.height ? data.height+"px" : "auto";
  508. if (wrap.style.height != approp) {
  509. wrap.style.height = approp;
  510. editor.refresh();
  511. }
  512. });
  513. }
  514. //Ajout ou modification d'élément resource
  515. function hackpoint_resource_save(element){
  516. var data = {
  517. action:'hackpoint_resource_save',
  518. sketch:$('#sketch-form').attr('data-id'),
  519. type:$(element).attr('data-slug')
  520. }
  521. $.action(data,function(r){
  522. var tpl = $('#resources li:eq(0)').get(0).outerHTML;
  523. var element = $(Mustache.render(tpl,r));
  524. $('#resources').append(element);
  525. element.removeClass('hidden')
  526. .css('transform','translateX(-120px)');
  527. setTimeout(function(){
  528. $(element).css('transform','translateX(0px)');
  529. },150);
  530. element.trigger('click');
  531. });
  532. }
  533. //Suppression d'élement resource
  534. function hackpoint_resource_delete(element,event){
  535. event.stopPropagation();
  536. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  537. var line = $(element).closest('li');
  538. $.action({
  539. action : 'hackpoint_resource_delete',
  540. id : line.attr('data-id')
  541. },function(r){
  542. $('#resources li:visible(:eq(0))').removeClass('active');
  543. $('#sketch-editor').html('');
  544. line.remove();
  545. });
  546. }
  547. function preloader(mode){
  548. var preloader = $('.hackpoint-preloader');
  549. if(preloader.length==0){
  550. preloader = $('<div class="hackpoint-preloader" title="Chargement..."><i class="fas fa-circle-notch fa-spin"></i></div>');
  551. $('body').append(preloader);
  552. }
  553. setTimeout(function(){
  554. if(mode){
  555. preloader.addClass('show');
  556. }else{
  557. preloader.removeClass('show');
  558. }
  559. },50);
  560. }