Browse code

Initial commit

Benjamin Roth authored on26/03/2015 16:52:11
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,151 @@
1
+(function() {
2
+  var vexDialogFactory;
3
+
4
+  vexDialogFactory = function($, vex) {
5
+    var $formToObject, dialog;
6
+    if (vex == null) {
7
+      return $.error('Vex is required to use vex.dialog');
8
+    }
9
+    $formToObject = function($form) {
10
+      var object;
11
+      object = {};
12
+      $.each($form.serializeArray(), function() {
13
+        if (object[this.name]) {
14
+          if (!object[this.name].push) {
15
+            object[this.name] = [object[this.name]];
16
+          }
17
+          return object[this.name].push(this.value || '');
18
+        } else {
19
+          return object[this.name] = this.value || '';
20
+        }
21
+      });
22
+      return object;
23
+    };
24
+    dialog = {};
25
+    dialog.buttons = {
26
+      YES: {
27
+        text: 'OK',
28
+        type: 'submit',
29
+        className: 'vex-dialog-button-primary'
30
+      },
31
+      NO: {
32
+        text: 'Cancel',
33
+        type: 'button',
34
+        className: 'vex-dialog-button-secondary',
35
+        click: function($vexContent, event) {
36
+          $vexContent.data().vex.value = false;
37
+          return vex.close($vexContent.data().vex.id);
38
+        }
39
+      }
40
+    };
41
+    dialog.defaultOptions = {
42
+      callback: function(value) {},
43
+      afterOpen: function() {},
44
+      message: 'Message',
45
+      input: "<input name=\"vex\" type=\"hidden\" value=\"_vex-empty-value\" />",
46
+      value: false,
47
+      buttons: [dialog.buttons.YES, dialog.buttons.NO],
48
+      showCloseButton: false,
49
+      onSubmit: function(event) {
50
+        var $form, $vexContent;
51
+        $form = $(this);
52
+        $vexContent = $form.parent();
53
+        event.preventDefault();
54
+        event.stopPropagation();
55
+        $vexContent.data().vex.value = dialog.getFormValueOnSubmit($formToObject($form));
56
+        return vex.close($vexContent.data().vex.id);
57
+      },
58
+      focusFirstInput: true
59
+    };
60
+    dialog.defaultAlertOptions = {
61
+      message: 'Alert',
62
+      buttons: [dialog.buttons.YES]
63
+    };
64
+    dialog.defaultConfirmOptions = {
65
+      message: 'Confirm'
66
+    };
67
+    dialog.open = function(options) {
68
+      var $vexContent;
69
+      options = $.extend({}, vex.defaultOptions, dialog.defaultOptions, options);
70
+      options.content = dialog.buildDialogForm(options);
71
+      options.beforeClose = function($vexContent) {
72
+        return options.callback($vexContent.data().vex.value);
73
+      };
74
+      $vexContent = vex.open(options);
75
+      if (options.focusFirstInput) {
76
+        $vexContent.find('input[type="submit"], textarea, input[type="date"], input[type="datetime"], input[type="datetime-local"], input[type="email"], input[type="month"], input[type="number"], input[type="password"], input[type="search"], input[type="tel"], input[type="text"], input[type="time"], input[type="url"], input[type="week"]').first().focus();
77
+      }
78
+      return $vexContent;
79
+    };
80
+    dialog.alert = function(options) {
81
+      if (typeof options === 'string') {
82
+        options = {
83
+          message: options
84
+        };
85
+      }
86
+      options = $.extend({}, dialog.defaultAlertOptions, options);
87
+      return dialog.open(options);
88
+    };
89
+    dialog.confirm = function(options) {
90
+      if (typeof options === 'string') {
91
+        return $.error('dialog.confirm(options) requires options.callback.');
92
+      }
93
+      options = $.extend({}, dialog.defaultConfirmOptions, options);
94
+      return dialog.open(options);
95
+    };
96
+    dialog.prompt = function(options) {
97
+      var defaultPromptOptions;
98
+      if (typeof options === 'string') {
99
+        return $.error('dialog.prompt(options) requires options.callback.');
100
+      }
101
+      defaultPromptOptions = {
102
+        message: "<label for=\"vex\">" + (options.label || 'Prompt:') + "</label>",
103
+        input: "<input name=\"vex\" type=\"text\" class=\"vex-dialog-prompt-input\" placeholder=\"" + (options.placeholder || '') + "\"  value=\"" + (options.value || '') + "\" />"
104
+      };
105
+      options = $.extend({}, defaultPromptOptions, options);
106
+      return dialog.open(options);
107
+    };
108
+    dialog.buildDialogForm = function(options) {
109
+      var $form, $input, $message;
110
+      $form = $('<form class="vex-dialog-form" />');
111
+      $message = $('<div class="vex-dialog-message" />');
112
+      $input = $('<div class="vex-dialog-input" />');
113
+      $form.append($message.append(options.message)).append($input.append(options.input)).append(dialog.buttonsToDOM(options.buttons)).bind('submit.vex', options.onSubmit);
114
+      return $form;
115
+    };
116
+    dialog.getFormValueOnSubmit = function(formData) {
117
+      if (formData.vex || formData.vex === '') {
118
+        if (formData.vex === '_vex-empty-value') {
119
+          return true;
120
+        }
121
+        return formData.vex;
122
+      } else {
123
+        return formData;
124
+      }
125
+    };
126
+    dialog.buttonsToDOM = function(buttons) {
127
+      var $buttons;
128
+      $buttons = $('<div class="vex-dialog-buttons" />');
129
+      $.each(buttons, function(index, button) {
130
+        var $button;
131
+        $button = $("<input type=\"" + button.type + "\" />").val(button.text).addClass(button.className + ' vex-dialog-button ' + (index === 0 ? 'vex-first ' : '') + (index === buttons.length - 1 ? 'vex-last ' : '')).bind('click.vex', function(e) {
132
+          if (button.click) {
133
+            return button.click($(this).parents("." + vex.baseClassNames.content), e);
134
+          }
135
+        });
136
+        return $button.appendTo($buttons);
137
+      });
138
+      return $buttons;
139
+    };
140
+    return dialog;
141
+  };
142
+
143
+  if (typeof define === 'function' && define.amd) {
144
+    define(['jquery', 'vex'], vexDialogFactory);
145
+  } else if (typeof exports === 'object') {
146
+    module.exports = vexDialogFactory(require('jquery'), require('vex'));
147
+  } else {
148
+    window.vex.dialog = vexDialogFactory(window.jQuery, window.vex);
149
+  }
150
+
151
+}).call(this);