spark.ko.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. var Result = function (snippet, result) {
  17. var self = this;
  18. self.id = ko.observable(typeof result.id != "undefined" && result.id != null ? result.id : UUID());
  19. self.type = ko.observable(typeof result.type != "undefined" && result.type != null ? result.type : 'table');
  20. self.hasResultset = ko.observable(typeof result.hasResultset != "undefined" && result.hasResultset != null ? result.hasResultset : true);
  21. self.handle = ko.observable({});
  22. self.meta = ko.observableArray(typeof result.meta != "undefined" && result.meta != null ? result.meta : []);
  23. self.meta.extend({ rateLimit: 50 });
  24. self.cleanedMeta = ko.computed(function(){
  25. return ko.utils.arrayFilter(self.meta(), function(item) {
  26. return item.name != ''
  27. });
  28. });
  29. self.startTime = ko.observable(typeof result.startTime != "undefined" && result.startTime != null ? new Date(result.startTime) : new Date());
  30. self.endTime = ko.observable(typeof result.endTime != "undefined" && result.endTime != null ? new Date(result.endTime) : new Date());
  31. self.executionTime = ko.computed(function() {
  32. return self.endTime().getTime() - self.startTime().getTime();
  33. });
  34. function isNumericColumn(type) {
  35. return $.inArray(type, ['TINYINT_TYPE', 'SMALLINT_TYPE', 'INT_TYPE', 'BIGINT_TYPE', 'FLOAT_TYPE', 'DOUBLE_TYPE', 'DECIMAL_TYPE', 'TIMESTAMP_TYPE', 'DATE_TYPE']) > -1;
  36. }
  37. function isDateTimeColumn(type) {
  38. return $.inArray(type, ['TIMESTAMP_TYPE', 'DATE_TYPE']) > -1;
  39. }
  40. function isStringColumn(type) {
  41. return ! isNumericColumn(type) && ! isDateTimeColumn(type);
  42. }
  43. self.cleanedNumericMeta = ko.computed(function(){
  44. return ko.utils.arrayFilter(self.meta(), function(item) {
  45. return item.name != '' && isNumericColumn(item.type)
  46. });
  47. });
  48. self.cleanedStringMeta = ko.computed(function(){
  49. return ko.utils.arrayFilter(self.meta(), function(item) {
  50. return item.name != '' && isStringColumn(item.type)
  51. });
  52. });
  53. self.cleanedDateTimeMeta = ko.computed(function(){
  54. return ko.utils.arrayFilter(self.meta(), function(item) {
  55. return item.name != '' && isDateTimeColumn(item.type)
  56. });
  57. });
  58. self.data = ko.observableArray(typeof result.data != "undefined" && result.data != null ? result.data : []);
  59. self.data.extend({ rateLimit: 50 });
  60. self.logs = ko.observable('');
  61. self.errors = ko.observable('');
  62. self.hasSomeResults = ko.computed(function(){
  63. return self.hasResultset() && self.data().length > 0; // status() == 'available'
  64. });
  65. if (typeof result.handle != "undefined" && result.handle != null) {
  66. $.each(result.handle, function(key, val) {
  67. self.handle()[key] = val;
  68. });
  69. }
  70. self.clear = function() {
  71. $.each(self.handle, function(key, val) {
  72. delete self.handle()[key];
  73. });
  74. self.meta.removeAll();
  75. self.data.removeAll();
  76. self.logs('');
  77. self.errors('');
  78. self.startTime(new Date());
  79. self.endTime(new Date());
  80. };
  81. }
  82. var TYPE_EDITOR_MAP = {
  83. 'hive': 'text/x-hiveql',
  84. 'impala': 'text/x-impalaql',
  85. 'python': 'text/x-python',
  86. 'scala': 'text/x-scala',
  87. 'pig': 'text/x-pig'
  88. }
  89. var Snippet = function (vm, notebook, snippet) {
  90. var self = this;
  91. self.id = ko.observable(typeof snippet.id != "undefined" && snippet.id != null ? snippet.id : UUID());
  92. self.name = ko.observable(typeof snippet.name != "undefined" && snippet.name != null ? snippet.name : '');
  93. self.type = ko.observable(typeof snippet.type != "undefined" && snippet.type != null ? snippet.type : "hive");
  94. self.editorMode = ko.observable(TYPE_EDITOR_MAP[self.type()]);
  95. self.statement_raw = ko.observable(typeof snippet.statement_raw != "undefined" && snippet.statement_raw != null ? snippet.statement_raw : vm.snippetPlaceholders[self.type()]);
  96. self.codemirrorSize = ko.observable(typeof snippet.codemirrorSize != "undefined" && snippet.codemirrorSize != null ? snippet.codemirrorSize : 100);
  97. //self.statement_raw.extend({ rateLimit: 150 });
  98. self.status = ko.observable(typeof snippet.status != "undefined" && snippet.status != null ? snippet.status : 'loading');
  99. self.settings = ko.mapping.fromJS(typeof snippet.settings != "undefined" && snippet.settings != null ? snippet.settings : {});
  100. self.variables = ko.observableArray([]);
  101. self.variableNames = ko.computed(function () {
  102. var matches = [];
  103. var myRegexp = /(?:[^\\]\$)([^\d'" ]\w*)/g;
  104. var match = myRegexp.exec(self.statement_raw());
  105. while (match != null) {
  106. matches.push(match[1]);
  107. match = myRegexp.exec(self.statement());
  108. }
  109. return matches;
  110. });
  111. self.variableNames.extend({ rateLimit: 150 });
  112. self.variableNames.subscribe(function (newVal) {
  113. var toDelete = [];
  114. var toAdd = [];
  115. $.each(newVal, function (key, name) {
  116. var match = ko.utils.arrayFirst(self.variables(), function (_var) {
  117. return _var.name() == name;
  118. });
  119. if (!match) {
  120. toAdd.push(name);
  121. }
  122. });
  123. $.each(self.variables(), function (key, _var) {
  124. var match = ko.utils.arrayFirst(newVal, function (name) {
  125. return _var.name() == name;
  126. });
  127. if (!match) {
  128. toDelete.push(_var);
  129. }
  130. });
  131. $.each(toDelete, function (index, item) {
  132. self.variables.remove(item);
  133. });
  134. $.each(toAdd, function (index, item) {
  135. self.variables.push(ko.mapping.fromJS({'name': item, 'value': ''}));
  136. });
  137. self.variables.sort(function (left, right) {
  138. var leftIndex = newVal.indexOf(left.name());
  139. var rightIndex = newVal.indexOf(right.name());
  140. return leftIndex == rightIndex ? 0 : (leftIndex < rightIndex ? -1 : 1);
  141. });
  142. });
  143. self.statement = ko.computed(function () {
  144. var statement = self.statement_raw();
  145. $.each(self.variables(), function (index, variable) {
  146. statement = statement.replace(RegExp("([^\\\\])\\$" + variable.name(), "g"), "$1" + variable.value());
  147. });
  148. return statement;
  149. });
  150. self.result = new Result(snippet, snippet.result);
  151. self.showGrid = ko.observable(typeof snippet.showGrid != "undefined" && snippet.showGrid != null ? snippet.showGrid : true);
  152. self.showChart = ko.observable(typeof snippet.showChart != "undefined" && snippet.showChart != null ? snippet.showChart : false);
  153. self.showLogs = ko.observable(typeof snippet.showLogs != "undefined" && snippet.showLogs != null ? snippet.showLogs : false);
  154. self.progress = ko.observable(typeof snippet.progress != "undefined" && snippet.progress != null ? snippet.progress : 0);
  155. self.progress.subscribe(function (val){
  156. $(document).trigger("progress", {data: val, snippet: self});
  157. });
  158. self.showGrid.subscribe(function (val){
  159. if (val){
  160. self.showChart(false);
  161. }
  162. });
  163. self.showChart.subscribe(function (val){
  164. if (val){
  165. self.showGrid(false);
  166. self.isLeftPanelVisible(true);
  167. $(document).trigger("forceChartDraw", self);
  168. }
  169. });
  170. self.showLogs.subscribe(function (val){
  171. if (val){
  172. self.getLogs();
  173. }
  174. });
  175. self.size = ko.observable(typeof snippet.size != "undefined" && snippet.size != null ? snippet.size : 12).extend({ numeric: 0 });
  176. self.offset = ko.observable(typeof snippet.offset != "undefined" && snippet.offset != null ? snippet.offset : 0).extend({ numeric: 0 });
  177. self.isLoading = ko.computed(function(){
  178. return self.status() == "loading";
  179. });
  180. self.klass = ko.computed(function () {
  181. return "snippet card card-widget";
  182. });
  183. self.editorKlass = ko.computed(function(){
  184. return "editor span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  185. });
  186. self.resultsKlass = ko.computed(function(){
  187. return "results " + self.type();
  188. });
  189. self.errorsKlass = ko.computed(function(){
  190. return self.resultsKlass + " alert alert-error";
  191. });
  192. self.chartType = ko.observable(typeof snippet.chartType != "undefined" && snippet.chartType != null ? snippet.chartType : ko.HUE_CHARTS.TYPES.BARCHART);
  193. self.chartSorting = ko.observable(typeof snippet.chartSorting != "undefined" && snippet.chartSorting != null ? snippet.chartSorting : "none");
  194. self.chartScatterGroup = ko.observable(typeof snippet.chartScatterGroup != "undefined" && snippet.chartScatterGroup != null ? snippet.chartScatterGroup : null);
  195. self.chartScatterSize = ko.observable(typeof snippet.chartScatterSize != "undefined" && snippet.chartScatterSize != null ? snippet.chartScatterSize : null);
  196. self.chartX = ko.observable(typeof snippet.chartX != "undefined" && snippet.chartX != null ? snippet.chartX : null);
  197. self.chartYSingle = ko.observable(typeof snippet.chartYSingle != "undefined" && snippet.chartYSingle != null ? snippet.chartYSingle : null);
  198. self.chartYMulti = ko.observableArray(typeof snippet.chartYMulti != "undefined" && snippet.chartYMulti != null ? snippet.chartYMulti : []);
  199. self.chartData = ko.observableArray(typeof snippet.chartData != "undefined" && snippet.chartData != null ? snippet.chartData : []);
  200. self.chartMapLabel = ko.observable(typeof snippet.chartMapLabel != "undefined" && snippet.chartMapLabel != null ? snippet.chartMapLabel : null);
  201. self.chartType.subscribe(function(val){
  202. $(document).trigger("forceChartDraw", self);
  203. });
  204. self.tempChartOptions = {};
  205. self.isLeftPanelVisible = ko.observable(typeof snippet.isLeftPanelVisible != "undefined" && snippet.isLeftPanelVisible != null ? snippet.isLeftPanelVisible : false);
  206. self.toggleLeftPanel = function () {
  207. self.isLeftPanelVisible(! self.isLeftPanelVisible());
  208. $(document).trigger("toggleLeftPanel", self);
  209. };
  210. self.expand = function () {
  211. self.size(self.size() + 1);
  212. $("#snippet_" + self.id()).trigger("resize");
  213. }
  214. self.compress = function () {
  215. self.size(self.size() - 1);
  216. $("#snippet_" + self.id()).trigger("resize");
  217. }
  218. self.moveLeft = function () {
  219. self.offset(self.offset() - 1);
  220. }
  221. self.moveRight = function () {
  222. self.offset(self.offset() + 1);
  223. }
  224. self.remove = function (notebook, snippet) {
  225. notebook.snippets.remove(snippet);
  226. }
  227. self.checkStatusTimeout = null;
  228. self._ajax_error = function(data) {
  229. if (data.status == -2) {
  230. self.create_session();
  231. }
  232. else if (data.status == -3) {
  233. self.status('expired');
  234. }
  235. else if (data.status == 1) {
  236. self.status('failed');
  237. self.result.errors(data.message);
  238. } else {
  239. $(document).trigger("error", data.message);
  240. self.status('failed');
  241. }
  242. };
  243. self.create_session = function() {
  244. self.status('loading');
  245. $.post("/spark/api/create_session", {
  246. notebook: ko.mapping.toJSON(notebook),
  247. snippet: ko.mapping.toJSON(self)
  248. }, function (data) {
  249. if (data.status == 0) {
  250. notebook.addSession(ko.mapping.fromJS(data.session));
  251. self.status('ready');
  252. }
  253. else {
  254. $(document).trigger("error", data.message);
  255. }
  256. }).fail(function (xhr, textStatus, errorThrown) {
  257. $(document).trigger("error", xhr.responseText);
  258. });
  259. };
  260. self.execute = function () {
  261. $(document).trigger("executeStarted", self);
  262. $(".jHueNotify").hide();
  263. logGA('/execute/' + self.type());
  264. if (self.result.meta().length > 0) {
  265. self.close();
  266. }
  267. self.result.clear();
  268. self.progress(0);
  269. self.status('running');
  270. $.post("/spark/api/execute", {
  271. notebook: ko.mapping.toJSON(notebook),
  272. snippet: ko.mapping.toJSON(self)
  273. }, function (data) {
  274. if (data.status == 0) {
  275. $.each(data.handle, function (key, val) {
  276. self.result.handle()[key] = val;
  277. });
  278. self.result.hasResultset(data.handle.has_result_set);
  279. self.checkStatus();
  280. } else {
  281. self._ajax_error(data);
  282. }
  283. }).fail(function (xhr, textStatus, errorThrown) {
  284. $(document).trigger("error", xhr.responseText);
  285. self.status('failed');
  286. });
  287. };
  288. self.fetchResult = function(rows, startOver) {
  289. if (typeof startOver == "undefined") {
  290. startOver = true;
  291. }
  292. self.fetchResultData(rows, startOver);
  293. //self.fetchResultMetadata(rows);
  294. };
  295. self.fetchResultData = function(rows, startOver) {
  296. $.post("/spark/api/fetch_result_data", {
  297. notebook: ko.mapping.toJSON(notebook),
  298. snippet: ko.mapping.toJSON(self),
  299. rows: rows,
  300. startOver: startOver
  301. }, function (data) {
  302. if (data.status == 0) {
  303. rows -= data.result.data.length;
  304. if (self.result.meta().length == 0) {
  305. data.result.meta.unshift({type: "INT_TYPE", name: "", comment: null});
  306. self.result.meta(data.result.meta);
  307. }
  308. var _initialIndex = self.result.data().length;
  309. var _tempData = [];
  310. $.each(data.result.data, function (index, row) {
  311. row.unshift(_initialIndex + index);
  312. self.result.data.push(row);
  313. _tempData.push(row);
  314. });
  315. $(document).trigger("renderData", {data: _tempData, snippet: self, initial: _initialIndex == 0});
  316. if (data.result.has_more && rows > 0) {
  317. setTimeout(function () {
  318. self.fetchResultData(rows, false);
  319. }, 500);
  320. }
  321. } else {
  322. self._ajax_error(data);
  323. $(document).trigger("renderDataError", {snippet: self});
  324. }
  325. }).fail(function (xhr, textStatus, errorThrown) {
  326. $(document).trigger("error", xhr.responseText);
  327. });
  328. };
  329. self.fetchResultMetadata = function () {
  330. $.post("/spark/api/fetch_result_metadata", {
  331. notebook: ko.mapping.toJSON(notebook),
  332. snippet: ko.mapping.toJSON(self),
  333. }, function (data) {
  334. if (data.status == 0) {
  335. self.result.meta(data.result.meta);
  336. } else {
  337. $(document).trigger("error", data.message);
  338. }
  339. }).fail(function (xhr, textStatus, errorThrown) {
  340. $(document).trigger("error", xhr.responseText);
  341. self.status('failed');
  342. });
  343. };
  344. self.checkStatus = function() {
  345. $.post("/spark/api/check_status", {
  346. notebook: ko.mapping.toJSON(notebook),
  347. snippet: ko.mapping.toJSON(self)
  348. }, function (data) {
  349. if (data.status == 0) {
  350. self.status(data.query_status.status);
  351. self.getLogs();
  352. if (self.status() == 'running') {
  353. self.result.endTime(new Date());
  354. self.checkStatusTimeout = setTimeout(self.checkStatus, 1000);
  355. }
  356. else if (self.status() == 'available') {
  357. self.fetchResult(100);
  358. self.progress(100);
  359. }
  360. } else {
  361. self._ajax_error(data);
  362. }
  363. }).fail(function (xhr, textStatus, errorThrown) {
  364. $(document).trigger("error", xhr.responseText);
  365. self.status('failed');
  366. });
  367. };
  368. self.cancel = function() {
  369. if (self.checkStatusTimeout != null) {
  370. clearTimeout(self.checkStatusTimeout);
  371. self.checkStatusTimeout = null;
  372. }
  373. $.post("/spark/api/cancel_statement", {
  374. notebook: ko.mapping.toJSON(notebook),
  375. snippet: ko.mapping.toJSON(self)
  376. }, function (data) {
  377. if (data.status == 0) {
  378. self.status('canceled');
  379. } else {
  380. self._ajax_error(data);
  381. }
  382. }).fail(function (xhr, textStatus, errorThrown) {
  383. $(document).trigger("error", xhr.responseText);
  384. self.status('failed');
  385. });
  386. };
  387. self.close = function() {
  388. if (self.checkStatusTimeout != null) {
  389. clearTimeout(self.checkStatusTimeout);
  390. self.checkStatusTimeout = null;
  391. }
  392. $.post("/spark/api/close_statement", {
  393. notebook: ko.mapping.toJSON(notebook),
  394. snippet: ko.mapping.toJSON(self)
  395. }, function (data) {
  396. if (data.status == 0) {
  397. self.status('closed');
  398. } else {
  399. self._ajax_error(data);
  400. }
  401. }).fail(function (xhr, textStatus, errorThrown) {
  402. $(document).trigger("error", xhr.responseText);
  403. self.status('failed');
  404. });
  405. };
  406. self.getLogs = function() {
  407. $.post("/spark/api/get_logs", {
  408. notebook: ko.mapping.toJSON(notebook),
  409. snippet: ko.mapping.toJSON(self)
  410. }, function (data) {
  411. if (data.status == 0) {
  412. self.result.logs(data.logs); // Way to append?
  413. self.progress(data.progress);
  414. } else {
  415. self._ajax_error(data);
  416. }
  417. }).fail(function (xhr, textStatus, errorThrown) {
  418. $(document).trigger("error", xhr.responseText);
  419. self.status('failed');
  420. });
  421. };
  422. self.init = function() {
  423. if (self.status() == 'running') {
  424. self.checkStatus();
  425. }
  426. if (self.status() != 'loading' && self.status() != 'ready') {
  427. self.getLogs();
  428. }
  429. };
  430. }
  431. var Notebook = function (vm, notebook) {
  432. var self = this;
  433. self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : null);
  434. self.uuid = ko.observable(typeof notebook.uuid != "undefined" && notebook.uuid != null ? notebook.uuid : UUID());
  435. self.name = ko.observable(typeof notebook.name != "undefined" && notebook.name != null ? notebook.name : 'My Notebook');
  436. self.snippets = ko.observableArray();
  437. self.selectedSnippet = ko.observable(vm.availableSnippets()[0].type());
  438. self.sessions = ko.mapping.fromJS(typeof notebook.sessions != "undefined" && notebook.sessions != null ? notebook.sessions : []);
  439. self.getSession = function(session_type) {
  440. var _s = null;
  441. $.each(self.sessions(), function (index, s) {
  442. if (s.type() == session_type) {
  443. _s = s;
  444. return false;
  445. }
  446. });
  447. return _s;
  448. };
  449. self.addSession = function(session) {
  450. var toRemove = []
  451. $.each(self.sessions(), function (index, s) {
  452. if (s.type() == session.type()) {
  453. toRemove.push(s);
  454. }
  455. });
  456. $.each(toRemove, function (index, s) {
  457. self.sessions.remove(s);
  458. });
  459. self.sessions.push(session);
  460. };
  461. self.addSnippet = function(snippet) {
  462. var _snippet = new Snippet(vm, self, snippet);
  463. self.snippets.push(_snippet);
  464. if (self.getSession(_snippet.type()) == null) {
  465. _snippet.create_session();
  466. }
  467. _snippet.init();
  468. };
  469. self.newSnippet = function() {
  470. var _snippet = new Snippet(vm, self, {type: self.selectedSnippet(), result: {}});
  471. self.snippets.push(_snippet);
  472. if (self.getSession(self.selectedSnippet()) == null) {
  473. _snippet.create_session();
  474. }
  475. else {
  476. _snippet.status('ready');
  477. }
  478. $(document).trigger("snippetAdded", _snippet);
  479. };
  480. if (notebook.snippets) {
  481. $.each(notebook.snippets, function(index, snippet) {
  482. self.addSnippet(snippet);
  483. });
  484. }
  485. self.save = function () {
  486. $.post("/spark/api/notebook/save", {
  487. "notebook": ko.mapping.toJSON(self)
  488. }, function (data) {
  489. if (data.status == 0) {
  490. self.id(data.id);
  491. $(document).trigger("info", data.message);
  492. if (window.location.search.indexOf("notebook") == -1) {
  493. window.location.hash = '#notebook=' + data.id;
  494. }
  495. }
  496. else {
  497. $(document).trigger("error", data.message);
  498. }
  499. }).fail(function (xhr, textStatus, errorThrown) {
  500. $(document).trigger("error", xhr.responseText);
  501. });
  502. };
  503. self.close = function () {
  504. if (self.id() != null) {
  505. $.post("/spark/api/notebook/close", {
  506. "notebook": ko.mapping.toJSON(self)
  507. });
  508. }
  509. };
  510. }
  511. function EditorViewModel(notebooks, options) {
  512. var self = this;
  513. self.notebooks = ko.observableArray();
  514. self.selectedNotebook = ko.observable();
  515. self.isEditing = ko.observable(false);
  516. self.isEditing.subscribe(function(newVal){
  517. $(document).trigger("editingToggled");
  518. });
  519. self.toggleEditing = function () {
  520. self.isEditing(! self.isEditing());
  521. };
  522. self.isAssistVisible = ko.observable(options.assistVisible);
  523. self.toggleAssist = function () {
  524. self.isAssistVisible(! self.isAssistVisible());
  525. $(document).trigger("toggleAssist");
  526. };
  527. self.isAssistVisible = ko.observable(options.assistVisible);
  528. self.assistContent = ko.observable();
  529. self.assistSelectedMainObject = ko.observable();
  530. self.availableSnippets = ko.mapping.fromJS(options.languages);
  531. self.snippetPlaceholders = options.snippet_placeholders;
  532. self.init = function() {
  533. $.each(notebooks, function(index, notebook) {
  534. self.loadNotebook(notebook);
  535. if (self.selectedNotebook() == null){
  536. self.selectedNotebook(self.notebooks()[0]);
  537. }
  538. });
  539. };
  540. self.loadNotebook = function(notebook) {
  541. var _n = new Notebook(self, notebook);
  542. self.notebooks.push(_n);
  543. if (_n.snippets().length > 0){
  544. _n.selectedSnippet(_n.snippets()[_n.snippets().length - 1].type());
  545. }
  546. };
  547. self.newNotebook = function() {
  548. self.notebooks.push(new Notebook(self, {}));
  549. self.selectedNotebook(self.notebooks()[self.notebooks().length - 1]);
  550. };
  551. self.saveNotebook = function() {
  552. self.selectedNotebook().save();
  553. };
  554. }
  555. function logGA(page) {
  556. if (typeof trackOnGA == 'function') {
  557. trackOnGA('editor/' + page);
  558. }
  559. }