浏览代码

shortcuts

Robert Carnecky 10 年前
父节点
当前提交
a82b2777c4
共有 4 个文件被更改,包括 270 次插入10 次删除
  1. 3
    1
      generator/generate.html
  2. 8
    4
      generator/js/card.ts
  3. 36
    5
      generator/js/ui.ts
  4. 223
    0
      generator/lib/shortcut/shortcut.js

+ 3
- 1
generator/generate.html 查看文件

11
     <link rel="stylesheet" type="text/css" href="lib/bootstrap/css/bootstrap-theme.min.css" />
11
     <link rel="stylesheet" type="text/css" href="lib/bootstrap/css/bootstrap-theme.min.css" />
12
     <!-- Library: Bootstrap typeahead -->
12
     <!-- Library: Bootstrap typeahead -->
13
     <script type="text/javascript" src="lib/typeahead/bootstrap3-typeahead.min.js" charset="utf-8"></script>
13
     <script type="text/javascript" src="lib/typeahead/bootstrap3-typeahead.min.js" charset="utf-8"></script>
14
+    <!-- Library: Shortcut -->
15
+    <script type="text/javascript" src="lib/shortcut/shortcut.js" charset="utf-8"></script>
14
     <!-- Library: Color picker -->
16
     <!-- Library: Color picker -->
15
     <link href="lib/colorpicker/css/bootstrap-colorselector.css" rel="stylesheet" />
17
     <link href="lib/colorpicker/css/bootstrap-colorselector.css" rel="stylesheet" />
16
     <script type="text/javascript" src="lib/colorpicker/js/bootstrap-colorselector.js" charset="utf-8"></script>
18
     <script type="text/javascript" src="lib/colorpicker/js/bootstrap-colorselector.js" charset="utf-8"></script>
211
                     <div class="form-group">
213
                     <div class="form-group">
212
                         <label for="selected-card" class="col-sm-2 control-label">Deck</label>
214
                         <label for="selected-card" class="col-sm-2 control-label">Deck</label>
213
                         <div class="col-sm-10">
215
                         <div class="col-sm-10">
214
-                            <p class="form-control-static" id="total_card_count">Deck contains 0 cards.</p>
216
+                            <p class="form-control-static" id="total_card_count">This deck contains 0 cards.</p>
215
                         </div>
217
                         </div>
216
                     </div>
218
                     </div>
217
                     <div class="form-group">
219
                     <div class="form-group">

+ 8
- 4
generator/js/card.ts 查看文件

146
             return this.color_back || this.color || options.default_color || "black";
146
             return this.color_back || this.color || options.default_color || "black";
147
         }
147
         }
148
         public getIconFront(options: Options): string {
148
         public getIconFront(options: Options): string {
149
-            return this.icon_front || this.icon || options.default_icon || "ace";
149
+            return this.icon_front || this.icon || options.default_icon || "";
150
         }
150
         }
151
         public getIconBack(options: Options): string {
151
         public getIconBack(options: Options): string {
152
-            return this.icon_back || this.icon || options.default_icon || "ace";
152
+            return this.icon_back || this.icon || options.default_icon || "";
153
         }
153
         }
154
     };
154
     };
155
 
155
 
234
         constructor() {
234
         constructor() {
235
         }
235
         }
236
 
236
 
237
-        private  _icon(src: string, ind: string, ind0: string): string {
238
-            return ind + '<card-icon src="/icons/' + src + '.svg"></card-icon>\n';
237
+        private _icon(src: string, ind: string, ind0: string): string {
238
+            if (src.length > 0) {
239
+                return ind + '<card-icon src="/icons/' + src + '.svg"></card-icon>\n';
240
+            } else {
241
+                return "";
242
+            }
239
         }
243
         }
240
 
244
 
241
         private  _subtitle(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
245
         private  _subtitle(params: string[], card: Card, options: Options, ind: string, ind0: string): string {

+ 36
- 5
generator/js/ui.ts 查看文件

4
 /// <reference path="./example_data.ts" />
4
 /// <reference path="./example_data.ts" />
5
 /// <reference path="./jquery.d.ts" />
5
 /// <reference path="./jquery.d.ts" />
6
 /// <reference path="./ace.d.ts" />
6
 /// <reference path="./ace.d.ts" />
7
+/// <reference path="./shortcut.d.ts" />
7
 
8
 
8
 module RpgCardsUI {
9
 module RpgCardsUI {
9
 
10
 
31
     }
32
     }
32
 
33
 
33
     function select_card_by_index(index: number) {
34
     function select_card_by_index(index: number) {
34
-        if (index > -1) {
35
-            $("#selected-card").val("" + index);
35
+        var size = deck.cards.length;
36
+        if (size === 0) {
37
+            $("#selected-card").val("");
36
         } else {
38
         } else {
37
-            $("#selected-card").val("" + (deck.cards.length - 1));
39
+            index = Math.min(size - 1, index);
40
+            index = Math.max(0, index);
41
+            $("#selected-card").val("" + index);
38
         }
42
         }
39
         update_selected_card();
43
         update_selected_card();
40
     }
44
     }
41
 
45
 
46
+    function select_first_card() {
47
+        select_card_by_index(0);
48
+    }
49
+
50
+    function select_last_card() {
51
+        select_card_by_index(deck.cards.length - 1);
52
+    }
53
+
54
+    function select_next_card() {
55
+        select_card_by_index(selected_card_index() + 1);
56
+    }
57
+
58
+    function select_prev_card() {
59
+        select_card_by_index(selected_card_index() - 1);
60
+    }
61
+
42
     function select_card_by_card(card: RpgCards.Card) {
62
     function select_card_by_card(card: RpgCards.Card) {
43
         var index = deck.cards.indexOf(card);
63
         var index = deck.cards.indexOf(card);
44
         select_card_by_index(index);
64
         select_card_by_index(index);
89
 
109
 
90
     function update_card_list() {
110
     function update_card_list() {
91
         deck.commit();
111
         deck.commit();
92
-        $("#total_card_count").text("Deck contains " + deck.cards.length + " unique cards.");
112
+        $("#total_card_count").text("This deck contains " + deck.cards.length + " unique cards.");
93
 
113
 
94
         $('#selected-card').empty();
114
         $('#selected-card').empty();
95
         for (var i = 0; i < deck.cards.length; ++i) {
115
         for (var i = 0; i < deck.cards.length; ++i) {
496
         setup_color_selector();
516
         setup_color_selector();
497
         (<any>$('.icon-list')).typeahead({ source: icon_names });
517
         (<any>$('.icon-list')).typeahead({ source: icon_names });
498
 
518
 
499
-
500
         // Menu
519
         // Menu
501
         $("#sort-execute").click(sort_execute);
520
         $("#sort-execute").click(sort_execute);
502
         $("#filter-execute").click(filter_execute);
521
         $("#filter-execute").click(filter_execute);
543
 
562
 
544
         $(".icon-select-button").click(select_icon);
563
         $(".icon-select-button").click(select_icon);
545
 
564
 
565
+        // Shortcuts
566
+        var shortcut_options = { disable_in_input: true };
567
+        shortcut.add("ctrl+n", add_new_card, shortcut_options);
568
+        shortcut.add("ctrl+d", duplicate_card, shortcut_options);
569
+        shortcut.add("delete", delete_card, shortcut_options);
570
+        shortcut.add("down", select_next_card, shortcut_options);
571
+        shortcut.add("page_down", select_next_card, shortcut_options);
572
+        shortcut.add("up", select_prev_card, shortcut_options);
573
+        shortcut.add("page_up", select_prev_card, shortcut_options);
574
+        shortcut.add("home", select_first_card, shortcut_options);
575
+        shortcut.add("end", select_last_card, shortcut_options);
576
+
546
         update_card_list();
577
         update_card_list();
547
     }
578
     }
548
 
579
 

+ 223
- 0
generator/lib/shortcut/shortcut.js 查看文件

1
+/**
2
+ * http://www.openjs.com/scripts/events/keyboard_shortcuts/
3
+ * Version : 2.01.B
4
+ * By Binny V A
5
+ * License : BSD
6
+ */
7
+shortcut = {
8
+	'all_shortcuts':{},//All the shortcuts are stored in this array
9
+	'add': function(shortcut_combination,callback,opt) {
10
+		//Provide a set of default options
11
+		var default_options = {
12
+			'type':'keydown',
13
+			'propagate':false,
14
+			'disable_in_input':false,
15
+			'target':document,
16
+			'keycode':false
17
+		}
18
+		if(!opt) opt = default_options;
19
+		else {
20
+			for(var dfo in default_options) {
21
+				if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
22
+			}
23
+		}
24
+
25
+		var ele = opt.target;
26
+		if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
27
+		var ths = this;
28
+		shortcut_combination = shortcut_combination.toLowerCase();
29
+
30
+		//The function to be called at keypress
31
+		var func = function(e) {
32
+			e = e || window.event;
33
+			
34
+			if(opt['disable_in_input']) { //Don't enable shortcut keys in Input, Textarea fields
35
+				var element;
36
+				if(e.target) element=e.target;
37
+				else if(e.srcElement) element=e.srcElement;
38
+				if(element.nodeType==3) element=element.parentNode;
39
+
40
+				if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
41
+			}
42
+	
43
+			//Find Which key is pressed
44
+			if (e.keyCode) code = e.keyCode;
45
+			else if (e.which) code = e.which;
46
+			var character = String.fromCharCode(code).toLowerCase();
47
+			
48
+			if(code == 188) character=","; //If the user presses , when the type is onkeydown
49
+			if(code == 190) character="."; //If the user presses , when the type is onkeydown
50
+
51
+			var keys = shortcut_combination.split("+");
52
+			//Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
53
+			var kp = 0;
54
+			
55
+			//Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
56
+			var shift_nums = {
57
+				"`":"~",
58
+				"1":"!",
59
+				"2":"@",
60
+				"3":"#",
61
+				"4":"$",
62
+				"5":"%",
63
+				"6":"^",
64
+				"7":"&",
65
+				"8":"*",
66
+				"9":"(",
67
+				"0":")",
68
+				"-":"_",
69
+				"=":"+",
70
+				";":":",
71
+				"'":"\"",
72
+				",":"<",
73
+				".":">",
74
+				"/":"?",
75
+				"\\":"|"
76
+			}
77
+			//Special Keys - and their codes
78
+			var special_keys = {
79
+				'esc':27,
80
+				'escape':27,
81
+				'tab':9,
82
+				'space':32,
83
+				'return':13,
84
+				'enter':13,
85
+				'backspace':8,
86
+	
87
+				'scrolllock':145,
88
+				'scroll_lock':145,
89
+				'scroll':145,
90
+				'capslock':20,
91
+				'caps_lock':20,
92
+				'caps':20,
93
+				'numlock':144,
94
+				'num_lock':144,
95
+				'num':144,
96
+				
97
+				'pause':19,
98
+				'break':19,
99
+				
100
+				'insert':45,
101
+				'home':36,
102
+				'delete':46,
103
+				'end':35,
104
+				
105
+				'pageup':33,
106
+				'page_up':33,
107
+				'pu':33,
108
+	
109
+				'pagedown':34,
110
+				'page_down':34,
111
+				'pd':34,
112
+	
113
+				'left':37,
114
+				'up':38,
115
+				'right':39,
116
+				'down':40,
117
+	
118
+				'f1':112,
119
+				'f2':113,
120
+				'f3':114,
121
+				'f4':115,
122
+				'f5':116,
123
+				'f6':117,
124
+				'f7':118,
125
+				'f8':119,
126
+				'f9':120,
127
+				'f10':121,
128
+				'f11':122,
129
+				'f12':123
130
+			}
131
+	
132
+			var modifiers = { 
133
+				shift: { wanted:false, pressed:false},
134
+				ctrl : { wanted:false, pressed:false},
135
+				alt  : { wanted:false, pressed:false},
136
+				meta : { wanted:false, pressed:false}	//Meta is Mac specific
137
+			};
138
+                        
139
+			if(e.ctrlKey)	modifiers.ctrl.pressed = true;
140
+			if(e.shiftKey)	modifiers.shift.pressed = true;
141
+			if(e.altKey)	modifiers.alt.pressed = true;
142
+			if(e.metaKey)   modifiers.meta.pressed = true;
143
+                        
144
+			for(var i=0; k=keys[i],i<keys.length; i++) {
145
+				//Modifiers
146
+				if(k == 'ctrl' || k == 'control') {
147
+					kp++;
148
+					modifiers.ctrl.wanted = true;
149
+
150
+				} else if(k == 'shift') {
151
+					kp++;
152
+					modifiers.shift.wanted = true;
153
+
154
+				} else if(k == 'alt') {
155
+					kp++;
156
+					modifiers.alt.wanted = true;
157
+				} else if(k == 'meta') {
158
+					kp++;
159
+					modifiers.meta.wanted = true;
160
+				} else if(k.length > 1) { //If it is a special key
161
+					if(special_keys[k] == code) kp++;
162
+					
163
+				} else if(opt['keycode']) {
164
+					if(opt['keycode'] == code) kp++;
165
+
166
+				} else { //The special keys did not match
167
+					if(character == k) kp++;
168
+					else {
169
+						if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug created by using lowercase
170
+							character = shift_nums[character]; 
171
+							if(character == k) kp++;
172
+						}
173
+					}
174
+				}
175
+			}
176
+			
177
+			if(kp == keys.length && 
178
+						modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
179
+						modifiers.shift.pressed == modifiers.shift.wanted &&
180
+						modifiers.alt.pressed == modifiers.alt.wanted &&
181
+						modifiers.meta.pressed == modifiers.meta.wanted) {
182
+				callback(e);
183
+	
184
+				if(!opt['propagate']) { //Stop the event
185
+					//e.cancelBubble is supported by IE - this will kill the bubbling process.
186
+					e.cancelBubble = true;
187
+					e.returnValue = false;
188
+	
189
+					//e.stopPropagation works in Firefox.
190
+					if (e.stopPropagation) {
191
+						e.stopPropagation();
192
+						e.preventDefault();
193
+					}
194
+					return false;
195
+				}
196
+			}
197
+		}
198
+		this.all_shortcuts[shortcut_combination] = {
199
+			'callback':func, 
200
+			'target':ele, 
201
+			'event': opt['type']
202
+		};
203
+		//Attach the function with the event
204
+		if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
205
+		else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
206
+		else ele['on'+opt['type']] = func;
207
+	},
208
+
209
+	//Remove the shortcut - just specify the shortcut and I will remove the binding
210
+	'remove':function(shortcut_combination) {
211
+		shortcut_combination = shortcut_combination.toLowerCase();
212
+		var binding = this.all_shortcuts[shortcut_combination];
213
+		delete(this.all_shortcuts[shortcut_combination])
214
+		if(!binding) return;
215
+		var type = binding['event'];
216
+		var ele = binding['target'];
217
+		var callback = binding['callback'];
218
+
219
+		if(ele.detachEvent) ele.detachEvent('on'+type, callback);
220
+		else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
221
+		else ele['on'+type] = false;
222
+	}
223
+}

正在加载...
取消
保存