1 | <?php |
||
13 | class JS |
||
14 | { |
||
15 | /** |
||
16 | * Holds all javascript content |
||
17 | * |
||
18 | * @var array { |
||
19 | * @var array $file All files that need to be included |
||
20 | * @var array $script All raw scripts |
||
21 | * @var array $ready All scripts that need to be wrapped in "document.ready()" |
||
22 | * @var array $settings The settings for the javascript page |
||
23 | * } |
||
24 | */ |
||
25 | protected $queue; |
||
26 | |||
27 | /** |
||
28 | * Prepare the queue |
||
29 | */ |
||
30 | 30 | public function __construct($queue) |
|
34 | |||
35 | /** |
||
36 | * Add a file |
||
37 | * |
||
38 | * @param string $content |
||
39 | * @return $this |
||
40 | */ |
||
41 | 3 | public function file($content) |
|
47 | |||
48 | /** |
||
49 | * Add a raw script |
||
50 | * |
||
51 | * @param string $content |
||
52 | * @return $this |
||
53 | */ |
||
54 | 3 | public function script($content) |
|
60 | |||
61 | /** |
||
62 | * Add a script to wrap inside `document.ready()` |
||
63 | * |
||
64 | * @param string $content |
||
65 | * @return $this |
||
66 | */ |
||
67 | 3 | public function ready($content) |
|
73 | |||
74 | /** |
||
75 | * Add settings to the page |
||
76 | * |
||
77 | * @param array $content |
||
78 | * @return $this |
||
79 | */ |
||
80 | 21 | public function setting(array $content) |
|
86 | |||
87 | /** |
||
88 | * Outputs the scripts |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 30 | public function render() |
|
93 | { |
||
94 | 30 | $output = ''; |
|
95 | |||
96 | //files |
||
97 | 30 | if (count($this->queue['file'])) { |
|
98 | 3 | $output .= $this->renderFiles(); |
|
99 | } |
||
100 | |||
101 | 30 | $script_tag = $this->renderScriptTag(); |
|
102 | |||
103 | 30 | if (!empty($script_tag)) { |
|
104 | 24 | $output .= '<script type="text/javascript">'; |
|
105 | 24 | $output .= "var APP = APP || {'settings': {}, 'behaviors':{}, 'locale':{}, 'utilities':{}};"; |
|
106 | 24 | $output .= $this->renderScriptTag(); |
|
107 | 24 | $output .= '</script>'; |
|
108 | } |
||
109 | |||
110 | 30 | return $output; |
|
111 | } |
||
112 | |||
113 | 30 | protected function renderScriptTag() |
|
114 | { |
||
115 | 30 | $output = ''; |
|
116 | 30 | if (count($this->queue['setting'])) { |
|
117 | 18 | $output .= $this->renderSettings(); |
|
118 | } |
||
119 | |||
120 | //standard script |
||
121 | 30 | if (count($this->queue['script'])) { |
|
122 | 3 | $output .= $this->renderScript(); |
|
123 | } |
||
124 | |||
125 | //jquery document.ready |
||
126 | 30 | if (count($this->queue['ready'])) { |
|
127 | 3 | $output .= $this->renderReady(); |
|
128 | } |
||
129 | |||
130 | 30 | return $output; |
|
131 | } |
||
132 | |||
133 | 3 | protected function renderFiles() |
|
134 | { |
||
135 | 3 | $output = ''; |
|
136 | 3 | foreach ($this->queue['file'] as $item) { |
|
137 | 3 | $output .= '<script type="text/javascript" src="' . $item . '"></script>'; |
|
138 | } |
||
139 | |||
140 | 3 | return $output; |
|
141 | } |
||
142 | |||
143 | 3 | protected function renderScript() |
|
147 | |||
148 | 3 | protected function renderReady() |
|
152 | |||
153 | 18 | protected function renderSettings() |
|
159 | |||
160 | /** |
||
161 | * Walk the settings array to output them as a pure array |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 6 | protected function resolveSettings() |
|
169 | } |
||
170 |