arnaucube
arnaucube/decentralized-blogging-platformPublic Self-hosted repository
decentralized-blogging-platform/webapp/views/write/write.js
webapp/views/write/write.jsRaw
  1'use strict';
  2
  3angular.module('app.write', ['ngRoute'])
  4
  5  .config(['$routeProvider', function($routeProvider) {
  6    $routeProvider.when('/write', {
  7      templateUrl: 'views/write/write.html',
  8      controller: 'WriteCtrl'
  9    });
 10  }])
 11
 12  .controller('WriteCtrl', function($scope, $rootScope, $http, toastr) {
 13    $scope.post = {
 14      title: "",
 15      content: "",
 16      summary: ""
 17    };
 18    var editor = new MediumEditor('.editable', {
 19      toolbar: {
 20        /* These are the default options for the toolbar,
 21           if nothing is passed this is what is used */
 22        allowMultiParagraphSelection: true,
 23        buttons: ['bold', 'italic', 'underline', 'anchor', 'h2', 'h3', 'quote'],
 24        diffLeft: 0,
 25        diffTop: -10,
 26        firstButtonClass: 'medium-editor-button-first',
 27        lastButtonClass: 'medium-editor-button-last',
 28        relativeContainer: null,
 29        standardizeSelectionStart: false,
 30        static: false,
 31        /* options which only apply when static is true */
 32        align: 'center',
 33        sticky: false,
 34        updateOnEmptySelection: false
 35      },
 36      autoLink: true,
 37      placeholder: {
 38        text: 'Start to writting your decentralized blog post...'
 39      }
 40    }).subscribe('editableInput', function(event, editable) {
 41      // Do some work
 42      $scope.post.content = editable.innerHTML;
 43    });
 44
 45
 46    $scope.publicate = function() {
 47      //add the thumbimg to ipfs
 48      var formdata = new FormData();
 49      formdata.append("file", $scope.file);
 50      $http({
 51          url: ipfsurl + 'add',
 52          method: "POST",
 53          headers: {
 54            "Content-Type": undefined
 55          },
 56          data: formdata
 57        })
 58        .then(function(data) {
 59            console.log("data: ");
 60            console.log(data.data);
 61            $scope.post.thumbimg = data.data.Hash;
 62            toastr.success("Thumb image added to IPFS");
 63            $scope.publicate2();
 64          },
 65          function(data) {
 66            console.log(data);
 67            toastr.error("Error adding Model to IPFS");
 68          });
 69    };
 70    $scope.publicate2 = function() {
 71      //$scope.post.thumbimg = hash;
 72      //send the post content to the Go api, that will add the html content to ipfs
 73      console.log("post", $scope.post);
 74      $http({
 75          url: apiurl + 'post',
 76          method: "POST",
 77          headers: {
 78            "Content-Type": undefined
 79          },
 80          data: $scope.post
 81        })
 82        .then(function(data) {
 83            console.log("data: ");
 84            console.log(data.data);
 85            window.location = "#!/main";
 86          },
 87          function(data) {
 88            console.log(data);
 89          });
 90
 91    };
 92  }).directive('fileModel', ['$parse', function($parse) {
 93    //directive code from https://www.tutorialspoint.com/angularjs/angularjs_upload_file.htm
 94    return {
 95      restrict: 'A',
 96      link: function(scope, element, attrs) {
 97        var model = $parse(attrs.fileModel);
 98        var modelSetter = model.assign;
 99        element.bind('change', function() {
100          scope.$apply(function() {
101            modelSetter(scope, element[0].files[0]);
102          });
103        });
104      }
105    };
106  }]);