main.js 16 KB

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