From 4db692309b5e1de8eec0e4af8366e388fefd8449 Mon Sep 17 00:00:00 2001
From: Dan Brown <ssddanbrown@googlemail.com>
Date: Mon, 28 Dec 2015 15:58:13 +0000
Subject: [PATCH] Standardized vue component http access and fixed some small
 bugs

---
 app/Http/Controllers/PageController.php       |  7 ++-
 app/Repos/PageRepo.php                        |  1 +
 package.json                                  |  4 +-
 .../assets/js/components/image-manager.vue    | 55 ++++++++-----------
 .../assets/js/components/image-picker.vue     | 19 +++----
 5 files changed, 40 insertions(+), 46 deletions(-)

diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php
index 2002fbdf0..499796314 100644
--- a/app/Http/Controllers/PageController.php
+++ b/app/Http/Controllers/PageController.php
@@ -61,9 +61,7 @@ class PageController extends Controller
     {
         $this->checkPermission('page-create');
         $this->validate($request, [
-            'name'   => 'required|string|max:255',
-            'html'   => 'required|string',
-            'parent' => 'integer|exists:pages,id'
+            'name'   => 'required|string|max:255'
         ]);
 
         $input = $request->all();
@@ -121,6 +119,9 @@ class PageController extends Controller
     public function update(Request $request, $bookSlug, $pageSlug)
     {
         $this->checkPermission('page-update');
+        $this->validate($request, [
+            'name'   => 'required|string|max:255'
+        ]);
         $book = $this->bookRepo->getBySlug($bookSlug);
         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
         $this->pageRepo->updatePage($page, $book->id, $request->all());
diff --git a/app/Repos/PageRepo.php b/app/Repos/PageRepo.php
index f7b48efdc..052d9dd1e 100644
--- a/app/Repos/PageRepo.php
+++ b/app/Repos/PageRepo.php
@@ -120,6 +120,7 @@ class PageRepo
      */
     protected function formatHtml($htmlText)
     {
+        if($htmlText == '') return $htmlText;
         libxml_use_internal_errors(true);
         $doc = new \DOMDocument();
         $doc->loadHTML($htmlText);
diff --git a/package.json b/package.json
index e33ad170e..2797cf0c8 100644
--- a/package.json
+++ b/package.json
@@ -9,9 +9,9 @@
     "bootstrap-sass": "^3.0.0",
     "dropzone": "^4.0.1",
     "laravel-elixir": "^3.4.0",
-    "vue": "^1.0.4",
+    "vue": "^1.0.13",
     "vue-hot-reload-api": "^1.2.1",
-    "vue-resource": "^0.1.16",
+    "vue-resource": "^0.5.1",
     "vueify": "^5.0.1",
     "vueify-insert-css": "^1.0.0",
     "zeroclipboard": "^2.2.0"
diff --git a/resources/assets/js/components/image-manager.vue b/resources/assets/js/components/image-manager.vue
index dd4a77d57..757480606 100644
--- a/resources/assets/js/components/image-manager.vue
+++ b/resources/assets/js/components/image-manager.vue
@@ -94,11 +94,11 @@
 
         methods: {
             fetchData: function () {
-                var _this = this;
-                this.$http.get('/images/' + _this.imageType + '/all/' + _this.page, function (data) {
-                    _this.images = _this.images.concat(data.images);
-                    _this.hasMore = data.hasMore;
-                    _this.page++;
+                var url = '/images/' + this.imageType + '/all/' + this.page;
+                this.$http.get(url).then((response) => {
+                    this.images = this.images.concat(response.data.images);
+                    this.hasMore = response.data.hasMore;
+                    this.page++;
                 });
             },
 
@@ -108,16 +108,16 @@
                     url: '/images/' + _this.imageType + '/upload',
                     init: function () {
                         var dz = this;
-                        this.on("sending", function (file, xhr, data) {
+                        dz.on("sending", function (file, xhr, data) {
                             data.append("_token", _this.token);
                         });
-                        this.on("success", function (file, data) {
+                        dz.on("success", function (file, data) {
                             _this.images.unshift(data);
                             $(file.previewElement).fadeOut(400, function () {
                                 dz.removeFile(file);
                             });
                         });
-                        this.on('error', function (file, errorMessage, xhr) {
+                        dz.on('error', function (file, errorMessage, xhr) {
                             if (errorMessage.file) {
                                 $(file.previewElement).find('[data-dz-errormessage]').text(errorMessage.file[0]);
                             }
@@ -149,9 +149,7 @@
             },
 
             selectButtonClick: function () {
-                if (this.callback) {
-                    this.returnCallback(this.selectedImage);
-                }
+                if (this.callback) this.returnCallback(this.selectedImage);
                 this.hide();
             },
 
@@ -177,17 +175,14 @@
 
             saveImageDetails: function (e) {
                 e.preventDefault();
-                var _this = this;
-                _this.selectedImage._token = _this.token;
-                var form = $(_this.$els.imageForm);
-                $.ajax('/images/update/' + _this.selectedImage.id, {
-                    method: 'PUT',
-                    data: _this.selectedImage
-                }).done(function () {
+                this.selectedImage._token = this.token;
+                var form = $(this.$els.imageForm);
+                var url = '/images/update/' + this.selectedImage.id;
+                this.$http.put(url, this.selectedImage).then((response) => {
                     form.showSuccess('Image name updated');
-                }).fail(function (jqXHR) {
-                    form.showFailure(jqXHR.responseJSON);
-                })
+                }, (response) => {
+                    form.showFailure(response.data);
+                });
             },
 
             deleteImage: function (e) {
@@ -195,17 +190,15 @@
                 var _this = this;
                 _this.deleteForm.force = _this.dependantPages !== false;
                 _this.deleteForm._token = _this.token;
-                $.ajax('/images/' + _this.selectedImage.id, {
-                    method: 'DELETE',
-                    data: _this.deleteForm
-                }).done(function () {
-                    _this.images.splice(_this.images.indexOf(_this.selectedImage), 1);
-                    _this.selectedImage = false;
-                    $(_this.$els.imageTitle).showSuccess('Image Deleted');
-                }).fail(function (jqXHR, textStatus) {
+                var url = '/images/' + _this.selectedImage.id;
+                this.$http.delete(url, this.deleteForm).then((response) => {
+                    this.images.splice(this.images.indexOf(this.selectedImage), 1);
+                    this.selectedImage = false;
+                    $(this.$els.imageTitle).showSuccess('Image Deleted');
+                }, (response) => {
                     // Pages failure
-                    if (jqXHR.status === 400) {
-                        _this.dependantPages = jqXHR.responseJSON;
+                    if (response.status === 400) {
+                        _this.dependantPages = response.data;
                     }
                 });
             }
diff --git a/resources/assets/js/components/image-picker.vue b/resources/assets/js/components/image-picker.vue
index 7be976caf..e3c564eb9 100644
--- a/resources/assets/js/components/image-picker.vue
+++ b/resources/assets/js/components/image-picker.vue
@@ -65,9 +65,8 @@
                 this.value = this.currentId === 'false' ?  imageUrl : imageModel.id;
             },
             showImageManager: function(e) {
-                var _this = this;
-                ImageManager.show(function(image) {
-                    _this.updateImageFromModel(image);
+                ImageManager.show((image) => {
+                    this.updateImageFromModel(image);
                 });
             },
             reset: function() {
@@ -75,20 +74,20 @@
             },
             remove: function() {
                 this.image = 'none';
+                this.value = 'none';
             },
             updateImageFromModel: function(model) {
-                var _this = this;
-                var isResized = _this.resizeWidth && _this.resizeHeight;
+                var isResized = this.resizeWidth && this.resizeHeight;
 
                 if (!isResized) {
-                    _this.setCurrentValue(model, model.url);
+                    this.setCurrentValue(model, model.url);
                     return;
                 }
 
-                var cropped = _this.resizeCrop ? 'true' : 'false';
-                var requestString = '/images/thumb/' + model.id + '/' + _this.resizeWidth + '/' + _this.resizeHeight + '/' + cropped;
-                _this.$http.get(requestString, function(data) {
-                    _this.setCurrentValue(model, data.url);
+                var cropped = this.resizeCrop ? 'true' : 'false';
+                var requestString = '/images/thumb/' + model.id + '/' + this.resizeWidth + '/' + this.resizeHeight + '/' + cropped;
+                this.$http.get(requestString).then((response) => {
+                    this.setCurrentValue(model, response.data.url);
                 });
             }
         }