Browse Source

Implemented author links form as items in admin

Markus Ochel 13 years ago
parent
commit
34206af645

+ 16 - 4
admin/controllers/authors.coffee

@@ -14,7 +14,7 @@ class AuthorForm extends Spine.Controller
     '.error-message':          'errorMessage'
     'form':                    'form'
     'select[name=site]':       'formSite'
-    'select[name=links]':      'formLinks'
+    '.links-list':             'linksList'
     '.save-button':            'saveButton'
     '.cancel-button':          'cancelButton'
 
@@ -24,6 +24,7 @@ class AuthorForm extends Spine.Controller
     'click .cancel-button':     'cancel'
     'click .delete-button':     'destroy'
     'change select[name=site]': 'siteChange'
+    'click .add-link':          'addLink'
 
   constructor: ->
     super
@@ -51,9 +52,9 @@ class AuthorForm extends Spine.Controller
     # Set few initial form values
     if @editing
       @formSite.val(@item.site)
-      @formLinks.val(JSON.stringify(@item.links))
     else
       @formSite.val(@stack.stack.filterBox.siteId)
+      @addLink()
     @siteChange()
 
   siteChange: ->
@@ -64,6 +65,10 @@ class AuthorForm extends Spine.Controller
     else
       $siteSelected.html ""
 
+  addLink: (e) ->
+    e?.preventDefault()
+    @linksList.append templates.render('partials/link-form.html', {}, {})
+
   save: (e) ->
     e.preventDefault()
     if @editing
@@ -71,8 +76,14 @@ class AuthorForm extends Spine.Controller
     else
       @item = new Author().fromForm(@form)
 
-    # Transform some fields before saving
-    @item.links = JSON.parse(@item.links)
+    # Construct the links list object
+    links = []
+    @linksList.find('.link-form').each ->
+      label = $.trim $(@).find('input[name=link_label]').val()
+      url = $.trim $(@).find('input[name=link_url]').val()
+      if label and url
+        links.push label: label, url: url
+    @item.links = links
     
     # Save the item and make sure it validates
     if @item.save()
@@ -86,6 +97,7 @@ class AuthorForm extends Spine.Controller
     @el.scrollTop(0, 0)
   
   destroy: ->
+    e.preventDefault()
     if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
       @item.destroy()
       @back()

+ 1 - 0
admin/controllers/blocks.coffee

@@ -81,6 +81,7 @@ class BlockForm extends Spine.Controller
     @el.scrollTop(0, 0)
   
   destroy: ->
+    e.preventDefault()
     if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
       @item.destroy()
       @back()

+ 1 - 0
admin/controllers/collections.coffee

@@ -97,6 +97,7 @@ class CollectionForm extends Spine.Controller
     @el.scrollTop(0, 0)
   
   destroy: ->
+    e.preventDefault()
     if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
       @item.destroy()
       @back()

+ 1 - 0
admin/controllers/contacts.coffee

@@ -62,6 +62,7 @@ class ContactForm extends Spine.Controller
     @el.scrollTop(0, 0)
   
   destroy: ->
+    e.preventDefault()
     if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
       @item.destroy()
       @back()

+ 1 - 0
admin/controllers/sites.coffee

@@ -71,6 +71,7 @@ class SiteForm extends Spine.Controller
     @el.scrollTop(0, 0)
   
   destroy: ->
+    e.preventDefault()
     if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
       @item.destroy()
       @back()

+ 1 - 0
admin/controllers/sponsors.coffee

@@ -71,6 +71,7 @@ class SponsorForm extends Spine.Controller
     @el.scrollTop(0, 0)
   
   destroy: ->
+    e.preventDefault()
     if @item and confirm "Are you sure you want to delete this #{@item.constructor.name}?"
       @item.destroy()
       @back()

+ 3 - 0
admin/static/css/common.styl

@@ -144,6 +144,9 @@ button, .button
     &:hover
       background: #f3f3f3
 
+  &.small
+    padding: 0 5px
+
 ::-webkit-input-placeholder
   color: #e3e3e3
 

+ 1 - 0
admin/static/css/theme.styl

@@ -233,6 +233,7 @@ span.label
 
       .heading
         color: $primaryColor
+        margin: 1em 0
      
       .field
         position: relative

+ 9 - 3
admin/templates/author-form.html

@@ -34,9 +34,15 @@
     <label>Photo</label>
     <input type="text" name="photo" value="{{photo}}" placeholder="URL to photo including http://">
   </div>
-  <div class="field">
-    <label>Links JSON</label>
-    <textarea name="links" placeholder="[{ label: url }, ...]">{{links}}</textarea>
+  
+  <h4 class="heading">
+    Links
+    <a class="button small add-link" href="#">+</a>
+  </h4>
+  <div class="field inline links-list">
+    {{#each links}}
+      {{{include "partials/link-form.html"}}}
+    {{/each}}
   </div>
 </form>
 

+ 10 - 0
admin/templates/partials/link-form.html

@@ -0,0 +1,10 @@
+<div class="link-form clearfix">
+  <div class="field-left">
+    <label>Label</label>
+    <input type="text" name="link_label" value="{{label}}" placeholder="ie. Facebook">
+  </div>
+  <div class="field-right">
+    <label>URL</label>
+    <input type="text" name="link_url" value="{{url}}" placeholder="http://facebook.com/UserName">
+  </div>
+</div>