aboutsummaryrefslogtreecommitdiff
path: root/static/js/posts.js
blob: a6963de59ba5bc8ccf14c294c16ba303d4b646f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
function newpost()
{
    var el = document.getElementById("newpostbtn")
    var state = el.getAttribute("state")
    if(state = "0")
    {
        el.style="display:none;"
        el.setAttribute("state", "1")
        document.getElementById("newpost").style = "display: block;";        
    }
    else
    {
        el.style="display:block;"
        el.setAttribute("state", "0")
        document.getElementById("newpost").style = "display: hidden;";        
    }
}

function getMIMEType(type)
{
    re = /\/.+/g
    return type.replace(re, "")
}

function shortURL(actorName, url)
{
    var check = url.replace(actorName + "/", "")
    re = /.+\//g;
    temp = re.exec(url)

    if(stripTransferProtocol(temp[0]) == stripTransferProtocol(actorName) + "/")
    {
        var short = url.replace("https://", "");
        short = short.replace("http://", "");
        short = short.replace("www.", "");

        var re = /^.{3}/g;

        var u = re.exec(short);

        re = /\w+$/g;              

        u =  re.exec(short);

        return u;            
    }else{

        var short = url.replace("https://", "");
        short = short.replace("http://", "");
        short = short.replace("www.", "");

        var re = /^.{3}/g;

        var u = re.exec(short);

        re = /\w+$/g;              

        u =  re.exec(short);


        replace = short.replace(/\/+/g, " ")
        replace = replace.replace(u, " ").trim()
        re = /\w+$/;
        v = re.exec(replace)

        v = "f" + v[0] + "-" + u

        return v;                        
    }
}

function shortImg(url)
{
    var u = url;
    if(url.length > 26)
    {
        var re = /^.{26}/g;

        u = re.exec(url);

        re = /\..+$/g;

        var v = re.exec(url);

        u += "(...)" + v;
    }
    return u;        
}            

function convertSize(size)
{
    var convert = size / 1024.0;
    if(convert > 1024)
    {
        convert = convert / 1024.0
        convert = convert.toFixed(2) + " MB"
    }
    else
    {
        convert = convert.toFixed(2) + " KB"
    }

    return convert
}

function getBoardId(url)
{
    var re = /\/([^/\n]+)(.+)?/gm
    var matches = re.exec(url);
    return matches[1]
}

function convertContent(actorName, content, opid)
{
    var re = /(>>)(https:\/\/|http:\/\/)?(www\.)?.+\/\w+/gm;
    var match = content.match(re);
    var newContent = content;
    if(match)
    {
        match.forEach(function(quote, i){
            var link = quote.replace('>>', '')
            var isOP = ""
            if(link == opid)
            {
                isOP = " (OP)";
            }

            newContent = newContent.replace(quote, '<a class="reply" title="' + link +  '" href="'+ (actorName) + "/" + shortURL(actorName, opid)  +  '#' + shortURL(actorName, link) + '";">>>' + shortURL(actorName, link)  + isOP + '</a>');

        })            
    }
    
    re =  /^>.+/gm;

    match = newContent.match(re);
    if(match)
    {
        match.forEach(function(quote, i) {
            newContent = newContent.replace(quote, '<span class="quote">' + quote + '</span>');
        })
    }
    
    return newContent
}

function closeReply()
{
    document.getElementById("reply-box").style.display = "none";
    document.getElementById("reply-comment").value = "";        
}


function previous(actorName, page)
{
    var prev = parseInt(page) - 1;
    if(prev < 0)
        prev = 0;
    window.location.href = "/" + actorName + "/" + prev;
}

function next(actorName, totalPage, page)
{
    var next = parseInt(page) + 1;
    if(next > parseInt(totalPage))
        next = parseInt(totalPage);
    window.location.href = "/" + actorName + "/" + next;
}

function quote(actorName, opid, id)
{
    var box = document.getElementById("reply-box");
    var header = document.getElementById("reply-header");
    var comment = document.getElementById("reply-comment");
    var inReplyTo = document.getElementById("inReplyTo-box");      

    var w = window.innerWidth / 2 - 200;
    var h = document.getElementById(id + "-content").offsetTop - 448;

    box.setAttribute("style", "display: block; position: absolute; width: 400px; height: 550px; z-index: 9; top: " + h + "px; left: " + w + "px; padding: 5px;");


    if (inReplyTo.value != opid)
        comment.value = "";
    
    header.innerText = "Replying to Thread No. " + shortURL(actorName, opid);
    inReplyTo.value = opid;

    if(comment.value != "")
        comment.value += "\n>>" + id;
    else
        comment.value += ">>" + id;

    dragElement(header);            

}  

function dragElement(elmnt) {
    var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
    
    elmnt.onmousedown = dragMouseDown;

    function dragMouseDown(e) {
        e = e || window.event;
        e.preventDefault();
        // get the mouse cursor position at startup:
        pos3 = e.clientX;
        pos4 = e.clientY;
        document.onmouseup = closeDragElement;
        // call a function whenever the cursor moves:
        document.onmousemove = elementDrag;
    }

    function elementDrag(e) {
        e = e || window.event;
        e.preventDefault();
        // calculate the new cursor position:
        pos1 = pos3 - e.clientX;
        pos2 = pos4 - e.clientY;
        pos3 = e.clientX;
        pos4 = e.clientY;
        // set the element's new position:
        elmnt.parentElement.style.top = (elmnt.parentElement.offsetTop - pos2) + "px";
        elmnt.parentElement.style.left = (elmnt.parentElement.offsetLeft - pos1) + "px";
    }

    function closeDragElement() {
        // stop moving when mouse button is released:
        document.onmouseup = null;
        document.onmousemove = null;
    }
}

function stripTransferProtocol(value){
    var re = /(https:\/\/|http:\/\/)?(www.)?/

    return value.replace(re, "")
}