main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. //CHARGEMENT DE LA PAGE
  2. function init_plugin_hackpoint(){
  3. switch($.urlParam('page')){
  4. default:
  5. break;
  6. }
  7. $('#parts').sortable_table({
  8. onSort : hackpoint_part_search
  9. });
  10. $('#sketchs').sortable_table({
  11. onSort : hackpoint_sketch_search
  12. });
  13. hackpoint_resource_search();
  14. }
  15. //Enregistrement des configurations
  16. function hackpoint_setting_save(){
  17. $.action({
  18. action : 'hackpoint_setting_save',
  19. fields : $('#hackpoint-setting-form').toJson()
  20. },function(){ $.message('info','Configuration enregistrée'); });
  21. }
  22. /** PART **/
  23. //Récuperation d'une liste de part dans le tableau #parts
  24. function hackpoint_part_search(callback){
  25. $('#parts').fill({
  26. action:'hackpoint_part_search',
  27. resource : $('#sketch-editor').attr('data-id'),
  28. showItems : false
  29. },function(){
  30. $('#parts li:not(:eq(0)) [data-stream]').each(function(i,div){
  31. if($(div).attr('data-stream') =='') return;
  32. $(div).css('background','url(data:'+$(div).attr('data-stream')+')')
  33. .css('background-size','cover');
  34. });
  35. $('#parts li:not(:eq(0))').css('transform','scale(0)').removeClass('hidden')
  36. $('#parts li:not(:eq(0))').each(function(i,li){
  37. var li = $(li);
  38. setTimeout(function(){
  39. li.css('transform','scale(1)');
  40. },i*200);
  41. });
  42. if(callback!=null) callback();
  43. });
  44. }
  45. //Ajout ou modification d'élément part
  46. function hackpoint_part_save(element){
  47. var li = $(element).closest('li');
  48. console.log(li);
  49. var data = {
  50. action : 'hackpoint_part_save',
  51. resource : $('#sketch-editor').attr('data-id'),
  52. label : li.find('.label').val(),
  53. brand : li.find('.brand').val(),
  54. price : li.find('.price').val(),
  55. url : li.find('.url').val(),
  56. picture : li.find('.part-image').attr('data-stream')
  57. }
  58. $.action(data,function(r){
  59. $.message('success','Enregistré');
  60. });
  61. }
  62. function hackpoint_part_add(){
  63. var tpl = $('#parts li:eq(0)').get(0).outerHTML;
  64. var data = {
  65. price : 1,
  66. url : 'http://ebay.com'
  67. }
  68. var item = $(Mustache.render(tpl,data));
  69. item.removeClass('hidden');
  70. item.css({
  71. transform : 'scale(0)',
  72. opacity : 0,
  73. });
  74. $('#parts li:eq(0)').after(item);
  75. item.css({
  76. transform : 'scale(1) rotate(0deg)',
  77. opacity : 1,
  78. });
  79. init_components(item);
  80. var image = item.find('.part-image');
  81. image.click(function(e){
  82. e.preventDefault();
  83. e.stopPropagation();
  84. var picker = image.next('input[type="file"]');
  85. picker.trigger('click');
  86. picker.change(function(){
  87. var file = picker.get(0).files[0];
  88. var reader = new FileReader();
  89. reader.addEventListener("load", function () {
  90. image.css('background','url(data:'+reader.result+')')
  91. .attr('data-stream',reader.result)
  92. .css('background-size','cover');
  93. }, false);
  94. reader.readAsDataURL(file);
  95. });
  96. });
  97. image.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  98. e.preventDefault();
  99. e.stopPropagation();
  100. })
  101. image.on('drop', function (e) {
  102. var droppedFiles = e.originalEvent.dataTransfer.files;
  103. var reader = new FileReader();
  104. reader.readAsDataURL(droppedFiles[0]);
  105. reader.onload = function () {
  106. image.css('background','url(data:image/jpeg;'+reader.result+')');
  107. image.css('background-size','cover');
  108. };
  109. reader.onerror = function (error) {
  110. console.log('Error: ', error);
  111. };
  112. });
  113. /*
  114. var preload = $('<div class="preload progress-bar progress-bar-striped progress-bar-animated"></div>');
  115. item.find('.part-image').append(preload);
  116. item.find('.part-image').attr('id','part-image-'+$('#parts li').length());
  117. item.find('.part-image').upload({
  118. allowed : 'jpg,png,jpeg,bmp,svg',
  119. size : 0,
  120. action : 'hackpoint_part_image_upload',
  121. readonly: false,
  122. start: function(){
  123. preload.show();
  124. },
  125. success: function(response){
  126. if(response.previews.length && response.previews[0].name) {
  127. }
  128. console.log(response);
  129. preload.fadeOut();
  130. },
  131. complete: function(){
  132. preload.fadeOut();
  133. }
  134. });*/
  135. }
  136. //Suppression d'élement part
  137. function hackpoint_resource_part_delete(element){
  138. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  139. var line = $(element).closest('li');
  140. if(line.attr('data-id')==''){
  141. line.css('transform','scale(0)');
  142. setTimeout(function(){
  143. line.remove()
  144. },210);
  145. return;
  146. }
  147. $.action({
  148. action : 'hackpoint_resource_part_delete',
  149. id : line.attr('data-id')
  150. },function(r){
  151. line.css('transform','scale(0)');
  152. setTimeout(function(){
  153. line.remove()
  154. },210);
  155. });
  156. }
  157. //Suppression d'élement part
  158. function hackpoint_part_delete(element){
  159. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  160. var line = $(element).closest('li');
  161. if(line.attr('data-id')==''){
  162. line.css('transform','scale(0)');
  163. setTimeout(function(){
  164. line.remove()
  165. },210);
  166. return;
  167. }
  168. $.action({
  169. action : 'hackpoint_part_delete',
  170. id : line.attr('data-id')
  171. },function(r){
  172. line.css('transform','scale(0)');
  173. setTimeout(function(){
  174. line.remove()
  175. },210);
  176. });
  177. }
  178. /** SKETCH **/
  179. //Récuperation d'une liste de sketch dans le tableau #sketchs
  180. function hackpoint_sketch_search(callback){
  181. $('#sketchs').fill({
  182. action:'hackpoint_sketch_search',
  183. filters : $('#filters').filters(),
  184. sort : $('#sketchs').sortable_table('get')
  185. },function(){
  186. if(callback!=null) callback();
  187. });
  188. }
  189. //Ajout ou modification d'élément sketch
  190. function hackpoint_sketch_save(){
  191. var data = {
  192. action : 'hackpoint_sketch_save',
  193. id : $('#sketch-form').attr('data-id'),
  194. label : $('#label').val(),
  195. state : $('#state').prop('checked'),
  196. comment : $('#comment').val()
  197. }
  198. $.action(data);
  199. }
  200. //Suppression d'élement sketch
  201. function hackpoint_sketch_delete(element){
  202. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  203. $.action({
  204. action : 'hackpoint_sketch_delete',
  205. id : $('#sketch-form').attr('data-id')
  206. },function(r){
  207. window.location = 'index.php?module=hackpoint&success=Sketch supprimé';
  208. });
  209. }
  210. /** RESOURCE **/
  211. //Récuperation d'une liste de resource dans le tableau #resources
  212. function hackpoint_resource_search(callback,triggered){
  213. $('#resources').fill({
  214. action:'hackpoint_resource_search',
  215. sketch : $('#sketch-form').attr('data-id'),
  216. showItems : false
  217. },function(){
  218. //rend les menu sortables
  219. $( "#resources" ).sortable({
  220. axis: "y",
  221. update: function( event, ui ){
  222. var sort = [];
  223. $( "#resources li:visible" ).each(function(i,li){
  224. li = $(li);
  225. sort.push(li.attr('data-id'));
  226. });
  227. $.action({
  228. action : 'hackpoint_resource_sort',
  229. sort : sort
  230. },function(r){
  231. });
  232. }
  233. });
  234. $( "#resources" ).disableSelection();
  235. //affiche les menu de façon progressive
  236. $('#resources li:not(:eq(0))').removeClass('hidden')
  237. .css('transform','translateX(-120px)');
  238. $('#resources li:visible').each(function(i,element){
  239. setTimeout(function(){
  240. $(element)
  241. .css('transform','translateX(0px)');
  242. },150*i);
  243. });
  244. var trigger = '#resources li:eq(1)';
  245. if(triggered){
  246. trigger = triggered;
  247. }else if($.urlParam('resource')){
  248. trigger = '#resources li[data-id="'+$.urlParam('resource')+'"]';
  249. }
  250. $(trigger).trigger('click');
  251. if(callback!=null) callback();
  252. });
  253. }
  254. function hackpoint_resource_edit(element){
  255. var line = $(element).closest('li');
  256. var id = line.attr('data-id');
  257. $('#resources li:visible').removeClass('active');
  258. line.addClass('active');
  259. $.action({
  260. action : 'hackpoint_resource_edit',
  261. id : id
  262. },function(r){
  263. $('#sketch-editor').html(r.html).attr('data-id',id);
  264. window.history.replaceState(null, null, "index.php?module=hackpoint&page=sheet.sketch&id="+$('#sketch-form').attr('data-id')+"&resource="+id);
  265. init_components('#sketch-editor');
  266. if(r.javascript){
  267. eval(r.javascript);
  268. }
  269. });
  270. }
  271. function resource_add_document(files){
  272. $.action({
  273. action : 'resource_add_document',
  274. id: $('#sketch-editor').attr('data-id'),
  275. files : files
  276. }, function(r){
  277. $.each(r.files, function(i, file){
  278. var line = $('#sketch-editor li[data-path="'+file.oldPath+'"]');
  279. line.attr('data-path', file.relative);
  280. line.find('a').attr('href', file.url);
  281. line.find('i.pointer').attr('onclick', 'resource_delete_document(this)');
  282. line.find('img').attr('src', file.url);
  283. $('#sketch-editor [data-type="dropzone"] input:not(:visible)').val('');
  284. });
  285. });
  286. }
  287. function resource_delete_document(element){
  288. if(!confirm("Êtes-vous sûr de vouloir supprimer ce fichier ?")) return;
  289. var line = $(element).closest('li');
  290. $.action({
  291. action : 'resource_delete_document',
  292. path : line.attr('data-path')
  293. },function(r){
  294. line.remove();
  295. });
  296. }
  297. function hackpoint_resource_title_edit(event,element){
  298. event.stopPropagation();
  299. event.preventDefault();
  300. var title = $(element);
  301. var span = $(element).find('span');
  302. var input = $(element).find('input');
  303. span.hide();
  304. input.show();
  305. input.focus();
  306. input.val(span.text());
  307. input.select();
  308. input.blur(function(){
  309. input.hide();
  310. span.text(input.val());
  311. span.show();
  312. var li = $(this).closest('li');
  313. $.action({
  314. action : 'hackpoint_resource_save',
  315. id : li.attr('data-id'),
  316. label : input.val()
  317. },function(r){
  318. });
  319. });
  320. console.log(event,'hey!');
  321. }
  322. function hackpoint_resource_mirrorify(element,data){
  323. var editor = CodeMirror.fromTextArea($(element).get(0), data);
  324. editor.on("blur", function(cm,obj){
  325. var data = {};
  326. data.action = 'hackpoint_resource_save_content';
  327. data.id = $('#sketch-editor').attr('data-id');
  328. data.content = cm.getValue();
  329. $('.sketch-preloader').show();
  330. $.action(data,function(r){
  331. setTimeout(function(){
  332. $('.sketch-preloader').fadeOut(200);
  333. },300);
  334. });
  335. });
  336. editor.on("change", function() {
  337. var data ={height:800};
  338. var wrap = editor.getWrapperElement();
  339. var approp = editor.getScrollInfo().height > data.height ? data.height+"px" : "auto";
  340. if (wrap.style.height != approp) {
  341. wrap.style.height = approp;
  342. editor.refresh();
  343. }
  344. });
  345. }
  346. //Ajout ou modification d'élément resource
  347. function hackpoint_resource_save(element){
  348. var data = {
  349. action:'hackpoint_resource_save',
  350. sketch:$('#sketch-form').attr('data-id'),
  351. type:$(element).attr('data-slug')
  352. }
  353. $.action(data,function(r){
  354. hackpoint_resource_search(null,'#resources li[data-id="'+r.id+'"]');
  355. });
  356. }
  357. //Suppression d'élement resource
  358. function hackpoint_resource_delete(element,event){
  359. event.stopPropagation();
  360. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  361. var line = $(element).closest('li');
  362. $.action({
  363. action : 'hackpoint_resource_delete',
  364. id : line.attr('data-id')
  365. },function(r){
  366. $('#resources li:visible(:eq(0))').removeClass('active');
  367. $('#sketch-editor').html('');
  368. line.remove();
  369. });
  370. }