main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. default:
  11. break;
  12. }
  13. $('#sketchs').sortable_table({
  14. onSort : hackpoint_sketch_search
  15. });
  16. hackpoint_resource_search();
  17. }
  18. //Enregistrement des configurations
  19. function hackpoint_setting_save(){
  20. $.action({
  21. action : 'hackpoint_setting_save',
  22. fields : $('#hackpoint-setting-form').toJson()
  23. },function(){ $.message('info','Configuration enregistrée'); });
  24. }
  25. //plugin jquery pour modale
  26. $.fn.extend({
  27. modalize : function(options){
  28. var obj = $(this);
  29. var o = $.extend({
  30. blur : null
  31. }, options);
  32. var overlay = $('.modalize-overlay');
  33. var modal = $('.modalize');
  34. if(overlay.length==0){
  35. overlay = $('<div class="modalize-overlay"></div>');
  36. modal = $('<div class="modalize bounceIn"></div>');
  37. $('body').append(overlay).append(modal);
  38. }
  39. var form = obj.detach();
  40. form.removeClass('hide');
  41. modal.html(form.get(0).outerHTML);
  42. var modalize = {
  43. options : o,
  44. modal : modal,
  45. overlay : overlay,
  46. hide : function(){
  47. overlay.fadeOut();
  48. $(o.blur).removeClass('blur');
  49. modal.hide();
  50. }
  51. }
  52. modal.css('height',o.height?o.height+'px':'');
  53. modal.show();
  54. overlay.show();
  55. $(o.blur).addClass('blur');
  56. modal.find('[data-close]').click(function(){
  57. modalize.hide();
  58. });
  59. overlay.click(function(){
  60. modalize.hide();
  61. });
  62. return modalize;
  63. }
  64. });
  65. /** SKETCH **/
  66. //Récuperation d'une liste de sketch dans le tableau #sketchs
  67. function hackpoint_sketch_search(callback){
  68. $('#sketchs').fill({
  69. action:'hackpoint_sketch_search',
  70. filters : $('#filters').filters(),
  71. sort : $('#sketchs').sortable_table('get'),
  72. showing : function(li,i){
  73. //affiche les menu de façon progressive
  74. li.removeClass('hidden')
  75. .css({
  76. transform:'scale(0)',
  77. opacity:0,
  78. transition:'transform 0.2s ease-in-out,opacity 0.2s ease-in-out'
  79. });
  80. setTimeout(function(){
  81. li
  82. .css({
  83. transform:'scale(1)',
  84. opacity:1
  85. });
  86. },150*i);
  87. }
  88. },function(r){
  89. if(!r.rows) $('.no-sketch').removeClass('hidden');
  90. init_components('#sketchs');
  91. $('.progress').click(function(e){
  92. var x = e.pageX - $(this).offset().left;
  93. var percent = Math.round(x *100 / $(this).width());
  94. var progressClass = 'bg-danger';
  95. if(percent>97) percent = 100;
  96. if(percent > 30) progressClass = 'bg-warning';
  97. if(percent > 45) progressClass = 'bg-info';
  98. if(percent > 65) progressClass = '';
  99. if(percent > 85) progressClass = 'bg-success';
  100. var li = $(this).closest('li');
  101. $(this).find('.progress-bar')
  102. .css('width',percent+'%')
  103. .attr('aria-valuenow',percent)
  104. .text(percent+'%')
  105. .attr('class','progress-bar '+progressClass);
  106. $.action({
  107. action : 'hackpoint_sketch_progress_save',
  108. id : li.attr('data-id'),
  109. progress : percent,
  110. },function(r){});
  111. });
  112. if(callback!=null) callback();
  113. });
  114. }
  115. //Ajout ou modification d'élément sketch
  116. function hackpoint_sketch_save(){
  117. if($('.hackpoint').hasClass('readonly')) return;
  118. $('.sketch-preloader').show();
  119. var data = {
  120. action : 'hackpoint_sketch_save',
  121. id : $('#sketch-form').attr('data-id'),
  122. label : $('#label').val(),
  123. state : $('#state').prop('checked'),
  124. comment : $('#comment').val()
  125. }
  126. $.action(data,function(){
  127. $('.sketch-preloader').fadeOut(300);
  128. });
  129. }
  130. function hackpoint_sketch_download(){
  131. var id = $('#sketch-form').attr('data-id');
  132. $.action({
  133. action : 'hackpoint_sketch_download',
  134. downloadResponse : true,
  135. id : id
  136. },function(r){
  137. });
  138. }
  139. function hackpoint_sketch_share(){
  140. $('#share-sketch-modal').modalize({
  141. blur:".hackpoint,#mainMenu",
  142. height:300
  143. });
  144. hackpoint_sketch_share_mode();
  145. $('.share-input').click(function () {
  146. $(this).select();
  147. });
  148. $('.share-menu-mode').click(function () {
  149. hackpoint_sketch_share_mode();
  150. });
  151. }
  152. function hackpoint_sketch_share_mode(){
  153. var tpl = $('.shareCode').html();
  154. var data = {
  155. sketch : $('#sketch-form').attr('data-id'),
  156. resource : $.urlParam('resource'),
  157. menu : $('.share-menu-mode').prop('checked')?1:0,
  158. url : window.location.protocol+'//' + window.location.hostname + window.location.pathname
  159. }
  160. var html = Mustache.render(tpl,data);
  161. $('.share-input').html(html);
  162. }
  163. function hackpoint_sketch_save_cover(input,stream){
  164. console.log('hey');
  165. var li = input.closest('li');
  166. $.action({
  167. action : 'hackpoint_sketch_save_cover',
  168. stream : stream,
  169. sketch : li.attr('data-id')
  170. },function(r){
  171. input.attr('src',r.stream);
  172. });
  173. }
  174. //Suppression d'élement sketch
  175. function hackpoint_sketch_delete(element){
  176. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  177. $.action({
  178. action : 'hackpoint_sketch_delete',
  179. id : $('#sketch-form').attr('data-id')
  180. },function(r){
  181. window.location = 'index.php?module=hackpoint&success=Sketch supprimé';
  182. });
  183. }
  184. /** RESOURCE **/
  185. //Récuperation d'une liste de resource dans le tableau #resources
  186. function hackpoint_resource_search(callback,triggered){
  187. $('#resources').fill({
  188. action:'hackpoint_resource_search',
  189. sketch : $('#sketch-form').attr('data-id'),
  190. showing : function(li,i){
  191. //affiche les menu de façon progressive
  192. li.removeClass('hidden')
  193. .css('transform','translateX(-120px)');
  194. setTimeout(function(){
  195. li
  196. .css('transform','translateX(0px)');
  197. },150*i);
  198. }
  199. },function(){
  200. //rend les menu sortables
  201. $( ".hackpoint.editable #resources" ).sortable({
  202. axis: "y",
  203. update: function( event, ui ){
  204. var sort = [];
  205. $( "#resources li:visible" ).each(function(i,li){
  206. li = $(li);
  207. sort.push(li.attr('data-id'));
  208. });
  209. $.action({
  210. action : 'hackpoint_resource_sort',
  211. sort : sort
  212. },function(r){
  213. });
  214. }
  215. });
  216. $( "#resources" ).disableSelection();
  217. var trigger = '#resources li:eq(1)';
  218. if(triggered){
  219. trigger = triggered;
  220. }else if($.urlParam('resource')){
  221. trigger = '#resources li[data-id="'+$.urlParam('resource')+'"]';
  222. }
  223. $(trigger).trigger('click');
  224. if(callback!=null) callback();
  225. });
  226. }
  227. function hackpoint_resource_edit(element){
  228. var line = $(element).closest('li');
  229. var id = line.attr('data-id');
  230. $('#resources li:visible').removeClass('active');
  231. line.addClass('active');
  232. $.action({
  233. action : 'hackpoint_resource_edit',
  234. id : id
  235. },function(r){
  236. $('#sketch-editor')
  237. .html(r.html)
  238. .attr('data-id',id)
  239. .attr('class','resource-'+r.resourceType);
  240. window.history.replaceState(null, null, "index.php?module=hackpoint&page=sheet.sketch&id="+$('#sketch-form').attr('data-id')+"&resource="+id);
  241. init_components('#sketch-editor');
  242. if(r.javascript){
  243. eval(r.javascript);
  244. }
  245. if(window['hackpoint_resource_'+r.resourceType+'_init']){
  246. window['hackpoint_resource_'+r.resourceType+'_init']();
  247. }
  248. });
  249. }
  250. function resource_add_document(files){
  251. $.action({
  252. action : 'resource_add_document',
  253. id: $('#sketch-editor').attr('data-id'),
  254. files : files
  255. }, function(r){
  256. $.each(r.files, function(i, file){
  257. var line = $('#sketch-editor li[data-path="'+file.oldPath+'"]');
  258. line.attr('data-path', file.relative);
  259. line.find('a').attr('href', file.url);
  260. line.find('i.pointer').attr('onclick', 'resource_delete_document(this)');
  261. if($('.hackpoint').hasClass('readonly')) line.find('i.pointer').hide();
  262. if(!file.icon){
  263. line.find('img').attr('src', file.url);
  264. }else{
  265. line.find('img').after('<i class="'+file.icon+'"></i>');
  266. line.find('img').remove();
  267. }
  268. $('#sketch-editor [data-type="dropzone"] input:not(:visible)').val('');
  269. });
  270. });
  271. }
  272. function resource_delete_document(element){
  273. if($('.hackpoint').hasClass('readonly')) return;
  274. if(!confirm("Êtes-vous sûr de vouloir supprimer ce fichier ?")) return;
  275. var line = $(element).closest('li');
  276. $.action({
  277. action : 'resource_delete_document',
  278. path : line.attr('data-path')
  279. },function(r){
  280. line.remove();
  281. });
  282. }
  283. function hackpoint_resource_title_edit(event,element){
  284. event.stopPropagation();
  285. event.preventDefault();
  286. var title = $(element);
  287. var span = $(element).find('span');
  288. var input = $(element).find('input');
  289. span.hide();
  290. input.removeClass('hidden');
  291. input.focus();
  292. input.val(span.text());
  293. input.select();
  294. input.blur(function(){
  295. input.hide();
  296. span.text(input.val());
  297. span.show();
  298. var li = $(this).closest('li');
  299. $.action({
  300. action : 'hackpoint_resource_save',
  301. id : li.attr('data-id'),
  302. label : input.val()
  303. },function(r){
  304. });
  305. });
  306. console.log(event,'hey!');
  307. }
  308. function hackpoint_resource_mirrorify(element,data){
  309. if($('.hackpoint').hasClass('readonly')) data.readOnly = true;
  310. var editor = CodeMirror.fromTextArea($(element).get(0), data);
  311. editor.on("blur", function(cm,obj){
  312. if($('.hackpoint').hasClass('readonly')) return;
  313. var data = {};
  314. data.action = 'hackpoint_resource_save_content';
  315. data.id = $('#sketch-editor').attr('data-id');
  316. data.content = cm.getValue();
  317. $('.sketch-preloader').show();
  318. $.action(data,function(r){
  319. setTimeout(function(){
  320. $('.sketch-preloader').fadeOut(200);
  321. },300);
  322. });
  323. });
  324. editor.on("change", function() {
  325. var data ={height:800};
  326. var wrap = editor.getWrapperElement();
  327. var approp = editor.getScrollInfo().height > data.height ? data.height+"px" : "auto";
  328. if (wrap.style.height != approp) {
  329. wrap.style.height = approp;
  330. editor.refresh();
  331. }
  332. });
  333. }
  334. //Ajout ou modification d'élément resource
  335. function hackpoint_resource_save(element){
  336. var data = {
  337. action:'hackpoint_resource_save',
  338. sketch:$('#sketch-form').attr('data-id'),
  339. type:$(element).attr('data-slug')
  340. }
  341. $.action(data,function(r){
  342. var tpl = $('#resources li:eq(0)').get(0).outerHTML;
  343. var element = $(Mustache.render(tpl,r));
  344. $('#resources').append(element);
  345. element.removeClass('hidden')
  346. .css('transform','translateX(-120px)');
  347. setTimeout(function(){
  348. $(element).css('transform','translateX(0px)');
  349. },150);
  350. element.trigger('click');
  351. });
  352. }
  353. //Suppression d'élement resource
  354. function hackpoint_resource_delete(element,event){
  355. event.stopPropagation();
  356. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  357. var line = $(element).closest('li');
  358. $.action({
  359. action : 'hackpoint_resource_delete',
  360. id : line.attr('data-id')
  361. },function(r){
  362. $('#resources li:visible(:eq(0))').removeClass('active');
  363. $('#sketch-editor').html('');
  364. line.remove();
  365. });
  366. }
  367. function preloader(mode){
  368. var preloader = $('.hackpoint-preloader');
  369. if(preloader.length==0){
  370. preloader = $('<div class="hackpoint-preloader" title="Chargement..."><i class="fas fa-circle-notch fa-spin"></i></div>');
  371. $('body').append(preloader);
  372. }
  373. setTimeout(function(){
  374. if(mode){
  375. preloader.addClass('show');
  376. }else{
  377. preloader.removeClass('show');
  378. }
  379. },50);
  380. }