Here is all the code

License | Download
  1. /*
  2. * iIgnition, Copyright 2013 Lucid Ocean
  3. * License: MIT see http://www.iignition.com/#!license.html
  4. */
  5. var $i = iignition = (function () {
  6. var onReadyCallbacks = [];
  7. var consoleLines = [];
  8. var fireReady = false;
  9. fireOnReady = function () {
  10. for (var item in onReadyCallbacks) {
  11. if (onReadyCallbacks[item])
  12. onReadyCallbacks[item].callback();
  13. }
  14. fireReady = true;
  15. };
  16. $(function () {
  17. _init();
  18. _hashbang();
  19. fireOnReady();
  20. });
  21. function _init() {
  22. window.consoleSuppress = console;
  23. if (!$i.options.debug === true) {
  24. window.consoleSuppress = console;
  25. window.console = {};
  26. window.console.log = function () {
  27. consoleLines.push(arguments);
  28. };
  29. }
  30. else {
  31. console = window.consoleSuppress;
  32. }
  33. if ($i.Cache) {
  34. $.ajaxSetup({ cache: $i.options.enablecache });
  35. }
  36. }
  37. function _hashbang() {
  38. if (document.location.hash) {
  39. if (document.location.hash.startsWith("#!")) {
  40. $i.options.view = document.location.hash.replace("#!", "");
  41. }
  42. }
  43. }
  44. /** needed for IE **///
  45. if (typeof String.prototype.endsWith !== 'function') {
  46. String.prototype.endsWith = function (suffix) {
  47. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  48. };
  49. }
  50. if (typeof String.prototype.startsWith !== 'function') {
  51. String.prototype.startsWith = function (suffix) {
  52. return this.indexOf(suffix, 0) === 0;
  53. };
  54. }
  55. function _load(container, view, callback) {
  56. $(container).load(view, function (responseText, textStatus, XMLHttpRequest) {
  57. if (textStatus === "success") {
  58. if (callback)
  59. callback(responseText);
  60. }
  61. });
  62. }
  63. function _show(container, view, data, rowbindcallback, callback) {
  64. var $view = $(view);
  65. $view.appendTo($(container));
  66. _bind(container, data, rowbindcallback, function () {
  67. _constructor(view,callback);
  68. });
  69. }
  70. function _bind(container, data, rowbindcallback, callback) {
  71. var $container = $(container);
  72. if (data != undefined && data != null) {
  73. $i.Splash.map($container, data, rowbindcallback, function () {
  74. if (callback) callback();
  75. });
  76. }
  77. else {
  78. if (callback) callback();
  79. }
  80. };;
  81. function _constructor(view,callback) {
  82. view = view.substr(view.lastIndexOf("/") + 1);
  83. view = view.replace(".html", "");
  84. if (window[view]) {
  85. if (typeof window[view] === 'function') {
  86. window[view]();
  87. }
  88. };
  89. if (callback) {
  90. callback();
  91. }
  92. };
  93. /***end of IE *****/
  94. return {
  95. ready: function (options, callback) {
  96. $i.options = $.extend({ debug: false, enablecache: false }, options);
  97. _init();
  98. if (callback != undefined) {
  99. if (fireReady == false) {
  100. onReadyCallbacks.push({ callback: callback });
  101. } else {
  102. callback();
  103. }
  104. }
  105. $("a").css({ "pointer-events": "auto" });
  106. },
  107. show: function (container, view, data, rowbindcallback, callback) {
  108. if (view.endsWith(".html")) {
  109. _load(container, view, function (viewhtml) {
  110. _bind(container, data, rowbindcallback, function () {
  111. _constructor(view, callback);
  112. });
  113. });
  114. }
  115. else {
  116. _show(container, view, data, rowbindcallback, function () {
  117. _constructor(view, callback);
  118. });
  119. }
  120. if (window["_gaq"]) {
  121. _gaq.push(['_trackPageview', "#!" + view]);
  122. }
  123. document.location.hash = "#!" + view;
  124. },
  125. length: function (data) {
  126. if (data.length) {
  127. return data.length;
  128. }
  129. else {
  130. var size = 0, key;
  131. for (key in data) {
  132. if (data.hasOwnProperty(key)) size++;
  133. }
  134. return size;
  135. }
  136. },
  137. isMsg: function (obj) {
  138. if (typeof obj === "object") {
  139. if (obj !== undefined) {
  140. if (obj.Message !== undefined) {
  141. if (obj.MessageId) {
  142. return true;
  143. }
  144. }
  145. }
  146. }
  147. return false;
  148. },
  149. silentConsole: consoleLines
  150. }
  151. })();
  152. $i.Cache = (function () {
  153. var o = {};
  154. o.clear = function (name) {
  155. if (name) {
  156. localStorage.removeItem(["iCache_" + name]);
  157. }
  158. else {
  159. if ('localStorage' in window && window['localStorage'] !== null) {
  160. localStorage.clear();
  161. }
  162. }
  163. };
  164. o.Data = function (name, data) {
  165. if ('localStorage' in window && window['localStorage'] !== null) {
  166. if (data) {
  167. localStorage["iCache_" + name] = JSON.stringify(data);
  168. }
  169. else {
  170. if (localStorage["iCache_" + name]) {
  171. return JSON.parse(localStorage["iCache_" + name]);
  172. } else {
  173. return undefined;
  174. }
  175. }
  176. return true;
  177. }
  178. return false;
  179. };
  180. return o;
  181. })();
  182. $i.Data = (function () {
  183. var o = {};
  184. o.getData = function (url, data, successCallback, errorCallback) {
  185. //load from cache -this needs to be tightened up, but getData should read cache:DATAKEY and get the data as below
  186. if (url.indexOf("cache:") >= 0) {
  187. var s = url.split("cache:");
  188. if ($i.Cache && $i.options.enablecache) {
  189. var d = $i.Cache.Data(s[1]);
  190. console.log("loading cache: " + s[1]);
  191. if (d) {
  192. if (successCallback)
  193. successCallback(d);
  194. return;
  195. }
  196. else {
  197. url = s[1];
  198. }
  199. }
  200. else {
  201. url = s[1];
  202. }
  203. }
  204. function onSuccess(data) {
  205. if ($i.Cache) {
  206. $i.Cache.Data(url, data);
  207. }
  208. if (successCallback)
  209. successCallback(data);
  210. }
  211. function onError(xhr, status, error) {
  212. if (status === "timeout") {
  213. var ret = confirm("Timeout out occurred when trying to retrieve data. Would you like to retry?");
  214. console.log(ret);
  215. if (ret === true) {
  216. o.getData(url, data, successCallback, errorCallback, fullmsg);
  217. return;
  218. }
  219. }
  220. if (errorCallback)
  221. errorCallback(xhr.responseText, error);
  222. }
  223. var httptype = "get";
  224. var contentType = "text/javascript";
  225. var jsonp = true;
  226. var dataType = "jsonp";
  227. if (url.endsWith(".json")) {
  228. dataType = "json";
  229. contentType = "application/json";
  230. jsonp = false;
  231. }
  232. if (data) {
  233. if (data.DataUrl)
  234. data.DataUrl = "";
  235. if (data.ModelData)
  236. data.ModelData = "";
  237. }
  238. var processData = true;
  239. var hasFiles = false;
  240. var files = null;
  241. if (data) {
  242. httptype = "post";
  243. contentType = "application/json";
  244. if (data.viewName) {
  245. if ($("#" + data.ViewName + " input:file").length > 0) {
  246. hasFiles = true;
  247. $("#" + data.ViewName + " input:file")[0].files;
  248. }
  249. } else {
  250. if ($("input:file").length > 0) {
  251. files = $("input:file")[0].files;
  252. if (files.length > 0) {
  253. hasFiles = true;
  254. }
  255. }
  256. }
  257. }
  258. if (hasFiles === true) {
  259. contentType = "application/x-www-form-urlencoded";
  260. var formData = new FormData();
  261. processData = false;
  262. $.each(data, function (
  263. key,
  264. value) {
  265. formData.append(key, value);
  266. });
  267. $.each(files, function (
  268. key,
  269. value) {
  270. formData.append(key, value);
  271. });
  272. } else {
  273. formData = JSON.stringify(data);
  274. }
  275. console.log("fetching url: " + url);
  276. console.log(processData);
  277. console.log(formData);
  278. console.log(httptype);
  279. if ($i.Cache) {
  280. if ($i.options.enablecache === false) {
  281. url += "?icache=" + new Date().getTime();
  282. }
  283. }
  284. $.ajax({
  285. url: url,
  286. data: formData,
  287. type: httptype,
  288. dataType: dataType,
  289. success: onSuccess,
  290. error: onError,
  291. timeout: 1000 * 60, //1 minute
  292. cache: false,
  293. contentType: contentType,
  294. processData: processData,
  295. beforeSend: function (xhr) {
  296. }
  297. });
  298. };
  299. return o;
  300. })();
  301. $i.Splash = (function () {
  302. var o = {};
  303. o.element = null;
  304. o.map = function (element, data, rowbindcallback, callback, options) {
  305. if (data === undefined) {
  306. console.log("data is undefined. Splash aborted");
  307. return;
  308. }
  309. var datac = $.extend(true, {}, data);
  310. o.element = element;
  311. expandTemplate(element, datac);
  312. applyTemplate(element, datac, rowbindcallback, options);
  313. if (callback) {
  314. callback();
  315. }
  316. };
  317. o.setSelected = function (select, value) {
  318. $(select).val(value);
  319. };
  320. o.setRadio = function (radio, value) {
  321. $('input:radio#' + value).prop('checked', true);
  322. };
  323. o.setCheck = function (checkbox, value) {
  324. if (value) {
  325. if (value === 1 || value === "1" || value.toLowerCase() === "true") {
  326. $('input:checkbox#' + checkbox).attr('checked', 'checked');
  327. $('input:checkbox#' + checkbox).val('on');
  328. } else {
  329. $('input:checkbox#' + checkbox).removeAttr('checked');
  330. $('input:checkbox#' + checkbox).val('off');
  331. }
  332. }
  333. $('input:checkbox#' + checkbox).unbind("click");
  334. $('input:checkbox#' + checkbox).click(function () {
  335. if ($('input:checkbox#' + checkbox).is(':checked')) {
  336. $('input:checkbox#' + checkbox).val('on');
  337. }
  338. else {
  339. $('input:checkbox#' + checkbox).val('off');
  340. }
  341. });
  342. };
  343. function applyTemplate(element, data, rowbindcallback, options) {
  344. if (options && (options.append === true)) {
  345. }
  346. else {
  347. $(element).find("[data-templateclone]").not("[data-splashx]").remove();
  348. }
  349. $template = $(element).find("[data-splashtemplate]");
  350. if ($template.length === 0) {
  351. $template = $(element).filter("[data-splashtemplate]");
  352. if ($template.length === 0) {
  353. return;
  354. }
  355. }
  356. $cloneparent = $template.parent();
  357. if ($template.get(0).tagName === "TR") {
  358. $cloneparent = $template.parents("tbody");
  359. if ($cloneparent.length === 0) {
  360. $cloneparent = $template.parents("table");
  361. }
  362. } else if ($template.get(0).tagName === "FORM") {
  363. $cloneparent = $template.parents("[data-form]");
  364. }
  365. else if ($template.get(0).tagName === "DIV") {
  366. $cloneparent = $template.parent("div");
  367. }
  368. if (!$i.isMsg(data)) {
  369. var shouldRun = $i.length(data) > 0;
  370. shouldRun = options === undefined ? shouldRun : options.append === true;
  371. shouldRun = options === undefined ? shouldRun : options.action !== "edit";
  372. if (shouldRun) {
  373. var splashTemplate = $template.first();
  374. for (var d in data) {
  375. if ($(splashTemplate).parent().attr("id") !== $(o.element).attr("id")) {
  376. if ($(splashTemplate).get(0).tagName === "OPTION") {
  377. continue;
  378. }
  379. }
  380. var elementClone = $(splashTemplate).clone();
  381. $cloneparent.append($(elementClone));
  382. $(elementClone).attr("data-templateclone", "true");
  383. if (rowbindcallback) {
  384. var returneddata = undefined;
  385. if (data[d]) {
  386. returneddata = rowbindcallback(elementClone, data[d]);
  387. }
  388. if (returneddata !== undefined) {
  389. data[d] = returneddata;
  390. }
  391. }
  392. applyobject(elementClone, data[d]);
  393. $(elementClone).show();
  394. }
  395. $(splashTemplate).hide();
  396. if (options) {
  397. if (options.action == 'edit') {
  398. if ($template.parents("[data-form]")) {
  399. $template.first().remove();
  400. }
  401. }
  402. }
  403. }
  404. else {
  405. $template.each(function () {
  406. elementClone = $(this);
  407. if (rowbindcallback) {
  408. var returneddata = undefined;
  409. if (data[0]) {
  410. returneddata = rowbindcallback(elementClone, data[0]);
  411. }
  412. if (returneddata !== undefined) {
  413. data[0] = returneddata;
  414. }
  415. }
  416. if (data[0]) {
  417. applyobject(elementClone, data[0]);
  418. }
  419. });
  420. }
  421. } else {
  422. if (options) {
  423. if (options.action == 'edit') {
  424. } else {
  425. $template.hide();
  426. }
  427. }
  428. }
  429. }
  430. function applySplashx(element, data) {
  431. var $splashx = $(element).find("[data-splashx]");
  432. if ($splashx.length > 0) {
  433. $splashx.each(function () {
  434. var atts = $(this).attr("data-splashx");
  435. atts = atts.split("=");
  436. var name = "";
  437. if (atts.length === 2) {
  438. name = atts[1];
  439. }
  440. _selectedValue = $(this).attr("value");
  441. if (_selectedValue === data[name]) {
  442. $(this).attr("selected", "selected");
  443. }
  444. });
  445. $splashx.removeAttr("data-splashx");
  446. $splashx.removeAttr("data-splash");
  447. }
  448. }
  449. function expandTemplate(element, data) {
  450. $(element).find("*[data-splashscaffold]").each(function () {
  451. var elements = new Array();
  452. var $elementClone = "";
  453. $(this).find("*[data-splash^='\\[']").each(function () {
  454. $elementClone = $(this);
  455. var splashvalue = $(this).attr("data-splash").replace("[]", "");
  456. for (var d in data) {
  457. if (data.length > 0) {
  458. for (var col in data) {
  459. for (var obj in data[col]) {
  460. var $newtag = $elementClone.clone();
  461. $newtag.attr("data-splash", obj + splashvalue);
  462. elements.push($newtag);
  463. }
  464. break;//just do it once
  465. }
  466. }
  467. break;//just do it once
  468. }
  469. });
  470. $(this).find("*[data-splash^='\\[']").first().remove();
  471. $(this).append(elements);
  472. });
  473. }
  474. ;
  475. function applyobject(element, data) {
  476. var atts = $(element).data("splash");
  477. var _selectedValue = -1;
  478. if (atts) {
  479. atts = atts.split(",");
  480. for (var att in atts) {
  481. var name = atts[att].replace(/^\s+|\s+$/g, '');
  482. var attribute = "data-";
  483. var namepair = [];
  484. if (name.indexOf("=") > -1) {
  485. namepair = name.split("=");
  486. name = namepair[1];
  487. attribute = namepair[0];
  488. }
  489. else {
  490. attribute += name;
  491. }
  492. var val = undefined;
  493. var namespaces = name.split(".");
  494. var context = data;
  495. for (var i = 0; i < namespaces.length - 1; i++) {
  496. context = context[namespaces[i]];
  497. }
  498. if (namespaces.length > 0) {
  499. val = context[namespaces[namespaces.length - 1]];
  500. }
  501. else {
  502. if (data[name]) {
  503. val = data[name];
  504. }
  505. }
  506. if (namepair.length > 0) {
  507. if (attribute === "value") {
  508. if (val) {
  509. _selectedValue = data[name];
  510. }
  511. }
  512. if (attribute === "checked") {
  513. if ($(element).attr("id")) {
  514. o.setCheck($(element).attr("id"), val);
  515. }
  516. } else {
  517. $(element).attr(attribute, val);
  518. }
  519. }
  520. else {
  521. if (val !== undefined) {
  522. $(element).attr(attribute, val);
  523. if ($(element).get(0).tagName !== "TR") {
  524. if ($(element).attr("type") === "checkbox") {
  525. if ($(element).attr("id")) {
  526. o.setCheck($(element).attr("id"), val);
  527. }
  528. } else {
  529. var t = $(element).text();
  530. $(element).text(t + val);
  531. }
  532. }
  533. }
  534. }
  535. };
  536. }
  537. element.children().each(function () {
  538. if ($(this).attr("id") !== $(o.element).attr("id")) {
  539. if ($(this).get(0).tagName === "SELECT") {
  540. return;
  541. }
  542. }
  543. applyobject($(this), data);
  544. });
  545. $(element).show();
  546. }
  547. return o;
  548. })();