main.js 11 KB

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