main.js 12 KB

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