UNPKG

2.36 kBJavaScriptView Raw
1/*
2 * pub-ux.js
3 * browserify entry point
4 * connect/disconnect socket.io and inject pub UI into dom
5 *
6 * TODO: fix /pub/ button logic to handle non-pub urls ending in /pub/
7 * copyright 2015, Jurgen Leschner - github.com/jldec - MIT license
8*/
9
10var debug = require('debug')('pub:ux');
11
12if (window.io) {
13
14 debug('socket:connect');
15 var socket = io();
16
17 socket.on('reload', function() {
18 // in-browser generator case, just notify
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 // logic supports static at root or not, or pub-server /pub/ editor
71 // page param used in pub-preview.js to open editor on the proper page
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});