main.js 11 KB

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