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