main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. $(document).ready(function(){
  2. if($.urlParam('embeded') =="1"){
  3. $('.navbar,.resources-options,#sketch').hide();
  4. $('.container-fluid,.col-md-9,.col-md-3').css('padding','0');
  5. $('.list-group-item,#resource').css('padding','3px').css('border-radius','0px');
  6. $('.jumbotron').css('border-radius','0');
  7. $('.row').css('margin','0');
  8. $('#resources').before('<div style="width:100%;border-radius:0px;" onclick="'+$('#download').attr('onclick')+'" class="btn btn-success" ><i class="fa fa-arrow-circle-o-down"></i> Télécharger</div>');
  9. $('.col-md-3,.col-md-9').attr('style','width:20%;float:left;padding:0;');
  10. $('.col-md-9').attr('style','width:70%;float:left;padding:0;');
  11. }
  12. if($.urlParam('sidebar') =="0"){
  13. $('#resourceMenu').hide();
  14. $('#resourceContent').removeClass('col-md-9').addClass('col-md-12');
  15. }
  16. var init = 'init_'+$.page();
  17. if(window[init]!=null) window[init]();
  18. if($.page()=='' ) init_index();
  19. Dropzone.autoDiscover = false;
  20. });
  21. function init_plugin(){
  22. search_plugin(function(){
  23. $('.toggle').change(function(){
  24. var input = $('input',this);
  25. var value = input.prop('checked');
  26. $.action({
  27. action : 'change_plugin_state',
  28. plugin : $(this).closest('li').attr('data-id'),
  29. state:value?1:0
  30. },function(r){},
  31. function(r){
  32. input.prop('checked',!value);
  33. });
  34. });
  35. });
  36. }
  37. function init_index(){
  38. search_sketch();
  39. $('#label').enter(function(){
  40. create_sketch();
  41. });
  42. $('#importJsonSketch').dropzone({
  43. url : 'action.php?action=import_sketch',
  44. complete : function(useless){
  45. if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
  46. search_sketch();
  47. }
  48. },
  49. success: function(file, response){
  50. if(response.errors.length>0) alert(response.errors.join(','));
  51. },
  52. sending : function(file, xhr, formData){
  53. formData.append('from','file');
  54. }
  55. });
  56. }
  57. //COMPONENT
  58. function init_component(){
  59. search_component();
  60. /*
  61. $('#imageUpload').upzone({
  62. url : 'action.php?action=upload_component_image',
  63. id : $('#sketch').attr('data-id'),
  64. success : function(server){
  65. $('#imageUpload').attr('src',server);
  66. }
  67. });
  68. */
  69. $('#imageUpload').dropzone({
  70. url : 'action.php?action=upload_component_image',
  71. success : function(useless,server){
  72. if(server.error != null)
  73. return alert(server.error);
  74. $('#imageUpload').attr('src',server.thumb);
  75. },
  76. sending : function(file, xhr, formData){
  77. formData.append('id', $('#sketch').attr('data-id'));
  78. }
  79. });
  80. $('#label').autocomplete({
  81. source : 'http://hack.idleman.fr/action.php?action=find_component',
  82. minLength: 4,
  83. select: function( event, ui ) {
  84. $.setForm('#editComponent',ui.item.value);
  85. $('#imageUpload').attr('src',ui.item.value.image);
  86. return false;
  87. }
  88. }).data("uiAutocomplete")._renderItem = function (ul, item) {
  89. return $("<li />")
  90. .data("item.autocomplete", item)
  91. .append("<a><img style='height:50px;width:auto;' src='" + item.value.image + "' /> " + item.label + "</a>")
  92. .appendTo(ul);
  93. };
  94. };
  95. /*PLUGINS*/
  96. // SEARCH
  97. function search_plugin(callback){
  98. $('#plugins').fill({action:'search_plugin'},function(){
  99. if(callback!=null) callback();
  100. });
  101. }
  102. /*COMPONENTS*/
  103. function save_component(){
  104. var data = $.getForm('#editComponent');
  105. if($('#imageUpload').attr('src').substring(0,10)=='data:image')
  106. data.image = $('#imageUpload').attr('src');
  107. $.action(data,function(r){
  108. search_component();
  109. });
  110. }
  111. function search_component(){
  112. $('#components').fill({action:'search_component'});
  113. }
  114. function edit_component(element){
  115. var data = {action : 'edit_component'};
  116. $('#editComponent input').val('');
  117. $('#imageUpload').attr('src','img/default_image.png');
  118. if(element!=null){
  119. var line = $(element).closest('tr');
  120. data.id = line.attr('data-id');
  121. }
  122. $.action(data,function(r){
  123. $('#editComponent').modal('show');
  124. $.setForm('#editComponent',r);
  125. $('#imageUpload').attr('src',r.image);
  126. $('#editComponent').attr('data-id',r.id);
  127. });
  128. }
  129. function delete_component(element){
  130. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  131. var line = $(element).closest('tr');
  132. $.action({action : 'delete_component',id : line.attr('data-id')},function(r){
  133. line.remove();
  134. });
  135. }
  136. //SKETCH
  137. function init_sketch(){
  138. search_resources(function(){
  139. var resource = $.hashData('resource');
  140. resource = resource == '' ? $('#resources a:visible():eq(1)').attr('data-id') : resource;
  141. select_resource(resource);
  142. });
  143. $('#importResource i').dropzone({
  144. url : 'action.php?action=import_resource',
  145. complete : function(useless,server){
  146. if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
  147. search_resources();
  148. }
  149. },
  150. sending : function(file, xhr, formData){
  151. formData.append('id', $('#sketch').attr('data-id'));
  152. }
  153. });
  154. }
  155. function search_sketch(){
  156. $('#sketchs').fill({action:'search_sketch'});
  157. }
  158. function create_sketch(){
  159. $.action($.getForm('#editSketch'),function(r){
  160. window.location = 'sketch.php?id='+r.id;
  161. });
  162. }
  163. function import_sketch(){
  164. var data = $.getForm('#importSketch');
  165. data.from = 'url';
  166. $.action(data,function(){
  167. search_sketch();
  168. });
  169. }
  170. function delete_sketch(element){
  171. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  172. var line = $(element).closest('tr');
  173. $.action({action : 'delete_sketch',id : line.attr('data-id')},function(r){
  174. line.remove();
  175. });
  176. }
  177. function save_sketch_title(element){
  178. $.action({action : 'save_sketch_title',label : $(element).val(),id:$('#sketch').attr('data-id')});
  179. }
  180. function toggle_share_sketch(element){
  181. var button = $(element).find('i');
  182. var nextState = !button.hasClass('fa-eye');
  183. $.action({action:'toggle_share_sketch',state:(nextState?1:0),id:$('#sketch').attr('data-id')},function(){
  184. button.removeClass('fa-eye').removeClass('fa-eye-slash');
  185. button.addClass((nextState?'fa-eye':'fa-eye-slash'));
  186. $(element).attr('title','Rendre '+(nextState?'Privé':'Public'));
  187. });
  188. }
  189. function toggle_embed_sketch(){
  190. $('#embedModal').modal('show');
  191. get_embeded_code();
  192. }
  193. function get_embeded_code(){
  194. var url = window.location.protocol+'//'+window.location.host+window.location.pathname+window.location.search;
  195. url+= '&embeded=1';
  196. if(!$('#enableSideBar').prop('checked'))
  197. url+= '&sidebar=0';
  198. url+= window.location.hash;
  199. var html = '<a href="'+window.location+'"><small>Voir en taille réelle<small></a><br/>';
  200. html += '<iframe frameborder="0" width="100%" align="center" height="400px" src="'+url+'"></iframe>';
  201. $('#embedModal textarea').val(html);
  202. $("#embedModal textarea").focus(function() {
  203. var $this = $(this);
  204. $this.select();
  205. $this.mouseup(function() {
  206. $this.unbind("mouseup");
  207. return false;
  208. });
  209. });
  210. }
  211. //RESOURCE
  212. function add_resource(selected){
  213. $('#resources a').removeClass('active');
  214. $('#editResourceMeta').modal('show');
  215. if(selected!=null) $('#editResourceMeta select').val(selected);
  216. $('#label').val('').focus();
  217. $('#resource').attr('data-id','');
  218. }
  219. function save_resource(){
  220. var data = $.getForm('#editResourceMeta');
  221. data.sketch = $('#sketch').attr('data-id');
  222. $.action(data,function(r){
  223. $('#editResourceMeta input').val('');
  224. $('#editResourceMeta').attr('data-id','');
  225. search_resources(select_resource(r.id));
  226. });
  227. }
  228. function search_resources(callback){
  229. var id = $('#sketch').attr('data-id');
  230. $('#resources').fill({id:id,action:'search_resources'},function(){
  231. if(callback!=null) callback();
  232. });
  233. }
  234. function edit_resource(element,event){
  235. event.stopPropagation();
  236. var line = $(element).closest('a');
  237. $.action({action:'edit_resource',id:line.attr('data-id')},function(r){
  238. $('#editResourceMeta').modal('show');
  239. $.setForm('#editResourceMeta',r);
  240. $('#editResourceMeta').attr('data-id',r.id);
  241. });
  242. }
  243. function delete_resource(element,event){
  244. event.stopPropagation();
  245. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  246. var line = $(element).closest('a');
  247. $.action({action : 'delete_resource',id : line.attr('data-id')},function(r){
  248. line.remove();
  249. });
  250. }
  251. function select_resource(id){
  252. $.hashData({resource:id});
  253. load_resource();
  254. }
  255. function load_resource(){
  256. var id = $.hashData('resource');
  257. var line = $('[data-id="'+id+'"]');
  258. if(line.attr('data-id')==null) return;
  259. $('.preloader').show();
  260. $('#resource p').html('');
  261. $.action({action:'edit_resource',id:line.attr('data-id')},function(r){
  262. $('.preloader').hide();
  263. $('#resources a').removeClass('active');
  264. line.addClass('active');
  265. $('#resource').attr('data-id',r.id);
  266. $('#resource h2').html(r.label);
  267. $('#resource p').html(r.content);
  268. $('#resource textarea:eq(0)').focus();
  269. if(r.upload !=null){
  270. var data = {};
  271. data.url = r.upload.url;
  272. data.success = function(useless,r){
  273. if(r.errors && r.errors.length!=0){
  274. alert('Erreur : '+r.errors.join(','));
  275. }else{
  276. eval(r.upload.callback);
  277. }
  278. }
  279. data.sending = function(file, xhr, formData){
  280. formData.append('id', $('#resource').attr('data-id'));
  281. }
  282. data.createImageThumbnails = false;
  283. $(r.upload.element).dropzone(data);
  284. }
  285. if(r.code != null){
  286. var editor = CodeMirror.fromTextArea($('#resource > p > textarea:eq(0)').get(0),r.code);
  287. console.log(editor);
  288. editor.on("change", function() {
  289. var data ={height:800};
  290. var wrap = editor.getWrapperElement();
  291. var approp = editor.getScrollInfo().height > data.height ? data.height+"px" : "auto";
  292. if (wrap.style.height != approp) {
  293. wrap.style.height = approp;
  294. editor.refresh();
  295. }
  296. });
  297. editor.on("blur", function(cm,obj){
  298. var data = {};
  299. data.content = cm.getValue();
  300. data.action='save_resource_content';
  301. data.id = $('#resource').attr('data-id');
  302. $.action(data,function(r){
  303. });
  304. });
  305. }
  306. if(r.callback !=null){
  307. eval(r.callback);
  308. }
  309. });
  310. }
  311. /*FILE*/
  312. function init_file(){
  313. search_file();
  314. };
  315. function search_file(){
  316. $('#files').fill({action:'search_file',id:$('#resource').attr('data-id')});
  317. }
  318. function delete_file(element){
  319. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  320. var line = $(element).closest('tr');
  321. $.action({action : 'delete_file',resource : $('#resource').attr('data-id'),id : line.attr('data-id')},function(r){
  322. line.remove();
  323. });
  324. }
  325. /*PART*/
  326. function init_part(){
  327. $('#label').autocomplete({
  328. source : 'action.php?action=autocomplete_part',
  329. minLength: 2,
  330. select: function( event, ui ) {
  331. save_part(ui.item.value);
  332. return false;
  333. }
  334. }).data("uiAutocomplete")._renderItem = function (ul, item) {
  335. return $("<li />")
  336. .data("item.autocomplete", item)
  337. .append("<a><img style='height:50px;width:auto; float:left;' src='" + item.value.image + "' /> " + item.label + " <br/></a><small>"+item.value.brand+"</small>")
  338. .appendTo(ul);
  339. };
  340. search_part();
  341. };
  342. function search_part(){
  343. $('#parts').fill({action:'search_part',id:$('#resource').attr('data-id')});
  344. }
  345. function save_part(model){
  346. var data = $.getForm('#partForm');
  347. if (model!=null) data.model = model.id;
  348. data.resource = $('#resource').attr('data-id');
  349. $.action(data,function(r){
  350. search_part();
  351. clear_part();
  352. });
  353. }
  354. function clear_part(){
  355. $('#partForm input').val('');
  356. $('#partForm').attr('data-id','');
  357. }
  358. /*
  359. function edit_part(element){
  360. var line = $(element).closest('tr');
  361. $.action({action:'edit_part',id:line.attr('data-id')},function(r){
  362. $.setForm('#partForm',r);
  363. $('#partForm').attr('data-id',r.id);
  364. });
  365. }
  366. */
  367. function delete_part(element){
  368. if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
  369. var line = $(element).closest('tr');
  370. $.action({action : 'delete_part',id : line.attr('data-id')},function(r){
  371. line.remove();
  372. });
  373. }