Skip to content

Commit

Permalink
Split response and sajax, add to __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
burnhamrobertp committed Dec 7, 2014
1 parent 1d535f9 commit b1d0938
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 35 deletions.
7 changes: 2 additions & 5 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
import pkg_resources


def get_javascript_string():
return pkg_resources.resource_string('sajax', 'static/js/sajax.js')
from sajax import Sajax
from response import Response
69 changes: 40 additions & 29 deletions response.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
from flask import jsonify, session, url_for
import json, os, pprint
from config import *
import json, os


class SResponse:
"""An object which eases the work between ajax and python"""
data = []
class Response:
"""Eases sending of JS commands to the DOM
Builds an internal json array of commands (and their parameters) to be sent as
a response to an AJAX request. This AJAX request is expected to have been initiated
by SAjax's javascript-side methods.
Sajax JS POSTS requests to Python which uses Response, returns Response.get_json
back to Sajax JS for processing...pretty simple, no?
"""

project_root = ''
inc_file_prefix = ''
inc_file_suffix = ''

_data = []


def __init__(self, cls=None):
self.data = []
self._data = []

# If we're loading a view and have not yet loaded its static dependencies, asynchronously load them
if cls.__class__.__name__.find('View') != -1:
Expand All @@ -17,60 +30,58 @@ def __init__(self, cls=None):
# if its not in loaded, it hasn't been loaded
if folder not in session['sajax']['loaded_dep']:
# css first, /static/css/obt_folder.css
if os.path.isfile(ROOT+'/static/css/obt_'+folder+'.css'):
self.data.append({'action': 'load', 'data': url_for('static', filename='css/obt_%s.css' % folder)})
filepath = self.root+'/static/css/%s%s%s.css' % (self.inc_file_prefix, folder, self.inc_file_suffix)
if os.path.isfile(filepath):
self._data.append({'action': 'load', 'data': url_for('static', filename='css/%s.css' % folder)})

# first the js file at /static/js/folder/folder.js if it exists
if os.path.isfile(ROOT+'/static/js/%s/%s.js' % (folder, folder)):
self.data.append({'action': 'load', 'data': url_for('static', filename='js/%s/%s.js' % (folder, folder))})
if os.path.isfile(self.root+'/static/js/%s/%s.js' % (folder, folder)):
self._data.append({'action': 'load', 'data': url_for('static', filename='js/%s/%s.js' % (folder, folder))})

# then all the rest of the js files in /static/js/folder/
if os.path.isdir(ROOT+'/static/js/'+folder):
for file in os.listdir(ROOT+'/static/js/'+folder):
if os.path.isdir(self.root+'/static/js/'+folder):
for file in os.listdir(self.root+'/static/js/'+folder):
if (file != folder+'.js'):
self.data.append({'action': 'load', 'data': url_for('static', filename='js/%s/%s' % (folder, file))})
self._data.append({'action': 'load', 'data': url_for('static', filename='js/%s/%s' % (folder, file))})

# this folder's js files might have defined an object
self.data.append({'action': 'js', 'data': "if (typeof %s != 'undefined') window.%s = new %s()" % (folder, folder, folder)})
self._data.append({'action': 'js', 'data': "if (typeof %s != 'undefined') window.%s = new %s()" % (folder, folder, folder)})
# The files will be loaded, mark them as such
session['sajax']['loaded_dep'].append(folder)

def __add__(self, other):
if isinstance(other.data, basestring):
decoder = json.JSONDecoder()
self.data += decoder.decode(other.data)['r']
self._data += decoder.decode(other.data)['r']
elif isinstance(other.data, list):
self.data += other.data
self._data += other.data

return self

def get_json(self):
return jsonify(r=self.data)
return jsonify(r=self._data)

def assign(self, selector, html):
self.data.append({ 'action': 'as', 'selector': selector, 'data': html })
self._data.append({ 'action': 'as', 'selector': selector, 'data': html })

def append(self, selector, html):
self.data.append({ 'action': 'ap', 'selector': selector, 'data': html })
self._data.append({ 'action': 'ap', 'selector': selector, 'data': html })

def append_template(self, selector, html, template_id):
self.data.append({ 'action': 'ap_t', 'selector': selector, 'data': html, 'id': template_id })
self._data.append({ 'action': 'ap_t', 'selector': selector, 'data': html, 'id': template_id })

def script(self, js):
self.data.append({ 'action': 'js', 'data': js })

def position(self, selector, keyword):
self.data.append({ 'action': 'js', 'data': "$('%s').data('position', '%s').addClass('positioned'); sajax.position('%s');" % (selector, keyword, selector) })
self._data.append({ 'action': 'js', 'data': js })

def remove(self, selector):
self.data.append({ 'action': 'js', 'data': "$('%s').remove()" % selector })
self._data.append({ 'action': 'js', 'data': "$('%s').remove()" % selector })

def alert(self, text):
self.data.append({ 'action': 'alert', 'data': text})
self._data.append({ 'action': 'alert', 'data': text})

def call(self, path):
self.data.append({ 'action': 'js', 'data': "sajax.call('%s')" % path})
self._data.append({ 'action': 'js', 'data': "sajax.call('%s')" % path})

def error(self, baseSelector, error):
self.data.append({ 'action': 'assign', 'selector': baseSelector+' .error', 'data': error })
self.data.append({ 'action': 'js', 'data': "$('"+baseSelector+" .error').show();" })
self._data.append({ 'action': 'assign', 'selector': baseSelector+' .error', 'data': error })
self._data.append({ 'action': 'js', 'data': "$('"+baseSelector+" .error').show();" })
27 changes: 27 additions & 0 deletions sajax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pkg_resources
import pprint


class Sajax:
"""Register Python routes or methods with Sajax.js
Given a set of python routes or FlaskViews, register these routes so that they can be
easily called using dynamically built, route-specific javascript methods.
"""

def get_js_path(self):
"""Return the path to sajax.js"""
return pkg_resources.resource_string(__name__, 'static/js/sajax.js')


def get_js_string(self):
"""Return the string contents of sajax.js"""
pass


def register_view(self, view):
"""Register a FlaskView
"""
pprint.pprint(view)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='sajax',
version='0.1',
version='0.11',
url='https://github.com/harkenn/sajax',
license='BSD',
author='Robert Burnham',
Expand Down

0 comments on commit b1d0938

Please sign in to comment.