main.js 12 KB

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