summaryrefslogtreecommitdiff
path: root/themes/okadmin/public/js/app.js
blob: 2667ce61a96b9c72b89ddadd507e87469083b6fb (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
var OKAdmin = function(){
  
  // initialize our multi-image uploader with an element and a template
  $(".group.image-list").each(function(){
    var parent = this
    var uploader = new OKUpload ()
    uploader.bind( this )
    uploader.add = function(url){
      var imageTemplate = $(".image-template", parent).html()
      var $el = $(imageTemplate)
      $el.find(".uri").val(url)
      $el.find("img").attr("src", url)
      $("ol", parent).prepend($el)
    }
  })
  // delete image from gallery
  $(document).on("mousedown", ".image-list .remove", function(){
    if (confirm("Remove this image?")) {
      $(this).parent().remove()
    }
  })

  // initialize our multimedia uploader with an element and a template
  $(".group.media-list").each(function(){
    var parent = this
    var uploader = new OKUpload ()
    uploader.bind( this )
    uploader.add = function(url){
      var imageTemplate = $(".image-template", parent).html()
      var $el = $(imageTemplate)
      $el.find(".uri").val(url)
      $el.find("img").attr("src", url)
      $("ol", parent).prepend($el)
    }
    uploader.addVideo = function(media){
      console.log(media)
      var videoTemplate = $(".video-template", parent).html()
      var $el = $(videoTemplate)
      $el.addClass("loaded")
      $el.find(".video-type").val( media.type )
      $el.find(".video-token").val( media.token )
      $el.find(".video-title").val( media.title )
      $el.find(".video-thumb").val( media.thumbnail )
      $el.find("img").attr("src", media.thumbnail )
      $("ol", parent).prepend($el)
   }
  })
  // delete image from gallery
  $(document).on("mousedown", ".media-list .remove", function(){
    if (confirm("Remove this media?")) {
      $(this).parent().remove()
    }
  })

  // initialize our single image uploader with existing DOM
  $(".group.image").each(function(){
    var $el = $(this)
    var uploader = new OKUpload ()
    uploader.bind( this )
    uploader.add = function(url){
      console.log(url)
      $el.find(".uri").val(url)
      $el.find(".caption").val("")
      $el.find("img").attr("src", url).show()
      $el.addClass("loaded")
    }
  })
  // delete image from single image entry
  $(document).on("mousedown", ".image .remove", function(){
    if (confirm("Remove this image?")) {
      var $el = $(this).closest(".image")
      $el.removeClass('loaded')
      $el.find(".uri").val("")
      $el.find(".caption").val("")
      $el.find("img").attr("src", "")
    }
  })

  // make the region sortable with drag-and-drop
  $(".media-list ol, .image-list ol").sortable()
  $(".media-list ol, .image-list ol").disableSelection()
  
  // populate a video field with info from our url parser
  var last_url
  $(".video .url").on("focus", function(){
    var $el = $(this)
    last_url = $el.val()
  })
  $(".video .url").on("keydown blur", pressEnter(function(){
    var $el = $(this)
    var url = $el.val()
    if (url == last_url) { return }
    Parser.parse( url, function(media){
      console.log(url,media)
      $el.parent().addClass("loaded")
      $el.parent().find(".video-type").val( media.type )
      $el.parent().find(".video-token").val( media.token )
      $el.parent().find(".video-title").val( media.title )
      $el.parent().find(".video-thumb").val( media.thumbnail )
    })
  }))
  
  // fix post indexing in list-driven inputs
  $(".main.resource form").submit(function(e){
    var $id = $("[name=id]"), $title = $("[name=title]"), $menu = $("[name=menu]"), $section = $(".resource.main")
    var id = $section.data("id"), type = $section.data("type")

    if ($title.length && ! $title.val()) {
      $title.focus()
      alert("Please enter a title")
      e.preventDefault()
      return
    }

    if ($menu.length && ! $menu.val()) {
      $menu.val( $title.val() )
    }

    // TODO: pass through whether this page is static
    if (type === "page" && (id == "contact" || id == "about")) {
      ;
    }
    else {
      var slug = slugify( $title.val() )
      $id.val( slug )
    }

    $("ol").each(function(){
      $("li", this).each(function(index){
        $(this).find("input,textarea").each(function(){
          var field = $(this).attr("name").replace(/\[[0-9]*\]/, "[" + index + "]")
          $(this).attr("name", field)
        })
      })
    })
  })
  
  // delete individual records
  $("#delete_form").submit(function(e){
  	if (confirm("Are you sure you want to delete this record?")) {
  		return
  	}
  	else {
  		e.preventDefault()
  	}
  })

  // reorder items in categories
  $(".resource-category").on("click", ".edit-btn", function(e) {
    e.preventDefault();
    var $parent = $(e.delegateTarget);
    var $editBtn = $parent.find(".edit-btn");
    var $cancelBtn = $parent.find(".cancel-btn");
    var $saveBtn = $parent.find(".save-btn");
    var $ol = $parent.find("ol");
    var toggles = [$parent, $cancelBtn, $saveBtn, $editBtn];

    $ol.sortable();
    $ol.disableSelection();
    toggle();

    $cancelBtn.one("click", function(e) {
      $ol.sortable("cancel");
      $ol.enableSelection();
      toggle();
    });

    $saveBtn.one("click", function(e) {
      $ol.sortable();
      toggle();
    });

    function toggle() {
      toggles.forEach(function($el) {
        $el.toggleClass('active');
      })
    }
  });

  // save new category order
  $(".resource-category").on("submit", "form", function(e) {
    var $parent = $(e.delegateTarget);
    $parent.find(".resource-input").each(function(index) {
      var $input = $(this);
      var parsed = JSON.parse($input.val());
      parsed.__index = index;
      $input.val(JSON.stringify(parsed));
    })
  });
}

$(function(){
  window.app = new OKAdmin ()
})


function slugify (s){ return (s || "").toLowerCase().replace(/\s/g,"-").replace(/[^-_a-zA-Z0-9]/g, '-').replace(/-+/g, "-") }