UNPKG

2.42 kBJavaScriptView Raw
1(function() {
2 'use strict';
3
4 window.Ractive.Plumes = window.Ractive.Plumes || {};
5
6 window.Ractive.Plumes.bootstrap = function(callback) {
7 function _inScope(element, rvRequireCount) {
8 rvRequireCount = rvRequireCount || 0;
9
10 if (!element.parentNode) {
11 return true;
12 }
13
14 var tag = element.parentNode.tagName.toLowerCase();
15
16 if (tag == 'rv-require') {
17 rvRequireCount++;
18
19 if (rvRequireCount > 1) {
20 return false;
21 }
22 }
23 else if (tag == 'rv-partial') {
24 return false;
25 }
26
27 return _inScope(element.parentNode, rvRequireCount);
28 }
29
30 $(function() {
31 var partials = {},
32 $cleaner = $('<div />').append($('body').html());
33
34 $cleaner.find('script[type="text/javascript"]').remove();
35
36 var $rvPartials = $cleaner.find('rv-partial');
37
38 $rvPartials.each(function(i, rvPartial) {
39 var src = rvPartial.getAttribute('src') || false,
40 target = rvPartial.getAttribute('target');
41
42 if (!src && target && _inScope(rvPartial)) {
43 partials[target] = $.trim(rvPartial.innerHTML);
44 }
45 });
46
47 $rvPartials.remove();
48
49 var page = new window.Ractive({
50 plName: 'pl-page',
51 el: 'body',
52 data: {
53 cls: []
54 },
55 template: $.trim($cleaner.html()),
56 partials: partials
57 });
58
59 page.observe('cls', function(cls) {
60 $(page.el).attr('class', cls ? cls.join(' ') : '');
61 });
62
63 if (callback) {
64 callback(page);
65 }
66 });
67 };
68
69 window.Ractive.Plumes.bindUses = function(component, binds, defaultsToFalse) {
70 defaultsToFalse = defaultsToFalse || [];
71
72 binds.forEach(function(bind) {
73 component.observe('use-' + bind, function(value) {
74 var defaultValue = defaultsToFalse.indexOf(bind) > -1 ? false : true;
75
76 value = typeof value == 'undefined' ? defaultValue : value;
77 value = value == 'true' ? true : value;
78 value = value == 'false' ? false : value;
79
80 component.set('use' + bind, value);
81 });
82 });
83 };
84
85 window.Ractive.Plumes.bindTexts = function(component) {
86 Object.keys(component.get('texts') || {}).forEach(function(key) {
87 component.observe('text-' + key, function(value) {
88 if (typeof value != 'undefined') {
89 component.set('texts.' + key, value);
90 }
91 });
92 });
93 };
94
95})();