main.js 10 KB

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