summaryrefslogtreecommitdiff
path: root/views/new-post.php
blob: 7059c5e7c6a57271b837faae2091cd51b1280436 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
  <div class="narrow">
    <?= partial('partials/header') ?>

      <form role="form" style="margin-top: 20px;" id="note_form">

        <div id="reply">
          <div class="reply-section hidden">
            <div class="form-group has-feedback">
              <label for="note_in_reply_to">Reply To (a URL you are replying to)</label>
              <input type="text" id="note_in_reply_to" value="<?= $this->in_reply_to ?>" class="form-control">
              <span class="loading hidden glyphicon glyphicon-refresh glyphicon-spin form-control-feedback"></span>
            </div>
            <div class="reply-context hidden">
              <div class="reply-author">
                <img src="" width="48" class="author-img"> 
              </div>
              <div class="reply-content">
                <img src="" class="post-img hidden">
                <div class="author"><span class="name"></span> <span class="url"></span></div>
                <h4 class="post-name hidden"></h4>
                <span class="content"></span>
              </div>
            </div>
          </div>
          <a href="" id="expand-reply" class="btn btn-xs btn-info">Reply</a>
        </div>

        <div class="form-group">
          <div id="note_content_remaining" class="pcheck206"><img src="/images/twitter.ico"> <span>140</span></div>
          <label for="note_content">Content</label>
          <textarea id="note_content" value="" class="form-control" style="height: 4em;"></textarea>
        </div>

        <div class="form-group" id="form_tags">
          <label for="note_category">Tags</label>
          <input type="text" id="note_category" value="" class="form-control" placeholder="e.g. web, personal">
        </div>

        <div class="form-group" id="form_slug">
          <label for="note_slug">Slug</label>
          <input type="text" id="note_slug" value="" class="form-control">
        </div>

        <div class="form-group">
          <label for="note_photo">Photo</label>
          <input type="file" name="note_photo" id="note_photo" accept="image/*">
          <a href="javascript:switchToManualPhotoURL();" id="note_manual_photo">enter photo url</a>
          <a href="javascript:addPhotoURL();" class="hidden" id="add_photo">add photo</a>
          <br>
          <div id="photo_preview_container" class="hidden">
            <img src="" id="photo_preview" style="max-width: 300px; max-height: 300px;">
            <div>
              <button type="button" class="btn btn-danger btn-sm" id="remove_photo"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Remove image</button>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label for="note_syndicate-to">Syndicate <a href="javascript:reload_syndications()">(refresh list)</a></label>
          <div id="syndication-container">
            <?php
            if($this->syndication_targets) {
              echo '<ul>';
              foreach($this->syndication_targets as $syn) {
                echo '<li>'
                 . '<button data-syndicate-to="'.(isset($syn['uid']) ? htmlspecialchars($syn['uid']) : htmlspecialchars($syn['target'])).'" class="btn btn-default btn-block">'
                   . ($syn['favicon'] ? '<img src="'.htmlspecialchars($syn['favicon']).'" width="16" height="16"> ' : '')
                   . htmlspecialchars($syn['target'])
                 . '</button>'
               . '</li>';
              }
              echo '</ul>';
            } else {
              ?><div class="bs-callout bs-callout-warning">No syndication targets were found on your site.
              You can provide a <a href="/docs#syndication">list of supported syndication targets</a> that will appear as checkboxes here.</div><?php
            }
            ?>
          </div>
        </div>

        <div class="form-group">
          <label for="note_location">Location</label>
          <input type="checkbox" id="note_location_chk" value="">
          <img src="/images/spinner.gif" id="note_location_loading" style="display: none;">

          <input type="text" id="note_location_msg" value="" class="form-control" placeholder="" readonly="readonly">
          <input type="hidden" id="note_location">
          <input type="hidden" id="location_enabled" value="<?= $this->location_enabled ?>">

          <div id="note_location_img" style="display: none;">
            <img src="" height="180" id="note_location_img_wide" class="img-responsive">
            <img src="" height="320" id="note_location_img_small" class="img-responsive">
          </div>
        </div>

        <button class="btn btn-success" id="btn_post">Post</button>
      </form>

      <div class="alert alert-success hidden" id="test_success"><strong>Success! We found a Location header in the response!</strong><br>Your post should be on your website now!<br><a href="" id="post_href">View your post</a></div>
      <div class="alert alert-danger hidden" id="test_error"><strong>Your endpoint did not return a Location header.</strong><br>See <a href="/creating-a-micropub-endpoint">Creating a Micropub Endpoint</a> for more information.</div>


      <div id="last_request_container" style="display: none;">
        <h4>Request made to your Micropub endpoint</h4>
        <pre id="test_request" style="width: 100%; min-height: 140px;"></pre>
      </div>

      <?php if($this->test_response): ?>
        <h4>Last response from your Micropub endpoint <span id="last_response_date">(<?= relative_time($this->response_date) ?>)</span></h4>
      <?php endif; ?>
      <pre id="test_response" class="<?= $this->test_response ? '' : 'hidden' ?>" style="width: 100%; min-height: 240px;"><?= htmlspecialchars($this->test_response) ?></pre>

      <hr>
      <div style="text-align: right;">
        <a href="/add-to-home?start">Add to Home Screen</a>
      </div>
  </div>

<style type="text/css">

#reply {
  margin-bottom: 1em;
}

#note_content_remaining {
  float: right;
  font-size: 0.8em;
  font-weight: bold;
}

.pcheck206 { color: #6ba15c; } /* tweet fits within the limit even after adding RT @username */
.pcheck207 { color: #c4b404; } /* danger zone, tweet will overflow when RT @username is added */
.pcheck200,.pcheck208 { color: #59cb3a; } /* exactly fits 140 chars, both with or without RT */
.pcheck413 { color: #a73b3b; } /* over max tweet length */

.reply-context {
  display: flex;
  flex-direction: row;
  padding: 4px;
  margin: 0 3em;
  border: 1px #ccc solid;
  border-radius: 4px;
  background: #f4f4f4;
  max-height: 140px;
  overflow-y: hidden;
}
.reply-context .reply-content {
  flex: 1 0;
}
.reply-context img.author-img {
  border-radius: 4px;
  width: 48px;
  margin-right: 4px;
}
.reply-context .author {
  color: #777;
  font-weight: bold;
  font-size: 0.9em;
}
.reply-context .author .url {
  color: #aaa;
}
.reply-context img.post-img {
  float: right;
  width: 200px;
}

</style>

<script>
function saveNoteState() {
  var state = {
    content: $("#note_content").val(),
    inReplyTo: $("#note_in_reply_to").val(),
    category: $("#note_category").val(),
    slug: $("#note_slug").val(),
    photo: $("#note_photo_url").val()
  };
  state.syndications = [];
  $("#syndication-container button.btn-info").each(function(i,btn){
    state.syndications[$(btn).data('syndicate-to')] = 'selected';
  });
  console.log("saving",state);
  localforage.setItem('current-note', state);
}

function restoreNoteState() {
  localforage.getItem('current-note', function(err,note){
    if(note) {
      $("#note_content").val(note.content);
      $("#note_in_reply_to").val(note.inReplyTo);
      $("#note_category").val(note.category);
      $("#note_slug").val(note.slug);
      if(note.photo) {
        replacePhotoWithPhotoURL(note.photo);
      }
      if(note.inReplyTo) {
        expandReplySection();
      }
      $("#syndication-container button").each(function(i,btn){
        if($(btn).data('syndicate-to') in note.syndications) {
          $(btn).addClass('btn-info');
        }
      });
      $("#note_content").change();
      activateTokenField();
    } else {
      activateTokenField();
    }
  });  
}

function replacePhotoWithPhotoURL(url) {
  $("#note_photo").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control">');
  $(".note_photo_url").val(url);
  $("#note_photo").remove();  
  $("#photo_preview").attr("src", url);
  $("#photo_preview_container").removeClass("hidden");
  $("#note_manual_photo").addClass("hidden");
}

function switchToManualPhotoURL() {
  $("#note_photo").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control">');
  $("#note_photo").remove();  
  $("#note_photo_url").change(function(){
    $("#photo_preview").attr("src", $(this).val());
    $("#photo_preview_container").removeClass("hidden");
  });
  $("#note_manual_photo").addClass("hidden");
  $("#add_photo").removeClass("hidden");
}

function addPhotoURL() {
  $(".note_photo_url:last").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control" style="margin-top:2px;">');
  if($(".note_photo_url").length == 4) {
    $("#add_photo").remove();
  }
}

function expandReplySection() {
  $("#expand-reply").click();
  $("#note_in_reply_to").change();  
}

function activateTokenField() {
  $("#note_category").tokenfield({
    createTokensOnBlur: true,
    beautify: true
  });  
}

$(function(){

  var userHasSetCategory = false;

  var hasMediaEndpoint = <?= $this->media_endpoint ? 'true' : 'false' ?>;

  $("#note_content, #note_category, #note_in_reply_to, #note_slug, #note_photo_url").on('keyup change', function(e){
    saveNoteState();
  });

  $("#note_content").on('keyup', function(){
    var scrollHeight = document.getElementById("note_content").scrollHeight;
    var currentHeight = parseInt($("#note_content").css("height"));
    if(Math.abs(scrollHeight - currentHeight) > 20) {
      $("#note_content").css("height", (scrollHeight+30)+"px");
    }
  });

  $("#expand-reply").click(function(){
    $('.reply-section').removeClass('hidden');
    $(this).addClass('hidden');
    return false;
  });

  // Preview the photo when one is chosen
  $("#photo_preview_container").addClass("hidden");
  $("#note_photo").on("change", function(e){
    // If the user has a media endpoint, upload the photo to it right now
    if(hasMediaEndpoint) {
      // TODO: add loading state indicator here
      console.log("Uploading file to media endpoint...");
      var formData = new FormData();
      formData.append("null","null");
      formData.append("photo", e.target.files[0]);
      var request = new XMLHttpRequest();
      request.open("POST", "/micropub/media");
      request.onreadystatechange = function() {
        if(request.readyState == XMLHttpRequest.DONE) {
          try {
            var response = JSON.parse(request.responseText);
            if(response.location) {
              // Replace the file upload form with the URL
              replacePhotoWithPhotoURL(response.location);
              saveNoteState();
            } else {
              console.log("Endpoint did not return a location header", response);
            }
          } catch(e) {
            console.log(e);
          }
        }
      }
      request.send(formData);
    } else {
      $("#photo_preview").attr("src", URL.createObjectURL(e.target.files[0]) );
      $("#photo_preview_container").removeClass("hidden");
    }
  });
  $("#remove_photo").on("click", function(){
    $("#note_photo").val("");
    $(".note_photo_url").val("");
    $("#photo_preview").attr("src", "" );
    $("#photo_preview_container").addClass("hidden");
    saveNoteState();
  });

  $("#note_content").on('change keyup', function(e){
    var text = $("#note_content").val();
    var tweet_length = tw_text_proxy(text).length;
    var tweet_check = tw_length_check(text, 140, "<?= $this->user->twitter_username ?>");
    var remaining = 140 - tweet_length;
    $("#note_content_remaining span").html(remaining);
    $("#note_content_remaining").removeClass("pcheck200 pcheck206 pcheck207 pcheck208 pcheck413");
    $("#note_content_remaining").addClass("pcheck"+tweet_check);

    // If the user didn't enter any categories, add them from the post
    // if(!userHasSetCategory) {
    //   var tags = $("#note_content").val().match(/#[a-z][a-z0-9]+/ig);
    //   if(tags) {
    //     $("#note_category").val(tags.map(function(tag){ return tag.replace('#',''); }).join(", "));
    //   }
    // }
  });

  $("#note_in_reply_to").on('change', function(){
    var reply_to = $("#note_in_reply_to").val();

    if(reply_to == "") {
      $(".reply-section").addClass("hidden");
      $(".reply-context").addClass("hidden");
      $("#expand-reply").removeClass("hidden");
      return;
    }

    $(".reply-section .loading").removeClass("hidden");
    $.get("/reply/preview", {url:reply_to}, function(data){

      if(data.canonical_reply_url != reply_to) {
        $("#note_in_reply_to").val(data.canonical_reply_url);
      }
      // var category = csv_to_array($("#note_category").val());
      // for(var i in data.entry.category) {
      //   if($.inArray(data.entry.category[i], category) == -1) {
      //     category.push(data.entry.category[i]);
      //   }
      // }
      // $("#note_category").val(category.join(", "));

      if($("#note_content").val() == "" && data.mentions) {
        var mentions = '';
        for(var i in data.mentions) {
          mentions += '@'+data.mentions[i]+' ';
        }
        $("#note_content").val(mentions);
      }

      $(".reply-context .content").text(data.entry.content.text);
      if(data.entry.name) {
        $(".reply-context .post-name").text(data.entry.name).removeClass('hidden');
      } else {
        $(".reply-context .post-name").addClass('hidden');
      }
      if(data.entry.author) {
        $(".reply-context .author .name").text(data.entry.author.name);
        $(".reply-context .author .url").text(data.entry.author.url);
        $(".reply-context img.author-img").attr('src', data.entry.author.photo);
      }
      if(data.entry.photo) {
        $(".reply-context img.post-img").attr('src', data.entry.photo[0]).removeClass('hidden');
      } else {
        $(".reply-context img.post-img").addClass('hidden');
      }

      $(".reply-section .loading").addClass("hidden");
      $(".reply-context").removeClass("hidden");
    });
  });

  $("#note_category").on('keydown keyup', function(){
    userHasSetCategory = true;
  });
  $("#note_category").on('change', function(){
    if($("#note_category").val() == "") {
      userHasSetCategory = false;
    }
  });

  // When the reply URL is in the query string, or loads from localstorage, make sure
  // to run the event handlers to expand the reply section
  if($("#note_in_reply_to").val() != "") {
    expandReplySection();
  }

  $("#btn_post").click(function(){

    // Collect all the syndication buttons that are pressed
    var syndications = [];
    $("#syndication-container button.btn-info").each(function(i,btn){
      syndications.push($(btn).data('syndicate-to'));
    });

    var category = csv_to_array($("#note_category").val());

    var formData = new FormData();
    if(v=$("#note_content").val()) {
      formData.append("content", v);
    }
    if(v=$("#note_in_reply_to").val()) {
      formData.append("in-reply-to", v);
    }
    if(v=$("#note_location").val()) {
      formData.append("location", v);
    }
    if(category.length > 0) {
      for(var i in category) {
        formData.append("category[]", category[i]);
      }
    }
    if(syndications.length > 0) {
      for(var i in syndications) {
        formData.append("syndicate-to[]", syndications[i]);
      }
    }
    if(v=$("#note_slug").val()) {
      formData.append("<?= $this->user->micropub_slug_field ?>", v);
    }

    // Add either the photo as a file, or the photo URL depending on whether the user has a media endpoint
    if(document.getElementById("note_photo") && document.getElementById("note_photo").files[0]) {
      formData.append("photo", document.getElementById("note_photo").files[0]);
    } else if($(".note_photo_url").val()) {
      $(".note_photo_url").each(function(){
        if($(this).val()) {
          formData.append("photo[]", $(this).val());
        }
      });
    }

    // Need to append a placeholder field because if the file size max is hit, $_POST will
    // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
    // This will be stripped by Quill before it's sent to the Micropub endpoint
    formData.append("null","null");

    var request = new XMLHttpRequest();
    request.open("POST", "/micropub/multipart");
    request.onreadystatechange = function() {
      if(request.readyState == XMLHttpRequest.DONE) {
        // console.log(request.responseText);
        try {
          var response = JSON.parse(request.responseText);
          localforage.removeItem('current-note');
          if(response.location) {
            window.location = response.location;
            // console.log(response.location);
          } else {
            $("#test_response").html(response.response).removeClass('hidden');
            $("#test_success").addClass('hidden');
            $("#test_error").removeClass('hidden');
          }
        } catch(e) {
          $("#test_success").addClass('hidden');
          $("#test_error").removeClass('hidden');
        }
        $("#btn_post").removeClass("loading disabled").text("Post");
      }
    }
    $("#btn_post").addClass("loading disabled").text("Working...");
    request.send(formData);

    /*
    $.post("/micropub/multipart", {
      content: $("#note_content").val(),
      'in-reply-to': $("#note_in_reply_to").val(),
      location: $("#note_location").val(),
      category: category,
      slug: $("#note_slug").val(),
      'syndicate-to': syndications
    }, function(data){
      var response = JSON.parse(data);

      if(response.location != false) {
        $("#note_form").slideUp(200, function(){
          $(window).scrollTop($("#test_success").position().top);
        });

        $("#test_success").removeClass('hidden');
        $("#test_error").addClass('hidden');
        $("#post_href").attr("href", response.location);

        $("#note_content").val("");
        $("#note_in_reply_to").val("");
        $("#note_category").val("");
        $("#note_slug").val("");

      } else {
        $("#test_success").addClass('hidden');
        $("#test_error").removeClass('hidden');
      }

      $("#last_response_date").html("(just now)");
      $("#test_request").html(response.request);
      $("#last_request_container").show();
      $("#test_response").html(response.response);
    });
    */

    return false;
  });

  function location_error(msg) {
    $("#note_location_msg").val(msg);
    $("#note_location_chk").removeAttr("checked");
    $("#note_location_loading").hide();
    $("#note_location_img").hide();
    $("#note_location_msg").removeClass("img-visible");
  }

  var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";

  function fetch_location() {
    $("#note_location_loading").show();

    navigator.geolocation.getCurrentPosition(function(position){

      $("#note_location_loading").hide();
      var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
      $("#note_location_msg").val(geo);
      $("#note_location").val(geo);
      $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
      $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
      $("#note_location_img").show();
      $("#note_location_msg").addClass("img-visible");

      $.post("/prefs/timezone", {
        latitude: position.coords.latitude,
        longitude: position.coords.longitude
      });

    }, function(err){
      if(err.code == 1) {
        location_error("The website was not able to get permission");
      } else if(err.code == 2) {
        location_error("Location information was unavailable");
      } else if(err.code == 3) {
        location_error("Timed out getting location");
      }
    });
  }

  $("#note_location_chk").click(function(){
    if($(this).attr("checked") == "checked") {
      if(navigator.geolocation) {
        $.post("/prefs", {
          enabled: 1
        });
        fetch_location();
      } else {
        location_error("Browser location is not supported");
      }
    } else {
      $("#note_location_img").hide();
      $("#note_location_msg").removeClass("img-visible");
      $("#note_location_msg").val('');
      $("#note_location").val('');

      $.post("/prefs", {
        enabled: 0
      });
    }
  });

  if($("#location_enabled").val() == 1) {
    $("#note_location_chk").attr("checked","checked");
    fetch_location();
  }

  bind_syndication_buttons();

  if($("#note_in_reply_to").val() != "") {
    $("#note_in_reply_to").change();
  } else {
    restoreNoteState();    
  }

});

<?= partial('partials/syndication-js') ?>

</script>