1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var debug = require('debug')('pub:ux');
|
11 |
|
12 | if (window.io) {
|
13 |
|
14 | debug('socket:connect');
|
15 | var socket = io();
|
16 |
|
17 | socket.on('reload', function() {
|
18 |
|
19 | if (window.generator) {
|
20 | return window.generator.emit('notify', 'save');
|
21 | }
|
22 | debug('socket:reload');
|
23 | location.reload();
|
24 | });
|
25 |
|
26 | $(window).on('beforeunload', function() {
|
27 | debug('socket:disconnect');
|
28 | socket.disconnect();
|
29 | })
|
30 |
|
31 | } else console.log('no socket.io')
|
32 |
|
33 | $(function(){
|
34 |
|
35 | var style =
|
36 | { 'position':'fixed',
|
37 | 'z-index':'9999999',
|
38 | 'opacity':'0.5',
|
39 | 'font-family': '"Helvetica Neue",Tahoma,Arial,sans-serif',
|
40 | 'font-weight': '400',
|
41 | 'font-size': '18px',
|
42 | 'line-height': '20px',
|
43 | 'height':'21px',
|
44 | 'top':'0',
|
45 | 'right':'0',
|
46 | 'background-color':'#555',
|
47 | 'color':'#fff',
|
48 | 'border-bottom-left-radius':'10px',
|
49 | 'text-align':'right',
|
50 | 'padding':'0 2px 0 5px',
|
51 | 'cursor':'pointer' };
|
52 |
|
53 | var $b;
|
54 |
|
55 | if (window.parent.location.pathname.match(/\/pub\/$/)) {
|
56 | $.pubEditor = true;
|
57 | $b = $('<div class="pub-button" title="Close editor">Close</div>').css(style);
|
58 | $('body').prepend($b);
|
59 | $b.on('click', function(){
|
60 | var contentHref = location.pathname + location.search + location.hash;
|
61 | var staticRoot = window.generator && window.generator.opts.staticRoot;
|
62 | if (staticRoot && contentHref.slice(0, staticRoot.length) !== staticRoot) {
|
63 | contentHref = staticRoot + contentHref;
|
64 | }
|
65 | window.parent.location = contentHref;
|
66 | });
|
67 | }
|
68 | else {
|
69 | $.pubEditor = false;
|
70 |
|
71 |
|
72 | $b = $('<div class="pub-button" title="Edit">Edit</div>').css(style);
|
73 | $('body').prepend($b);
|
74 | $b.on('click', function(){
|
75 | var pubRef = window.pubRef || {};
|
76 | var contentHref = (pubRef.href || location.pathname) + location.search + location.hash;
|
77 | var editorHref = (pubRef.relPath || '') + '/pub/?page=' + encodeURIComponent(contentHref);
|
78 | location = editorHref;
|
79 | });
|
80 | }
|
81 |
|
82 | });
|