as.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ;(function(){
  2. function as(el, gun, cb){
  3. el = $(el);
  4. if(gun === as.gui && as.el && as.el.is(el)){ return }
  5. as.gui = gun;
  6. as.el = el;
  7. if(el.data('as')){
  8. el.html(el.data('as').fresh);
  9. } else {
  10. el.data('as', {
  11. fresh: el.html()
  12. })
  13. }
  14. el.find("[name]").each(function(){
  15. if($(this).find("[name]").length){ return }
  16. var name = $(this),
  17. parents = name.parents("[name]"),
  18. path = [],
  19. ref = gun;
  20. path.push(name.attr('name'));
  21. parents.each(function(){
  22. path.push($(this).attr('name'));
  23. });
  24. path = path.reverse();
  25. path.forEach(function(key){
  26. if('#' === key){
  27. ref = ref.map()
  28. } else {
  29. ref = ref.get(key);
  30. }
  31. });
  32. var many = path.slice().reverse().indexOf('#'), model;
  33. many = (0 < ++many)? many : false;
  34. if(many){
  35. model = name.closest("[name='#']");
  36. model = model.data('model') || model.data('model', {$: model.clone(), on: model.parent(), has: {}}).hide().data('model');
  37. }
  38. ref.get(function(at){
  39. var data = at.put, key = at.get, gui = at.gun || at.$, ui = name, back;
  40. if(model){
  41. ui = model.has[(gui._).id];
  42. if(!ui){
  43. back = gui.back(many - 1);
  44. ui = model.has[(back._).id];
  45. if(!ui){
  46. if(!(back._).get){ return }
  47. ui = (model.has[(back._).id] = model.$.clone(true).prependTo(model.on));
  48. }
  49. ui = ui.find("[name='"+key+"']").first();
  50. model.has[(gui._).id] = ui;
  51. }
  52. }
  53. ui.data('gun', gui);
  54. if(ui.data('was') === data){ return }
  55. if(many && ui.is('.sort')){
  56. var up = ui.closest("[name='#']");
  57. var tmp = as.sort(data, up.parent().children().last());
  58. up.insertAfter(tmp);
  59. }
  60. if(as.lock === gui){ return }
  61. (ui[0] && u === ui[0].value)? ui.text(data) : ui.val(data);
  62. ui.data('was', data);
  63. if(cb){
  64. cb(data, key, ui);
  65. }
  66. });
  67. });
  68. }
  69. as.wait = function(cb, wait, to){
  70. return function(a,b,c){
  71. var me = as.typing = this;
  72. clearTimeout(to);
  73. to = setTimeout(function(){
  74. cb.call(me, a,b,c);
  75. as.typing = me = false;
  76. }, wait || 200);
  77. }
  78. }
  79. as.sort = function sort(id, li){
  80. var num = parseFloat(id);
  81. var id = $(li).find('.sort').text() || -Infinity;
  82. var at = num >= parseFloat(id);
  83. return at ? li : sort(id, li.prev());
  84. }
  85. $(document).on('keyup', 'input, textarea, [contenteditable]', as.wait(function(){
  86. var el = $(this);
  87. var data = (el[0] && u === el[0].value)? el.text() : el.val();
  88. var g = el.data('gun');
  89. if(!g){ return }
  90. as.lock = g;
  91. g.put(data);
  92. }, 99));
  93. $(document).on('submit', 'form', function(e){ e.preventDefault() });
  94. var u;
  95. window.as = as;
  96. $.as = as;
  97. }());
  98. ;(function(){
  99. $(document).on('click', 'a, button', function(e){
  100. var tmp = $(this).attr('href') || '';
  101. if(0 === tmp.indexOf('http')){ return }
  102. e.preventDefault();
  103. r(tmp);
  104. });
  105. function r(href){
  106. if(!href){ return }
  107. if(href[0] == '#'){ href = href.slice(1) }
  108. var h = href.split('/')[0];
  109. $('.page').hide();
  110. $('#' + h).show();
  111. if(r.on === h){ return }
  112. location.hash = href;
  113. (r.page[h] || {on:function(){}}).on();
  114. r.on = h;
  115. return r;
  116. };
  117. r.page = function(h, cb){
  118. r.page[h] = r.page[h] || {on: cb};
  119. return r;
  120. }
  121. r.render = function(id, model, onto, data){
  122. var $data = $(
  123. $('#' + id).get(0) ||
  124. $('.model').find(model).clone(true).attr('id', id).appendTo(onto)
  125. );
  126. $.each(data, function(field, val){
  127. if($.isPlainObject(val)){ return }
  128. $data.find("[name='" + field + "']").val(val).text(val);
  129. });
  130. return $data;
  131. }
  132. window.onhashchange = function(){ r(location.hash.slice(1)) };
  133. $.as && ($.as.route = r);
  134. if(window.as){
  135. as.route = r;
  136. } else {
  137. $.route = r;
  138. }
  139. }());
  140. ;$(function(){
  141. $('.page').not(':first').hide();
  142. $.as.route(location.hash.slice(1));
  143. });