|
94 | 94 | this.options.onRefresh = window[onRefresh];
|
95 | 95 |
|
96 | 96 | // TODO: should we do this later on so we handle dynamically added buttons?
|
97 |
| - this.$element.find("[data-toggle=modal][href*='_griddlyIds']").each(function () |
| 97 | + this.$element.find("[data-griddly-toggle=modal][href*='_griddlyIds']").each(function () |
98 | 98 | {
|
99 | 99 | $(this).data("griddly-href-template", $(this).attr("href"));
|
100 | 100 | });
|
|
274 | 274 | else
|
275 | 275 | ids = "";
|
276 | 276 |
|
277 |
| - this.$element.find("[data-toggle=modal]").each(function () |
| 277 | + this.$element.find("[data-griddly-toggle=modal]").each(function () |
278 | 278 | {
|
279 | 279 | var template = $(this).data("griddly-href-template");
|
280 | 280 |
|
|
320 | 320 | onRowChange();
|
321 | 321 | }, this));
|
322 | 322 |
|
323 |
| - $(this.$element).on("click", "[data-toggle=ajaxbulk]", $.proxy(function (event) |
324 |
| - { |
325 |
| - var url = $(event.currentTarget).data("url"); |
326 |
| - var ids = this.getSelected(); |
327 |
| - |
328 |
| - if (ids.length == 0 && $(event.currentTarget).data("enable-on-selection")) |
329 |
| - return; |
330 |
| - |
331 |
| - $.ajax(url, |
332 |
| - { |
333 |
| - data: { ids : ids }, |
334 |
| - traditional: true, |
335 |
| - type: "POST" |
336 |
| - }).done($.proxy(function (data, status, xhr) |
337 |
| - { |
338 |
| - // TODO: handle errors |
339 |
| - // TODO: go back to first page? |
340 |
| - this.refresh(); |
341 |
| - }, this)); |
342 |
| - }, this)); |
343 |
| - |
344 |
| - $(this.$element).on("click", "[data-toggle=post]", $.proxy(function (event) |
345 |
| - { |
346 |
| - var url = $(event.currentTarget).data("url"); |
347 |
| - |
348 |
| - if (!url) |
349 |
| - url = $(event.currentTarget).attr("href"); |
350 |
| - |
351 |
| - var ids = this.getSelected(); |
352 |
| - var inputs = ""; |
353 |
| - |
354 |
| - if (ids.length == 0 && $(event.currentTarget).data("enable-on-selection")) |
355 |
| - return; |
356 |
| - |
357 |
| - var token = $("input[name^=__RequestVerificationToken]").first(); |
358 |
| - |
359 |
| - if (token.length) |
360 |
| - inputs += '<input type="hidden" name="' + token.attr("name") + '" value="' + token.val() + '" />'; |
361 |
| - |
362 |
| - $.each(ids, function () |
363 |
| - { |
364 |
| - inputs += "<input name=\"ids\" value=\"" + this + "\" />"; |
365 |
| - }); |
366 |
| - |
367 |
| - $("<form action=\"" + url + "\" method=\"post\">" + inputs + "</form>") |
368 |
| - .appendTo("body").submit().remove(); |
369 |
| - |
370 |
| - return false; |
371 |
| - }, this)); |
372 |
| - |
373 |
| - $(this.$element).on("click", "[data-toggle=postcriteria]", $.proxy(function (event) |
374 |
| - { |
375 |
| - var request = this.buildRequest(false); |
376 |
| - var inputs = ""; |
377 |
| - |
378 |
| - var token = $("input[name^=__RequestVerificationToken]").first(); |
379 |
| - |
380 |
| - if (token.length) |
381 |
| - inputs += '<input type="hidden" name="' + token.attr("name") + '" value="' + token.val() + '" />'; |
382 |
| - |
383 |
| - for (var key in request) |
384 |
| - inputs += '<input name="' + key + '" value="' + request[key] + '" />'; |
385 |
| - |
386 |
| - var url = $(event.currentTarget).data("url"); |
387 |
| - |
388 |
| - $("<form action=\"" + url + "\" method=\"post\">" + inputs + "</form>") |
389 |
| - .appendTo("body").submit().remove(); |
390 |
| - }, this)); |
391 |
| - |
392 |
| - $(this.$element).on("click", "[data-toggle=ajax]", $.proxy(function (event) |
393 |
| - { |
394 |
| - var url = $(event.currentTarget).data("url"); |
395 |
| - var ids = this.getSelected(); |
396 |
| - |
397 |
| - if (ids.length == 0) |
398 |
| - return; |
399 |
| - |
400 |
| - for (var i = 0; i < ids.length; i++) |
401 |
| - { |
402 |
| - $.ajax(url, |
403 |
| - { |
404 |
| - data: { id: ids[i] }, |
405 |
| - type: "POST" |
406 |
| - }).done($.proxy(function (data, status, xhr) |
407 |
| - { |
408 |
| - // TODO: handle errors |
409 |
| - // TODO: go back to first page? |
410 |
| - this.refresh(); |
411 |
| - }, this)); |
412 |
| - } |
413 |
| - }, this)); |
414 |
| - |
415 | 323 | $("a.export-xlsx", this.$element).on("click", $.proxy(function (e) {
|
416 | 324 | this.exportFile("xlsx");
|
417 | 325 | e.preventDefault();
|
|
922 | 830 | rowClickModal: null
|
923 | 831 | };
|
924 | 832 |
|
| 833 | + var GriddlyButton = function (element, options) { |
| 834 | + this.$element = $(element); |
| 835 | + this.options = options; |
| 836 | + |
| 837 | + this.create(); |
| 838 | + |
| 839 | + if ($(this.$element).is("[data-enable-on-selection=true]")) { |
| 840 | + $(this.$element).addClass("disabled"); |
| 841 | + } |
| 842 | + }; |
| 843 | + |
| 844 | + GriddlyButton.prototype = { |
| 845 | + constructor: GriddlyButton, |
| 846 | + |
| 847 | + // create and bind |
| 848 | + create: function () { |
| 849 | + var griddly = this.$element.closest("[data-role=griddly]"); |
| 850 | + var url = this.$element.data("url"); |
| 851 | + var toggle = this.$element.data("toggle"); |
| 852 | + var onclick = this.$element.data("onclick"); |
| 853 | + var confirmMessage = this.$element.data("confirm-message"); |
| 854 | + var enableOnSelection = this.$element.data("enable-on-selection"); |
| 855 | + |
| 856 | + $(this.$element).on("click", $.proxy(function (event) { |
| 857 | + if ((typeof confirmMessage === "undefined" || confirm(confirmMessage))) { |
| 858 | + if ($(this.$element).triggerHandler("beforeExecute") !== false) { |
| 859 | + if (toggle && ["ajaxbulk", "postcriteria", "ajax", "post"].indexOf(toggle) > -1) { |
| 860 | + if (!url) |
| 861 | + url = $(event.currentTarget).attr("href"); |
| 862 | + |
| 863 | + var ids = {}; |
| 864 | + if (griddly) |
| 865 | + ids = $(griddly).griddly("getSelected"); |
| 866 | + |
| 867 | + switch (toggle) { |
| 868 | + case "ajaxbulk": |
| 869 | + if (ids.length == 0 && enableOnSelection) |
| 870 | + return; |
| 871 | + return this.ajaxBulk(url, ids); |
| 872 | + |
| 873 | + case "post": |
| 874 | + if (ids.length == 0 && enableOnSelection) |
| 875 | + return; |
| 876 | + return this.post(url, ids); |
| 877 | + |
| 878 | + case "postcriteria": |
| 879 | + if (!griddly) |
| 880 | + return; |
| 881 | + return this.postCriteria(url, $(griddly).griddly("buildRequest")); |
| 882 | + |
| 883 | + case "ajax": |
| 884 | + if (ids.length == 0) |
| 885 | + return; |
| 886 | + return this.ajax(url, ids); |
| 887 | + } |
| 888 | + } |
| 889 | + |
| 890 | + if (onclick) { |
| 891 | + var f = window[onclick]; |
| 892 | + if ($.isFunction(f)) |
| 893 | + return f.call(this.$element); |
| 894 | + throw "onclick must be a global function"; |
| 895 | + // we do not support eval cause it's insecure |
| 896 | + } |
| 897 | + |
| 898 | + return true; |
| 899 | + } |
| 900 | + } |
| 901 | + |
| 902 | + return false; |
| 903 | + }, this)); |
| 904 | + }, |
| 905 | + |
| 906 | + ajaxBulk: function (url, ids) { |
| 907 | + $.ajax(url, |
| 908 | + { |
| 909 | + data: { ids: ids }, |
| 910 | + traditional: true, |
| 911 | + type: "POST" |
| 912 | + }).done($.proxy(function (data, status, xhr) { |
| 913 | + // TODO: handle errors |
| 914 | + // TODO: go back to first page? |
| 915 | + this.refresh(); |
| 916 | + |
| 917 | + $(this.$element).triggerHandler("afterExecute", [data, status, xhr]); |
| 918 | + }, this)); |
| 919 | + }, |
| 920 | + |
| 921 | + post: function (url, ids) { |
| 922 | + var inputs = ""; |
| 923 | + |
| 924 | + var token = $("input[name^=__RequestVerificationToken]").first(); |
| 925 | + |
| 926 | + if (token.length) |
| 927 | + inputs += '<input type="hidden" name="' + token.attr("name") + '" value="' + token.val() + '" />'; |
| 928 | + |
| 929 | + $.each(ids, function () { |
| 930 | + inputs += "<input name=\"ids\" value=\"" + this + "\" />"; |
| 931 | + }); |
| 932 | + |
| 933 | + $("<form action=\"" + url + "\" method=\"post\">" + inputs + "</form>") |
| 934 | + .appendTo("body").submit().remove(); |
| 935 | + |
| 936 | + return false; |
| 937 | + }, |
| 938 | + |
| 939 | + postCriteria: function (url, request) { |
| 940 | + var inputs = ""; |
| 941 | + |
| 942 | + var token = $("input[name^=__RequestVerificationToken]").first(); |
| 943 | + |
| 944 | + if (token.length) |
| 945 | + inputs += '<input type="hidden" name="' + token.attr("name") + '" value="' + token.val() + '" />'; |
| 946 | + |
| 947 | + for (var key in request) |
| 948 | + inputs += '<input name="' + key + '" value="' + request[key] + '" />'; |
| 949 | + |
| 950 | + $("<form action=\"" + url + "\" method=\"post\">" + inputs + "</form>") |
| 951 | + .appendTo("body").submit().remove(); |
| 952 | + }, |
| 953 | + |
| 954 | + ajax: function (url, ids) { |
| 955 | + for (var i = 0; i < ids.length; i++) { |
| 956 | + $.ajax(url, |
| 957 | + { |
| 958 | + data: { id: ids[i] }, |
| 959 | + type: "POST" |
| 960 | + }).done($.proxy(function (data, status, xhr) { |
| 961 | + // TODO: handle errors |
| 962 | + // TODO: go back to first page? |
| 963 | + this.refresh(); |
| 964 | + |
| 965 | + $(this.$element).triggerHandler("afterExecute", [data, status, xhr]); |
| 966 | + }, this)); |
| 967 | + } |
| 968 | + }, |
| 969 | + }; |
| 970 | + |
| 971 | + $.fn.griddlyButton = function (option, parameter) { |
| 972 | + var value; |
| 973 | + var args = arguments; |
| 974 | + |
| 975 | + this.each(function () { |
| 976 | + var data = $(this).data('griddly'), |
| 977 | + options = typeof option == 'object' && option; |
| 978 | + |
| 979 | + // initialize griddly button |
| 980 | + if (!data) { |
| 981 | + var instanceOptions = $.extend({}, $.fn.griddlyButton.defaults, options); |
| 982 | + |
| 983 | + $(this).data('griddly', (data = new GriddlyButton(this, instanceOptions))); |
| 984 | + } |
| 985 | + |
| 986 | + // call griddly method |
| 987 | + if (typeof option == 'string') { |
| 988 | + value = data[option].apply(data, Array.prototype.slice.call(args, 1)); |
| 989 | + } |
| 990 | + |
| 991 | + }); |
| 992 | + |
| 993 | + if (value !== undefined) |
| 994 | + return value; |
| 995 | + else |
| 996 | + return this; |
| 997 | + }; |
| 998 | + |
| 999 | + $.fn.griddlyButton.defaults = |
| 1000 | + { |
| 1001 | + }; |
| 1002 | + |
925 | 1003 | $(function()
|
926 | 1004 | {
|
927 | 1005 | $("[data-role=griddly]").griddly();
|
| 1006 | + $("[data-role=griddly-button]").griddlyButton(); |
928 | 1007 |
|
929 | 1008 | // patch stupid bootstrap js so it doesn't .empty() our inline filter dropdowns
|
930 | 1009 | // remove once bs fixes: https://github.com/twbs/bootstrap/pull/14244
|
|
0 commit comments