Skip to content
This repository

Added compatibility with nette.ajax.js #42

Open
wants to merge 1 commit into from

2 participants

Vojtěch Dobeš Lukáš Gavenda
Vojtěch Dobeš

I have not tested it, but this change makes the script compatible with nette.ajax.js while it remains fully working without it. It also gets rid of live which is deprecated, and does not affect global state of the application with ajaxSetup and ajaxStop calls anymore.

Vojtěch Dobeš

@iguana007 Díky za test - čím to je, že to nemůže najít template? Jak to může souviset s mojí úpravou?

Lukáš Gavenda

@vojtech-dobes to netuším, nepodařilo se mi najít důvod (nevím jak to v tom JS odkrokovat), ale tipnul bych si, že se to někde snaží volat actionNiftygrid - protože s tím původním grid.js to nedělá.
Konkrétně mi do Firebugu spadne error:
Nette\Application\BadRequestException: Page not found. Missing template '…\appRoot\app\SomeModule\templates\Foo\niftygrid.latte'. #40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 14, 2013
Vojtěch Dobeš vojtech-dobes Added compatibility with nette.ajax.js c84466b
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 153 additions and 132 deletions. Show diff stats Hide diff stats

  1. +153 132 resources/js/grid.js
285 resources/js/grid.js
... ... @@ -1,115 +1,6 @@
1   -$(function(){
2   - $.ajaxSetup({
3   - success: function(data){
4   - if(data.redirect){
5   - $.get(data.redirect);
6   - }
7   - if(data.snippets){
8   - for (var snippet in data.snippets){
9   - $("#"+snippet).html(data.snippets[snippet]);
10   - }
11   - }
12   - }
13   - });
14   -
15   - $(".grid-flash-hide").live("click", function(){
16   - $(this).parent().parent().fadeOut(300);
17   - });
18   -
19   - $(".grid-select-all").live("click", function(){
20   - var checkboxes = $(this).parents("thead").siblings("tbody").children("tr:not(.grid-subgrid-row)").find("td input:checkbox.grid-action-checkbox");
21   - if($(this).is(":checked")){
22   - $(checkboxes).attr("checked", "checked");
23   - }else{
24   - $(checkboxes).removeAttr("checked");
25   - }
26   - });
27   -
28   - $('.grid a.grid-ajax:not(.grid-confirm)').live('click', function (event) {
29   - event.preventDefault();
30   - $.get(this.href);
31   - });
32   -
33   - $('.grid a.grid-confirm:not(.grid-ajax)').live('click', function (event) {
34   - var answer = confirm($(this).data("grid-confirm"));
35   - return answer;
36   - });
37   -
38   - $('.grid a.grid-confirm.grid-ajax').live('click', function (event) {
39   - event.preventDefault();
40   - var answer = confirm($(this).data("grid-confirm"));
41   - if(answer){
42   - $.get(this.href);
43   - }
44   - });
45   -
46   - $(".grid-gridForm").find("input[type=submit]").live("click", function(){
47   - $(this).addClass("grid-gridForm-clickedSubmit");
48   - });
49   -
50   -
51   - $(".grid-gridForm").live("submit", function(event){
52   - var button = $(".grid-gridForm-clickedSubmit");
53   - $(button).removeClass("grid-gridForm-clickedSubmit");
54   - if($(button).data("select")){
55   - var selectName = $(button).data("select");
56   - var option = $("select[name=\""+selectName+"\"] option:selected");
57   - if($(option).data("grid-confirm")){
58   - var answer = confirm($(option).data("grid-confirm"));
59   - if(answer){
60   - if($(option).hasClass("grid-ajax")){
61   - event.preventDefault();
62   - $.post(this.action, $(this).serialize()+"&"+$(button).attr("name")+"="+$(button).val());
63   - }
64   - }else{
65   - return false;
66   - }
67   - }else{
68   - if($(option).hasClass("grid-ajax")){
69   - event.preventDefault();
70   - $.post(this.action, $(this).serialize()+"&"+$(button).attr("name")+"="+$(button).val());
71   - }
72   - }
73   - }else{
74   - event.preventDefault();
75   - $.post(this.action, $(this).serialize()+"&"+$(button).attr("name")+"="+$(button).val());
76   - }
77   - });
78   -
79   - $(".grid-autocomplete").live('keydown.autocomplete', function(){
80   - var gridName = $(this).data("gridname");
81   - var column = $(this).data("column");
82   - var link = $(this).data("link");
83   - $(this).autocomplete({
84   - source: function(request, response) {
85   - $.ajax({
86   - url: link,
87   - data: gridName+'-term='+request.term+'&'+gridName+'-column='+column,
88   - dataType: "json",
89   - method: "post",
90   - success: function(data) {
91   - response(data.payload);
92   - }
93   - });
94   - },
95   - delay: 100,
96   - open: function() { $('.ui-menu').width($(this).width()) }
97   - });
98   - });
99   -
100   - $(".grid-changeperpage").live("change", function(){
101   - $.get($(this).data("link"), $(this).data("gridname")+"-perPage="+$(this).val());
102   - });
103   -
104   - function hidePerPageSubmit()
105   - {
106   - $(".grid-perpagesubmit").hide();
107   - }
108   - hidePerPageSubmit();
109   -
110   - function setDatepicker()
111   - {
112   - if ( ! $.datepicker ) return;
  1 +(function (undefined) {
  2 + function niftyGridInitialize() {
  3 + if (!$.datepicker) return;
113 4
114 5 $.datepicker.regional['cs'] = {
115 6 closeText: 'Zavřít',
@@ -129,32 +20,162 @@ $(function(){
129 20 firstDay: 1,
130 21 isRTL: false,
131 22 showMonthAfterYear: false,
132   - yearSuffix: ''};
  23 + yearSuffix: ''
  24 + };
133 25 $.datepicker.setDefaults($.datepicker.regional['cs']);
  26 + }
134 27
135   - $(".grid-datepicker").each(function(){
136   - if(($(this).val() != "")){
137   - var date = $.datepicker.formatDate('yy-mm-dd', new Date($(this).val()));
  28 + function niftyGrid(ajax) {
  29 + $('.grid-flash-hide').off('click.niftygrid').on('click.niftygrid', function () {
  30 + $(this).parent().parent().fadeOut(300);
  31 + });
  32 +
  33 + $('.grid-select-all').off('click.niftygrid').on('click.niftygrid', function () {
  34 + var checkboxes = $(this).parents('thead').siblings('tbody').children('tr:not(.grid-subgrid-row)').find('td input:checkbox.grid-action-checkbox');
  35 + if ($(this).is(':checked')) {
  36 + checkboxes.attr('checked', 'checked');
  37 + } else {
  38 + checkboxes.removeAttr('checked');
138 39 }
139   - $(this).datepicker();
140   - $(this).datepicker({ constrainInput: false});
141 40 });
142   - }
143   - setDatepicker();
144 41
145   - $(this).ajaxStop(function(){
146   - setDatepicker();
147   - hidePerPageSubmit();
148   - });
  42 + $('.grid a.grid-ajax:not(.grid-confirm)').off('click.niftygrid').on('click.niftygrid', function (e) {
  43 + ajax({}, this, e);
  44 + });
149 45
150   - $("input.grid-editable").live("keypress", function(e) {
151   - if (e.keyCode == '13') {
  46 + $('.grid a.grid-confirm:not(.grid-ajax)').off('click.niftygrid').on('click.niftygrid', function () {
  47 + return confirm($(this).data('grid-confirm'));
  48 + });
  49 +
  50 + $('.grid a.grid-confirm.grid-ajax').off('click.niftygrid').on('click.niftygrid', function (e) {
152 51 e.preventDefault();
153   - $("input[type=submit].grid-editable").click();
  52 + var answer = confirm($(this).data('grid-confirm'));
  53 + if (answer) {
  54 + ajax({}, this, e);
  55 + }
  56 + });
  57 +
  58 + $('.grid-gridForm').find('input[type=submit]').off('click.niftygrid').on('click.niftygrid', function () {
  59 + $(this).addClass('grid-gridForm-clickedSubmit');
  60 + });
  61 +
  62 + $('.grid-gridForm').off('submit.niftygrid').on('submit.niftygrid', function (e) {
  63 + var button = $(this).find('.grid-gridForm-clickedSubmit');
  64 + button.removeClass('grid-gridForm-clickedSubmit');
  65 + if (button.data('select')) {
  66 + var selectName = button.data('select');
  67 + var option = $('select[name="' + selectName + '"] option:selected');
  68 + var answer = option.data("grid-confirm");
  69 + if (answer) {
  70 + if (confirm(answer)) {
  71 + if (option.hasClass('grid-ajax')) {
  72 + ajax({}, this, e);
  73 + }
  74 + } else {
  75 + return false;
  76 + }
  77 + } else {
  78 + if (option.hasClass('grid-ajax')) {
  79 + ajax({}, this, e);
  80 + }
  81 + }
  82 + } else {
  83 + ajax({}, this, e);
  84 + }
  85 + });
  86 +
  87 + $('.grid-autocomplete').off('keydown.autocomplete').on('keydown.autocomplete', function () {
  88 + var autocomplete = $(this);
  89 + var gridName = autocomplete.data('gridname');
  90 + var column = autocomplete.data('column');
  91 + var link = autocomplete.data('link');
  92 + autocomplete.autocomplete({
  93 + source: function (request, response) {
  94 + $.ajax({
  95 + url: link,
  96 + data: gridName + '-term=' + request.term + '&' + gridName + '-column=' + column,
  97 + dataType: 'json',
  98 + method: 'post'
  99 + }).done(function(data) {
  100 + response(data.payload);
  101 + });
  102 + },
  103 + delay: 100,
  104 + open: function() {
  105 + $('.ui-menu').width($(this).width());
  106 + }
  107 + });
  108 + });
  109 +
  110 + $('.grid-changeperpage').off('change.niftygrid').on('change.niftygrid', function () {
  111 + ajax({
  112 + type: 'get',
  113 + url: $(this).data('link'),
  114 + data: $(this).data('gridname') + '-perPage=' + $(this).val()
  115 + });
  116 + });
  117 +
  118 + $('.grid-perpagesubmit').hide();
  119 +
  120 + if ($.datepicker) {
  121 + $('.grid-datepicker').each(function () {
  122 + if ($(this).val() != '') {
  123 + var date = $.datepicker.formatDate('yy-mm-dd', new Date($(this).val()));
  124 + }
  125 + $(this).datepicker();
  126 + $(this).datepicker({
  127 + constrainInput: false
  128 + });
  129 + });
154 130 }
155   - });
156 131
157   - $("table.grid tbody tr:not(.grid-subgrid-row) td.grid-data-cell").live("dblclick", function(e) {
158   - $(this).parent().find("a.grid-editable:first").click();
159   - });
160   -});
  132 + $('input.grid-editable').off('keypress.niftygrid').on('keypress.niftygrid', function (e) {
  133 + if (e.keyCode == '13') {
  134 + e.preventDefault();
  135 + $('input[type=submit].grid-editable').click();
  136 + }
  137 + });
  138 +
  139 + $('table.grid tbody tr:not(.grid-subgrid-row) td.grid-data-cell').off('dblclick.niftygrid').on('dblclick.niftygrid', function (e) {
  140 + $(this).parent().find('a.grid-editable:first').click();
  141 + });
  142 + }
  143 +
  144 + if (typeof $.nette.ajax !== 'undefined') {
  145 + $.nette.ajax('niftyGrid', {
  146 + init: niftyGridInitialize,
  147 + load: function () {
  148 + niftyGrid($.nette.ajax);
  149 + }
  150 + });
  151 + } else {
  152 + function ajax(params, ui, e) {
  153 + e.preventDefault();
  154 + if ($(ui).is('form')) {
  155 + var button = $(ui).find('.grid-gridForm-clickedSubmit');
  156 + params.url = ui.action;
  157 + params.type = 'post';
  158 + params.data = $(ui).serialize() + '&' + button.attr('name') + '=' + button.val();
  159 + } else {
  160 + params.url = ui.href;
  161 + params.type = 'get';
  162 + }
  163 + return $.ajax(params).done(function () {
  164 + if (data.redirect) {
  165 + $.get(data.redirect);
  166 + }
  167 + if (data.snippets) {
  168 + for (var snippet in data.snippets) {
  169 + $('#' + snippet).html(data.snippets[snippet]);
  170 + }
  171 + }
  172 + niftyGrid(ajax);
  173 + });
  174 + };
  175 +
  176 + niftyGridInitialize();
  177 + $(function () {
  178 + niftyGrid(ajax);
  179 + });
  180 + }
  181 +})();

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.