From fdae99db2bdb3dcbe319ad274029db8ace81d5fb Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 4 Jan 2012 12:48:11 -0800 Subject: [PATCH] moved Admin.js into a Shipyard app --- apps/base/templates/admin.html | 10 ++++- media/base/js/Admin.js | 53 ---------------------- media/base/js/admin/index.js | 75 ++++++++++++++++++++++++++++++++ media/base/js/admin/package.json | 11 +++++ 4 files changed, 95 insertions(+), 54 deletions(-) delete mode 100644 media/base/js/Admin.js create mode 100644 media/base/js/admin/index.js create mode 100644 media/base/js/admin/package.json diff --git a/apps/base/templates/admin.html b/apps/base/templates/admin.html index dfec17db..61e7115b 100644 --- a/apps/base/templates/admin.html +++ b/apps/base/templates/admin.html @@ -35,7 +35,15 @@ 'update_package_url':'{{ url('admin_update_package') }}', }; - +{% endblock %} + +{% block shipyardjs %} + {{ shipyard("/media/base/js/admin/") }} +{% endblock %} + +{% block page_domready %} + var admin = require('admin'); + admin.init(admin_settings); {% endblock %} {% block app_content %} diff --git a/media/base/js/Admin.js b/media/base/js/Admin.js deleted file mode 100644 index 4a5dfb25..00000000 --- a/media/base/js/Admin.js +++ /dev/null @@ -1,53 +0,0 @@ -window.addEvent('domready', function(e){ - var setMsg = function(msg){ - var elem = $('package_msg'); - elem.set('html', msg); - setTimeout(function(){ - elem.set('html',''); - },2000); - } - $('btn_find_package').addEvent('click', function(e){ - var r = new Request({ - url: admin_settings.get_package_url, - onSuccess: function(p){ - $('package_item').set('html',p); - }, - onFailure: function(err){ - console.log(err); - setMsg(err.status + " " + err.statusText); - } - }); - r.get('package_id='+ $('txt_package_id').value); - - }); - - var updatePackage = function(elem, field, callback){ - var id = elem.getParent('.package').getElement('.package_id').value, - enabled = elem.getParent().hasClass('pressed'), - r = new Request({ - url: admin_settings.update_package_url, - method: 'post', - onSuccess: function(p){ - setMsg("updated"); - elem.getParent().toggleClass('pressed'); - if (callback) callback(); - }, - onFailure: function(err){ - if(err.status == 404){ - $('package_item').set('html', ''); - } - setMsg(err.status + " " + err.statusText); - } - }); - r.send('package_id='+id+'&'+field+'='+!enabled); - }; - - $('package_item').addEvent('click:relay(.UI_Package_Featured a)', function(e){ - e.stop(); - updatePackage(this, 'featured') - }); - $('package_item').addEvent('click:relay(.UI_Package_Example a)', function(e){ - e.stop(); - updatePackage(this, 'example') - }); -}); \ No newline at end of file diff --git a/media/base/js/admin/index.js b/media/base/js/admin/index.js new file mode 100644 index 00000000..42905048 --- /dev/null +++ b/media/base/js/admin/index.js @@ -0,0 +1,75 @@ +var Request = require('shipyard/http/Request'), + dom = require('shipyard/dom'); + +var admin_settings; +var PRESSED = 'pressed'; + +function setMsg(msg) { + var elem = dom.$('package_msg'); + elem.set('html', msg); + setTimeout(function() { + elem.set('html', ''); + }, 2000); +} + +function updatePackage(elem, field, callback) { + var id = elem.getParent('.package').getElement('.package_id').get('value'), + parent = elem.getParent(), + enabled = parent.hasClass(PRESSED), + data = { + package_id: id + }; + data[field] = !enabled; + new Request({ + url: admin_settings.update_package_url, + method: 'post', + data: data, + onSuccess: function() { + setMsg('updated'); + if (enabled) { + parent.removeClass(PRESSED); + } else { + parent.addClass(PRESSED); + } + if (callback) { + callback(); + } + }, + onFailure: function() { + if (this.xhr.status === 404) { + dom.$('package_item').set('html', ''); + } + setMsg(this.xhr.status + " " + this.xhr.statusText); + } + }).send(); +} + +exports.init = function(settings) { + admin_settings = settings; + + var item = dom.$('package_item'); + item.delegate('.UI_Package_Featured a', 'click', function(e) { + e.stop(); + updatePackage(this, 'featured'); + }); + item.delegate('.UI_Package_Example a', 'click', function(e) { + e.stop(); + updatePackage(this, 'example'); + }); + + dom.$('btn_find_package').addListener('click', function(e) { + new Request({ + url: admin_settings.get_package_url, + method: 'get', + data: { + package_id: dom.$('txt_package_id').get('value') + }, + onSuccess: function(text) { + item.set('html', text); + }, + onFailure: function() { + setMsg(this.xhr.status + ' ' + this.xhr.statusText); + } + }).send(); + }); +}; diff --git a/media/base/js/admin/package.json b/media/base/js/admin/package.json new file mode 100644 index 00000000..777ca918 --- /dev/null +++ b/media/base/js/admin/package.json @@ -0,0 +1,11 @@ +{ + "name": "admin", + + "shipyard": { + "dependencies": { + "flightdeck": "../flightdeck" + }, + "mini_require": true, + "target": "../admin-min.js" + } +}