-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblueprint.js
More file actions
161 lines (131 loc) · 5.04 KB
/
blueprint.js
File metadata and controls
161 lines (131 loc) · 5.04 KB
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
var Blueprint = (function( $, window, undefined ) {
var Blueprint = function( viewController, initElement ) {
// The blueprint prototype will decorate the extending view controller with it's functions
return new Blueprint.fn.init(viewController, initElement);
};
// Blueprint internal functions
Blueprint.fn = Blueprint.prototype = {
constructor : Blueprint,
init : function( viewController, initElements ) {
// extend the viewController, do a deep copy so each vc is unique
var viewController = $.extend(true, {}, this.fn, viewController);
// prepare the initElements map
if(initElements instanceof $ || $.zepto.isZ(initElements)) { // is a single jQuery wrapped element
initElements = { 'main' : initElements };
}
// define the elements object
viewController.elements = initElements;
// define the template object
viewController.templates = {};
// define the model object
viewController.model = {};
// scan the init element for data
viewController.dataScan(initElements.main);
// run build
if( typeof viewController.construct == 'function') viewController.construct();
return viewController;
}
};
Blueprint.fn.init.prototype = Blueprint;
/**
* Blueprint.theme
* Theme handling method
*
* @param template
* The template to insert from the .templates object
* @param variables
* Map of variables to make available to the template
* @param options
* An object of optional parameters
* - offsetElement: the element the theme will be inserted into or around based on the insertMethod, if not specified the theme output will be returned
* - insertMethod: append | prepend | insertAfter | insertBefore
* - preprocess: a function to run before the theme function is run, receives variables as an argument
* - postprocess: a function to run after the theme function is run but before it is inserted into the DOM, receives the theme output as an argument
* - afterInsert: a function to run after the element is inserted onto the DOM, receives the theme output as an argument
*/
Blueprint.fn.theme = function( template, variables, options ) {
var self = this,
buffer;
// if the specfied template isn't available bail
if(typeof this.templates[template] != 'function') return;
// normalize inputs
options = $.extend({
'insertMethod' : 'append',
'offsetElement' : {},
'preprocess' : null,
'postprocess' : null,
'afterInsert' : null
}, options);
variables = !variables ? {} : variables;
// run the preprocess method if provided
if(typeof options.preprocess == 'function') {
options.preprocess.apply(this, [variables]);
}
// run the theme function
var buffer = this.templates[template](variables);
// run the preprocess method if provided
if(typeof options.postprocess == 'function') {
options.postprocess.apply(this, [buffer]);
}
// if no offset element is specified, just return the buffer
if(!options.offsetElement.length) return buffer;
// update the offset element with content back from the template function
options.offsetElement[options.insertMethod](buffer);
// run the preprocess method if provided
if(typeof options.afterInsert == 'function') {
window.setTimeout(function() {
options.afterInsert.call(self, [buffer]);
}, 10);
}
// return the buffer
return buffer;
};
/**
* Blueprint.dataScan
* Finds an element of class .json with a well formatted JSON string and stores it into
* the view controller's model
*
* Data elements with an ID will be stored in the model keyed by that ID, otherwise they
* will be merged into under the 'data' key.
*
* @param $element
* An element to scan for data
*/
Blueprint.fn.dataScan = function( $element ) {
var self = this;
if(!$element) return;
$element.children('.json').each(function() {
var $this = $(this),
key = $this.attr('id') || 'data';
self.model[ key ] = $.extend((self.model[ key ] || {}), $.parseJSON($this.text()));
$this.remove();
});
};
/**
* Blueprint.data
* Finds an element of class .json with a well formatted JSON string and stores it into
* the view controller's model
*
* Data elements with an ID will be stored in the model keyed by that ID, otherwise they
* will be merged into under the 'data' key.
*
* @param $element
* An element to scan for data
*/
Blueprint.fn.data = function( key, value ) {
// key defaults to data
key = key || 'data';
// if key is an object, save the object to the model
if(typeof key == 'object') {
$.extend(this.model, key);
return this;
}
// if a value is present, set the key to that value in the model
if(typeof value != 'undefined') {
this.model[key] = value;
return this;
}
return this.model[key];
};
return Blueprint;
})( (window.Zepto || window.jQuery), this );